使用$ _POST变量选择MySQL行

时间:2014-01-17 20:19:20

标签: php sql mysqli xhtml

我在解决如何从包含所需列值的行中打印实际值时遇到了一些问题。我有两个php文件,quantity.php和quantity-inquiry.php,其中quantity.php有一个帖子提交表单,通过quantity-inquiry.php重定向来执行实际的sql。

数量查询开启php:

//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
include 'db-connect-99k.php';
$db = new mysqli($host, $user, $pass, $databaseName);

if($db->connect_errno > 0){
  die('Unable to connect to database [' . $db->connect_error . ']');
}

//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$searchName = $_POST['name'];
$res = $db->query("SELECT * FROM inventory WHERE name=$searchName");

$ searchName从$ _POST中正确设置(我可以回显它并从数量表单字段输出正确的输入):

<p> Product Name or PPI required (both not required) </p>
<p>--------------------------------------------------</p>
<form action="quantity_inquiry.php" method="post">
    Product Name: <input type="text" name="name"><br />
    PPI: <input type="text" name="ppi"><br />
<input type="submit" name="submit" id="submit">
</form>

换句话说,如果我在quantity.php的表单字段中键入“car”,则quantity-inquiry.php中的$ name将回显“car”作为$ name变量。我遇到的问题是使用$ name作为我的查询:

$searchName = $_POST['name'];
$sql = "SELECT * FROM inventory WHERE name='".$searchName."'";
$res = $db->query($sql);

我似乎永远无法获得实际价值。我尝试了很多东西,例如:

while($row = $res->fetch_assoc()){
    $rowName = $row['name'];
    $rowPPI = $row['ppi'];
    $rowQuantity = $row['quantity'];
    $rowPrice = $row['price'];
}   

设置变量后跟

<tr>
  <td> $rowName </td>
  <td> $rowPPI </td>
  <td> $rowQuantity </td>
  <td> $rowPrice </td}
</tr>

实际上并未选择该字段。例如,如果我知道我的库存表的'name'列中有'product1',并且它被设置为我的'$ searchName',它实际上并没有从表中选择它并将其打印为$ rowName, $ rowPPI,$ rowQuantity和$ rowPrice。

我已经尝试了许多其他的东西,这些东西最终会出现某种错误或打印出不正确的值。我的查询是错误的还是我错误地设置/使用我的变量?

2 个答案:

答案 0 :(得分:1)

而不是

$searchName = $_POST['name'];
$res = $db->query("SELECT * FROM inventory WHERE name=$searchName");

你应该这样做

$searchName = $_POST['name'];
$query = "SELECT * FROM inventory WHERE name = ?";
if($stmt = $mysqli->prepare($query)){
    $stmt->bind_param('s',$searchName);
    $stmt->execute();
    $result = $stmt->get_result();

    while($row = $result->fetch_assoc()){
        $rowName = $row['name'];
        $rowPPI = $row['ppi'];
        $rowQuantity = $row['quantity'];
        $rowPrice = $row['price'];
        ?>
        <tr>
            <td><?=$rowName?></td>
            <td><?=$rowPPI?></td>
            <td><?=$rowQuantity?></td>
            <td><?=$rowPrice?></td>
        </tr>

        <?php
    }

    $stmt->free_result();
    $stmt->close();
}else die("Failed to prepare!");

或者你可以超级喜欢这样做:

$query = "SELECT name, pip, quantity, price FROM inventory WHERE name = ?";

if($stmt = $mysqli->prepare($query)){
    $stmt->bind_param('s', $_POST['name']);
    $stmt->execute();
    $stmt->bind_result($rowName, $rowPPI, $rowQuantity, $rowPrice);


    while($stmt->fetch()){
        ?>
        <tr>
            <td><?=$rowName?></td>
            <td><?=$rowPPI?></td>
            <td><?=$rowQuantity?></td>
            <td><?=$rowPrice?></td>
        </tr>
        <?php
    }

    $stmt->free_result();
    $stmt->close();
}else die("Failed to prepare!");

答案 1 :(得分:0)

所以我刚看了一下电脑太久了。简单解决一个愚蠢的错误。我离开了

$sql = "SELECT * FROM inventory WHERE name='".$searchName."'";

这是正确的,当我改变以下

$rowName -> $row['name'];

哪个错了。然后在尝试修复它时,我将$ sql更改为

$sql = "SELECT * FROM inventory WHERE name=$searchName";

这是错误的,同时将我的变量设置更改为

$rowName = $row['name'];

如果将它们设置为

那么长的故事
$sql = "SELECT * FROM inventory WHERE name='".$searchName."'";
$rowName = $row['name'];

注意 - 所有$ row *变量应设置为=而不是 - &gt;