简体   繁体   English

使用一种HTML表单更新多个MySQL表行

[英]Update multiple MySQL table rows using one HTML form

I'm using an HTML form within PHP to try and simultaneously update multiple rows in my MySQL table. 我在PHP中使用HTML表单尝试同时更新MySQL表中的多行。 The PHP seems to run (kind of) but updates my table with 'Array' in all the affected columns. PHP似乎在运行(有点),但是在所有受影响的列中用'Array'更新了我的表。 Can someone please fill in the gaps to my code or explain what I've missed?! 有人可以填补我的代码空白或解释我错过的内容吗?

PS I know I need to escape my PHP to prevent injection attacks ... that'll come later :) PS我知道我需要逃脱我的PHP以防止注入攻击...稍后会来:)

HTML and PHP code below. 下面的HTML和PHP代码。

HTML form within PHP echo statement PHP echo语句中的HTML表单

<form action='edit.php' method='POST' id='scheduler'>

<input type='hidden' id='identifier' name='identifier[]' value='".  $row['id'] . "'>
<input type='hidden' id='assoc_to_username' name='assoc_to_username' value='" .     $schedule['assoc_to_username'] . "'>

<input id='assoc_to_date' name='assoc_to_date[]' type='text' value='" .     $schedule['assoc_to_date'] . "'/>

<select name='assoc_to_start_time[]' id='assoc_to_start_time'>
<option selected disabled='disabled' value='" . $schedule['assoc_to_start_time'] . "'>" . $schedule['assoc_to_start_time'] . "</option>
<option disabled='disabled'>----------</option>
<option value='All day'>All day</option>
</select>

<select name='assoc_to_end_time[]' id='assoc_to_end_time'>
<option selected disabled='disabled' value='" . $schedule['assoc_to_end_time'] . "'>" . $schedule['assoc_to_end_time'] . "</option>
<option disabled='disabled'>----------</option>
<option value=''>No end time</option>
<option value=''>All day</option>
</select>

<input id='taskname' name='taskname[]' type='text' value='" . $schedule['taskname'] . "'/>

<input id='submit' name='submit' class='submit' type='submit' value='Edit' type='submit' />

</form>

PHP process code PHP流程代码

require_once('../inc/database-config.php');

$id1 = $_POST['identifier'];

$assoc_to_username = $_POST['assoc_to_username'];

$assoc_to_date = $_POST['assoc_to_date'];
$assoc_to_start_time = $_POST['assoc_to_start_time'];
$assoc_to_end_time = $_POST['assoc_to_end_time'];
$taskname = $_POST['taskname'];

foreach ($_POST['identifier'] as $id1)

{
    $sql=mysql_query("UPDATE schedule SET assoc_to_date= $assoc_to_date, assoc_to_start_time= $assoc_to_start_time, assoc_to_end_time= $assoc_to_end_time, taskname=$taskname WHERE assoc_to_username=$assoc_to_username");

header("Location: ../admin.php?action=edited"); 
;}


if (!mysql_query($sql,$dbcon))

{   die (header("Location: ../error.php"));   }

mysql_close($dbcon);

Please help! 请帮忙!

I think the problem is in foreach block... other POST var are also arrays, how you can use those directly?? 我认为问题出在foreach块中...其他POST var也是数组,如何直接使用它们? change you loop like this and try: 像这样改变你的循环并尝试:

for($i =0;$i <count($_POST['identifier']);$i++)
{
    $sql=mysql_query("UPDATE schedule SET assoc_to_date= ".$assoc_to_date[$i].", assoc_to_start_time= ".$assoc_to_start_time[$i].", assoc_to_end_time= ".$assoc_to_end_time[$i].", taskname=".$taskname[$i]." WHERE assoc_to_username=".$assoc_to_username[$i].";");
    if (!mysql_query($sql,$dbcon)){
        die (header("Location: ../error.php")); 
    }
}

also add [] to name='assoc_to_username[]' input field. 还将[]添加到name ='assoc_to_username []'输入字段中。

By using square brackets [] on the input names, PHP is trying to pick up multiple inputs with that same name, even if you have one field with that name (which you do), PHP will still store that information as an array with one element. 通过在输入名称上使用方括号[],PHP会尝试选择具有相同名称的多个输入,即使您有一个使用该名称的字段(您确实这样做),PHP仍将这些信息存储为一个带有一个名称的数组。元件。 Remove the square brackets from your name attributes and you should be good to go. 从名称属性中删除方括号,您应该一切顺利。

首先,尝试更正您的查询,以便传递变量:

$sql=mysql_query("UPDATE schedule SET assoc_to_date= ".$assoc_to_date.", assoc_to_start_time=".$assoc_to_start_time.", assoc_to_end_time= ".$assoc_to_end_time.", taskname=".$taskname." WHERE assoc_to_username=".$assoc_to_username."");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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