タイトル通り、Warningを解決できないのでご助力願いたいです。
PHP初心者です。
あるhtmlファイルからformで登録したい変数を送信し、
php側でINSERT文を書いています
Warning: Use of undefined constant qry - assumed 'qry' (this will throw an Error in a future version of PHP) in /var/www/html/test_db_POST.php on line 18
(最下部にhtml出力載せています。)
環境
- リストPHP Version 7.4.3
- Apache/2.4.41 (Ubuntu)
- mysql Ver 8.0.28-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))
- mysqlnd 7.4.3
form送信側html(前後は省略)
html
1 <form action="test_db_POST.php" method="post"> 2 <ul> 3 <li> 4 <label for="char">文字:</label> 5 <input type="text" name="char2" value="" rows="10", cols="20"> 6 </li> 7 <input type="submit" value="送信"> 8 <input type="reset" value="リセット"> 9 </ul> 10 </form>
test_db_POST.php INSERT文作成、送信
PHP
1<body> 2<header> 3<h1>PHP</h1> 4</header> 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 6<main> 7<pre> 8 9<?php 10// データベースと接続 11include 'get_db_connection.php'; 12$db = get_db_connection(); 13 14$qry = 'INSERT INTO test_tbl (char2) VALUES (:char2);'; 15 16$stmt = $db->prepare(${qry}); 17 18$stmt->bindValue(':char2', $_POST["char2"], PDO::PARAM_STR); 19$stmt->execute(); 20 21echo "<h2>データ登録が完了しました</h2>"; 22?> 23</pre> 24</main> 25</body> 26
get_db_connection.php(接続はうまくいきます。)
PHP
1?php 2function get_db_connection(){ 3 try{ 4 $DB_DATABASE = 'household_management'; 5 $DB_USERNAME = '省略'; 6 $DB_PASSWORD = '省略'; 7 $DB_OPTION = 'charset=utf8'; 8 9 $PDO_DSN = "mysql:host=localhost;dbname=" . $DB_DATABASE . ";" . $DB_OPTION; 10 $db = new PDO($PDO_DSN, $DB_USERNAME, $DB_PASSWORD, 11 [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); 12 13 echo "DB接続成功<br/>\n"; 14 return $db; 15 } catch(PDOException $e){ 16 echo 'DB接続失敗' . $e->getMessage(); 17 return NULL; 18 } 19} 20?>
test_db_POST.phpのページ遷移後、出力内容
html
1<body> 2<header> 3<h1>PHP</h1> 4</header> 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 6<main> 7<pre> 8 9DB接続成功<br/> 10<br /> 11<b>Warning</b>: Use of undefined constant qry - assumed 'qry' (this will throw an Error in a future version of PHP) in <b>/var/www/html/test_db_POST.php</b> on line <b>18</b><br /> 12<h2>データ登録が完了しました</h2> 13</pre> 14</main> 15</body>
mysqlのテーブル定義
sql
1CREATE TABLE `test_tbl` ( 2 `int1` INT(10) NOT NULL AUTO_INCREMENT, 3 `char2` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci', 4 `date3` DATE NULL DEFAULT NULL, 5 PRIMARY KEY (`int1`) USING BTREE 6) 7COMMENT='基本的なクエリの確認\r\n接続確認、入力テスト、その他いろいろのためのテーブル\r\n' 8COLLATE='utf8mb4_0900_ai_ci' 9ENGINE=InnoDB 10AUTO_INCREMENT=20 11;
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/30 13:27
2022/03/31 06:04