タイトル通り、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
<form action="test_db_POST.php" method="post"> <ul> <li> <label for="char">文字:</label> <input type="text" name="char2" value="" rows="10", cols="20"> </li> <input type="submit" value="送信"> <input type="reset" value="リセット"> </ul> </form>
test_db_POST.php INSERT文作成、送信
PHP
<body> <header> <h1>PHP</h1> </header> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <main> <pre> <?php // データベースと接続 include 'get_db_connection.php'; $db = get_db_connection(); $qry = 'INSERT INTO test_tbl (char2) VALUES (:char2);'; $stmt = $db->prepare(${qry}); $stmt->bindValue(':char2', $_POST["char2"], PDO::PARAM_STR); $stmt->execute(); echo "<h2>データ登録が完了しました</h2>"; ?> </pre> </main> </body>
get_db_connection.php(接続はうまくいきます。)
PHP
?php function get_db_connection(){ try{ $DB_DATABASE = 'household_management'; $DB_USERNAME = '省略'; $DB_PASSWORD = '省略'; $DB_OPTION = 'charset=utf8'; $PDO_DSN = "mysql:host=localhost;dbname=" . $DB_DATABASE . ";" . $DB_OPTION; $db = new PDO($PDO_DSN, $DB_USERNAME, $DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); echo "DB接続成功<br/>\n"; return $db; } catch(PDOException $e){ echo 'DB接続失敗' . $e->getMessage(); return NULL; } } ?>
test_db_POST.phpのページ遷移後、出力内容
html
<body> <header> <h1>PHP</h1> </header> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <main> <pre> DB接続成功<br/> <br /> <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 /> <h2>データ登録が完了しました</h2> </pre> </main> </body>
mysqlのテーブル定義
sql
CREATE TABLE `test_tbl` ( `int1` INT(10) NOT NULL AUTO_INCREMENT, `char2` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci', `date3` DATE NULL DEFAULT NULL, PRIMARY KEY (`int1`) USING BTREE ) COMMENT='基本的なクエリの確認\r\n接続確認、入力テスト、その他いろいろのためのテーブル\r\n' COLLATE='utf8mb4_0900_ai_ci' ENGINE=InnoDB AUTO_INCREMENT=20 ;
まだ回答がついていません
会員登録して回答してみよう