###前提・実現したいこと
こんちわ、phpとMYSQLを勉強してます
IDを検索すると名前が出る仕組みをやってますが、
nvalid data source nameという文章がでてしまい、
番号検索とその結果が出ません。
なお、これが出た後に時間の打刻が出来るようにしたいです。
どなたか、よろしくお願いします。
###該当のソースコード
<form action="Attendances.php" method="post" > <table border> <tr> <th>会員番号</th> <td><input type="text" name="id"size=" 10" placeholder="00000"></td> <td rowspan="2"><input type="submit" name="namesearch" value="検索"></td> </tr> <tr> <th>名前</th> <td><input type="text" name="fname" size=" 10" maxlength="50" placeholder="愛福太郎"></td> </tr> <tr> <th rowspan="2">登録</th> <td><input type="button" value="出勤" onClick="Attendance()"></td> <td><input type="time" name="time3"></td> </tr> <tr> <td><input type="button" value="退勤" onClick="Taikin()"></td> <td><input type="time" name="time4"></td> </tr> </table> </form> <?php try { if (isset($_GET['id'])) { if ($_GET['id'] =="") { throw new Exception("idを入力してください"); } } //データベースに接続 $dbname = "myfirstdatabase"; $username = "root"; $password1 = ""; // PDO等使用してMySQLからデータを取得 $db = new PDO("$dbname",$username,$password1); //名前をなしにする $name_value = null; // $_POSTにidが入っていたら if(isset($_POST['id'])){ $id = $_POST['id']; $qry ="SELECT * FROM users WHERE fname LIKE:id";// DBから検索 $id = "%".$id."%"; $stmt = $db->prepare($qry); $stmt->bindParam(":id",$id); $stmt->execute(); } echo "<table border> <tr> <th>会員番号</th> <th>名前</th> </tr>"; while ($value = $stmt->fetch()) { $id = $value['id']; $fname = $value['fname']; echo "<tr> <td>{$id}</td> </tr> <tr> <td>{$fname}</td> <tr> </tr>"; } echo "</table>"; $db =null; // DBから取得した結果のnameを設定 $name_value = $colums['name']; } catch (Exception $e) { print "<h1>".$e->getMessage()."</h1>"; } ?>
訂正してみました。
<?php try { if (isset($_POST['id'])) { if ($_POST['id'] =="") { throw new Exception("idを入力してください"); } } //データベースに接続 $dbname = "myfirstdatabase"; $username = "root"; $password1 = ""; // PDO等使用してMySQLからデータを取得 $db = new PDO("mysql:dbname=$dbname;host=localhost;charset=utf8",$username,$password1); //$db->setAttribute(PDO::PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //名前をなしにする $name_value = null; // $_POSTにidが入っていたら if(isset($_POST['id'])){ $id = $_POST['id']; // DBから検索 $qry = "SELECT * FROM users WHERE name LIKE ? ESCAPE '!'"; $id = "%".$id."%"; $stmt = $db->prepare($qry); $stmt->bindParam(":id",$id); //$stmt->bindValue(1, '%' . preg_replace('/(?=[!_%])/', '!', $fname) . '%', PDO::PARAM_STR); //$stmt->execute(); } echo "<hr><h4>検索結果</h4><br>"; echo "<table border> <tr> <th>会員番号</th> <th>名前</th> <th>出勤</th> <th>退勤</th> </tr>"; while ($value = $stmt->fetch()) { $id = $value['id']; $fname = $value['fname']; echo "<tr> <td>{$id}</td> </tr> <tr> <td>{$fname}</td> <tr> </tr>"; } echo "</table>"; $db =null; // DBから取得した結果のnameを設定 //$name_value = $colums['name']; } catch (Exception $e) { print "<h4>".$e->getMessage()."</h4>"; } ?>###補足情報(言語/FW/ツール等のバージョンなど)
php、MYSQL
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/04 06:45
2016/08/04 06:51
退会済みユーザー
2016/08/04 06:52
2016/08/04 06:55
退会済みユーザー
2016/08/04 06:56
2016/08/04 08:43
2016/08/08 05:23