Re: [請益] php 撈資料庫的問題

看板PHP作者 (小殘)時間16年前 (2010/01/28 12:08), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/5 (看更多)
※ 引述《godispower (omg)》之銘言: : 小弟想請問一個效果如何做 : 資料庫有四個欄位 id item barcode datearrivetom : 我現有兩頁 一頁叫做 whquery.html 一頁叫whquery.php : whquery.html 可以輸入 id, item, barcode...等的資訊 : 並透過whquery.php 去資料庫把資料抓出來秀在頁面上 : 我要的效果是 使用者輸入資訊超過三個以上.例如以下 : id: 3 : item : 33444 : barcode: 56789 : datearrivetom: 空白 : 如此輸入時,頁面上會秀出一筆三個資訊都符合的資料 : 我寫的code: : whquery.html : <form method="post" action="whquery.php"> : id : <input name="id" type="text" /><br /> : item: <input name="item" type="text" /><br /> : barcode: <input name="barcode" type="text" /><br /> : datearrivetom: <input name="datearrivetom" type="text" /><br /> : <input name="Button1" type="submit" value="query" /> : </form> : whquery.php : <?php : $conn = mysql_connect('localhost','root','680712'); : mysql_select_db("purchaseinfo",$conn); : $sql = "select * from incoming where id='$_POST[id]' and item='$_POST[item]'"; // 只做 AND $keyArray = Array('id','item',...); // 改成自己欄位 $whereStr = ''; foreach($keyArray as $key) { if(isset($_POST[$key])) { if($whereStr!='') $whereStr.=" AND "; $whereStr .= "{$key} ='" . $_POST[$key] . "'"; } } if($whereStr!='') $whereStr= " WHERE " .$whereStr; $sql = "select * from incoming {$whereStr}" : $result = mysql_query($sql,$conn); : while($row = mysql_fetch_array($result)) : { : ?> : <tr> : <td align="left" height="20"><?php echo $row["id"]?></td> : <td align="left"><?php echo $row["item"]?></td> : <td align="left"><?php echo $row["barcode"]?></td> : <td align="left"><?php echo $row["datearrivetom"]?></td> : </tr> : <?php : } : mysql_free_result($result); : mysql_close($conn); : ?> : 問題: : 當我在 whquery.html 輸入如下 : id: 3 : item : 33444 : barcode: 空白 : datearrivetom: 空白 : 這樣可以成功 但是如果輸入 : id: 3 : item : 33444 : barcode: 56789 多查一個 : datearrivetom: 空白 : 就會失敗 請問我sql那裡要怎麼改? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.96.111.96
文章代碼(AID): #1BOGt182 (PHP)
文章代碼(AID): #1BOGt182 (PHP)