简体   繁体   中英

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. The PHP seems to run (kind of) but updates my table with 'Array' in all the affected columns. 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 :)

HTML and PHP code below.

HTML form within PHP echo statement

<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

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?? 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.

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. 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."");

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