简体   繁体   中英

Identifying specific row from SQL results

I have a list of 'orders' being pulled out, which consist of product name, description etc, one of the fields is quantity which is in an editable text box, next to that is an update button (which has an unique ID for that row pulled from the DB). Now when the update button is pressed, I want the quantity for that product to be updated. However i'm having problems getting the correct updated quantity to be matched with the ID of that row.

I can see that the problem is me setting the $quantity1 variable with just the last result pulled out inside the IF statement, but I can't think how to get it to relate the row i'm clicking on. Here is part of the code:

    echo "<td>".$row['uName']."</td>";
echo "<td>".$row['prodID']."</td>";?>
<form method="post" action="reserved.php">
<td><input name="quantity1" type="text" id="quantity1" size="1" value='<?= $qty ?>' />
<td><input name="order2"  id="order2" type="submit" class="button_add" value='<?= $row['ID']?>' /></td><?
echo "</tr>";       

}

}elseif(!empty($studyDir) && $rowCount == 0){
?>
<?
}
}
if (isset($_POST['order2'])){
$order2 = $_POST['order2'];
$quantity1 = $_POST['quantity1'];

\\echo $quantity1;

$link3 = mysql_connect('localhost', '******', '******');
$SQL1 = "UPDATE ybsinter_stock.reservedStock SET qty = $quantity1 WHERE ID = '$order2'";
$result1 = mysql_query($SQL1);  
mysql_close($link3);
unset($quantity1);
unset($order2);
header("Location:reserved.php");
}
?>  

I can't see your form ending ie there is no <\\form>. Also note that declaring forms in tables (except entirely enclosed in a td) is bad HTML, run your code through the W3C validator .

Also try PHP heredocs for outputting blocks of HTML with embedded data....

echo <<<EOF
<tr>
<td>{$row['uName']}</td>
<td>{$row['prodID']}</td>
<td>
    <form method="post" action="reserved.php">
    <input name="quantity1" type="text" id="quantity1" size="1" value="{$qty}" />
    // style this button right with CSS if you want ...
    <input name="order2"  id="order2" type="submit" class="button_add" value="{$row['ID']}" />
    </form>
</td>
</tr>
EOF;   

The above form will only submit data to your script with the id that you're interested in..

Your SQL query seems roughly correct, but beware of SQL injection - please bind your variables into your queries instead of inserting them. Use the mysqli or PDO libraries instead of the outdated basic mysql functions.

$mysqli = new mysqli( /* your connection params here */ );
$sql1 = 'UPDATE ybsinter_stock.reservedStock SET qty = ? WHERE ID = ?';

$stmt = $mysqli->query( $sql1);
$stmt->bind_param( 'sd', $quantity1, $order2);
$result = $stmt->execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM