dbへsql文で挿入をしたいのですが、pdoの使い方、記述の仕方がわからなく、エラーがでます。テーブルのフィールドは8つあり、2つは時刻でNOW()関数を使用しています。
DBへINSERTを行うには、どのように記述すれば良いでしょうか?
よろしくお願いします。
【エラー】
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in
html
1<form action ="" method="post"> 2<table class="table table-bordered table-hover text-center"> 3 <tr> 4 <th class="header text-center"> 5 <div class="form-group"> 6 <span class="glyphicon glyphicon-user"><label for="customer"> 顧客名</label></span> 7 </th> 8 <td><input type="text" class="form-control" style="width:200px;" name="username" required></td> 9 </div> 10 <div class="form-group"> 11 <th class="header text-center vertical-align: middle"> 12 <span class="glyphicon glyphicon-phone-alt"><label for="tel_a"> 電話番号</label></span> 13 </th> 14 <td><input type="text" class="form-control" style="width:200px;" name="usertel" required></td> 15 </div> 16 </tr> 17 <tr> 18 <th class="header text-center vertical-align: middle"> 19 <div class="form-group"> 20 <label for="subject">件名</label></td> 21 </th> 22 <td colspan="3"><input type="text" class="form-control" style="width:400px;" name="usersubject" required></td> 23 </div> 24 </tr> 25 <tr> 26 <th class="header text-center vertical-align: middle"> 27 <div class="form-group" > 28 <label for="info">お問合内容</label> 29 </th> 30 <td colspan="3"><textarea rows="8" class="form-control" style="width:800px;" name="userinfo" required></textarea></td> 31 </div> 32 </tr> 33 <tr> 34 <th class="header text-center vertical-align: middle"> 35 <div class="form-group"> 36 <label for="inputter">登録者</label> 37 </th> 38 <td colspan="3"><select class="form-control text-center" style="width:80px;"> 39 <option>ーーー</option> 40 <option>AAAさん</option> 41 <option>BBBさん</option> 42 <option>CCCさん</option> 43 </select> 44 </td> 45 </div> 46 <tr> 47 <td class="text-center vertical-align: middle"> 48 <span class="glyphicon glyphicon-envelope"> メール送信先</span> 49 </td> 50 <td colspan="3" class="vertical-align: middle text-left"> 51 <div class="form-group"> 52 <label class="checkbox-inline"><input type="checkbox">AAA</label> 53 <label class="checkbox-inline"><input type="checkbox">BBB</label> 54 <label class="checkbox-inline"><input type="checkbox">CCC</label> 55 </div> 56 </td> 57 </tr> 58 <tr> 59 <td colspan="4"> 60 <button class="btn btn-default"><span class="glyphicon glyphicon-remove">クリア</span></button> 61 <button class="btn btn-primary" type="submit" name="insert" value="登録">登 録</button> 62 </td> 63 </tr> 64</table> 65</form> 66```php 67<?php 68$res = ""; 69if (isset($_POST['insert'])) { 70 $info_no = htmlspecialchars($_POST['info_no']); 71 $info_tel = htmlspecialchars($_POST['info_tel']); 72 $info_subject = htmlspecialchars($_POST['info_subject']); 73 $info_contents = htmlspecialchars($_POST['info_contents']); 74 $info_input = htmlspecialchars($_POST['info_input']); 75 $info_regist = htmlspecialchars($_POST['info_regist']); 76 $info_update = htmlspecialchars($_POST['info_update']); 77 $info_to = htmlspecialchars($_POST['info_to']); 78 79 $array_info = array($info_no, $info_tel, $info_subject, $info_contents, $info_input);
/******************************************************************************* ■データベース接続設定 *******************************************************************************/ //DBアカウント情報の設定 $server = 'XXX.co.jp'; //ユーザ名 $user = 'XXX'; //パスワード $password = 'xxxx'; //データベース名 $dbname = 'test_dabname'; //DNS $dnsinfo = 'mysql:dbname=' . $dbname . ';host=' . $server . ';charset=utf8'; // try { $pdo = new PDO($dnsinfo, $user, $password); $res = '接続しました'; $sql = 'INSERT INTO dot_tbl_info VALUES(?,?,?,?,?,NOW(),NOW(),?)'; $stmt = $pdo->prepare($sql); $res = $stmt->execute($array_info); } catch (Exception $e) { $res = $e->getMessage(); }
}
echo $res;
?>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。