[英]PHP Submit Inputs Foreach() ID
I'm trying to submit a form that contains a schedule for each user ID. 我正在尝试提交一个包含每个用户ID时间表的表单。 So far it looks like this:
到目前为止,它看起来像这样:
$sql = "SELECT * FROM dbtable";
$result = $conn->query($sql);
$name_info = "SELECT udidId, name FROM udid";
$name_result = $conn->query($name_info);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$udidId = $row["udidId"];
echo "<label for='hours' class='schedule'><strong>I want <span>".$row["name"]."</span>";
echo "<input type='text' name='udidId' class='hidden' value='".$row["udidId"]."' />";
echo " to be <br />allowed out between <input type='text' name='outAllowedStartHour' placeholder='8' value='" . $row["outAllowedStartHour"] . "'> - <input type='text' name='outAllowedEndHour' placeholder='8' value='" . $row["outAllowedEndHour"] . "'><br />allowed in between <div class='padd_left'></div><input type='text' name='inAllowedStartHour' placeholder='8' value='" . $row["inAllowedStartHour"] . "'> - <input type='text' name='inAllowedEndHour' placeholder='8' value='" . $row["inAllowedEndHour"] . "'></strong></label>";
}
}
if(isset($_POST["update_schedule"])) {
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value' <br />";
while($row = $result->fetch_assoc()) {
foreach($value as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
$update_pets = "UPDATE v_spottData SET $x_value = $x_value WHERE udidId = $x";
$conn->execute($update_pets);
}
}
}
However is only updating inputs from the last ID in the database, and is not updating the input values at all. 但是,仅从数据库中的最后一个ID更新输入,而根本不更新输入值。 Any suggestions?
有什么建议么?
Execute
doesn't execute
a query, it executes a prepared statement. Execute
不execute
查询,而是执行准备好的语句。 You need to use prepare
to prepare the query. prepare
来准备查询。 $updating_column
below to whatever column you are trying to update. $updating_column
下面的$updating_column
更改$updating_column
您要更新的任何列。 $columns = array('outAllowedStartHour', 'outAllowedEndHour', 'inAllowedStartHour', 'inAllowedEndHour'); // whitelist columns
if(in_array($updating_column, $columns)) {
$update_pets = "UPDATE v_spottData SET `$updating_column` = ? WHERE udidId = ?";
$stmt = $con->prepare($update_pets);
$stmt->bind_param("ii", $x_value, $x);
$stmt->execute();
} else {
echo 'Using a Not Allowed Column';
}
You can read more about prepared statements here, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php . 您可以在http://php.net/manual/en/mysqli.quickstart.prepared-statements.php上阅读有关预准备语句的更多信息。
I feel really silly, but for anyone else dealing with the issue, my solution was simple. 我真的很傻,但是对于其他处理此问题的人,我的解决方案很简单。
Try putting the PHP to handle the form submission at the top of your document, instead of at the bottom. 尝试将PHP用来处理表单提交的文档放在文档的顶部,而不是底部。 Everything worked fine once I moved it up!
一旦我将其向上移动,一切都很好!
Thank you for all of your help everyone, especially @chris85! 谢谢大家的帮助,尤其是@ chris85!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.