2013-07-12 80 views
1

讓我首先說明我過去幾個月只進行了編碼。我知道我可能到處都有很多錯誤或不好的做法。我喜歡建設性的批評,所以請讓我知道我可以做得更好,以及如何解決我目前的問題。在SELECT WHERE查詢中使用遞增的值MYSQL

此代碼的用途是根據先前輸入的信息創建零件號碼錶及其相關位置列數據(存儲類型,機架號,機架號)。我有完美的參賽表格。我輸入了許多我想要搜索的部分,然後將該數字發回給自己。然後我將顯示這些文本輸入字段以輸入部件號。

//this form is to submit the number of parts you're looking for, 
    //and posts back to itself 
    <form action=View2.php method="post"> 
    <input type="text" name="num"> 
    <button type="submit">Number of Parts</button> 
    </form> 

    //This form takes the posted number, and creates that many text fields, 
    //populated with the number part you are entering. 
    <form action="List.php" method="post"> 
    <?php while ($i<=$num){ 
    echo "<input type='text' name='part$i' value='part$i'><br><br>";$i++;}?><input type="hidden" name="num" value="<?php $num?>"> 
    <button type="submit">Submit</button> 
    </form> 

我的問題與運行mysqli_query來填充表。我堅持要從這裏走出去。我知道,我需要的是被張貼每個零件編號,並把它作爲一個SELECT WHERE搜索的條件,所以我做了這個while循環:

<?php 
    echo "<table border='1'> 
     <tr> 
     <th>Part Number</th> 
     <th>Location</th> 
     <th>Rack</th> 
     <th>Shelf</th> 
     </tr>"; 

    while($i<=$num){ 
      $x=$_POST["part$i"]; 
      $result = ($con,"SELECT * FROM parts WHERE pn ='$x'"); 
      $row = ($result); 
      echo "<tr>"; 
      echo "<td>" . $x . "</td>"; 
      echo "<td>" . $row['rcb'] . "</td>"; 
      echo "<td>" . $row['ra'] . "</td>"; 
      echo "<td>" . $row['sh'] . "</td>"; 
      echo "</tr>"; 
      $i++; 
      } 

     echo "</table>";?> 

此時的頁面崩潰,但如果我註釋掉$結果行,我會得到填充了上一頁中值的零件編號字段的表格。

任何人都知道我做錯了什麼,或者我怎麼能做得更好?

+0

你必須調用mysqli的功能。 '($ con,'SELECT ...)'什麼都不做,只有parse_error。 – Fracsi

回答

1

這條線沒有做任何事情好:

$result = ($con,"SELECT * FROM parts WHERE pn ='$x'"); 

實際上你需要查詢數據庫。

$mysqli = new mysqli("localhost", "my_user", "my_password", "..."); 

... 

$result = $mysqli->query("SELECT * FROM parts WHERE pn ='$x'"); 

你應該用準備好的發言使你的代碼是不開放給SQL注入,但...

+0

我坐在這裏,我的嘴巴張開,震驚......這是我的一部分骨頭。只要我能,我會接受回答 – user2564848

+0

@ user2564848發生在我們身上。不要冒汗:P。你可以接受我的答案,如果你覺得它有幫助,我會感激它:) – Bun