<html> <?php try { $pdo = new PDO("mysql:host=localhost;dbname=testdb", "root",""); foreach($pdo->query('SELECT * from test_table') as $test) {//->はインスタンスのプロパティを参照する演算子 print_r($test); printf("<br>"); } if ($pdo == null){ print('接続に失敗しました。<br>'); }else{ } }catch (PDOException $e){ print('Error:'.$e->getMessage()); die(); } $pdo = null; ?> <body> <form method="POST" action=""> <input type="text" name="name"> <input type="text" name="name1"> <input type="text" name="name2"> <input type="submit" value="送信" name="sub1"> </form> <?php //buttonが押されたときの処理 if (isset($_POST["sub1"])) { $pdo = new PDO("mysql:host=localhost;dbname=testdb", "root",""); $stmt = $pdo -> prepare("INSERT INTO test_table (user_id, user_name,kamoku) VALUES (:user_id,:user_name,:kamoku)"); $stmt->bindValue(':user_id',$_POST["name"], PDO::PARAM_INT); $stmt->bindValue(':user_name',$_POST["name1"], PDO::PARAM_STR); $stmt->bindValue(':kamoku',$_POST["name2"], PDO::PARAM_STR); $stmt->execute(); } ?> </body> </html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。