简体   繁体   中英

multiple row update with php into mysql database

I am trying to update multiple rows here.However I fail to point the right ID of the row.

<?php
$table = 'DynamicPage';
$query = mysql_query(Query::SelectAllFrom($table));
// Count table rows 
$count = mysql_num_rows($query);
while ($row = mysql_fetch_array($query)) {
    $id[] = $row['ID'];
    echo '        
                    <h3>Column name: </h3><input type="text" name="name" maxlength="30" value="' . $row['Name'] . '" />

                    <h3>Tekst: </h3><textarea type="text" name="fulltext[]" maxlength="2000">' . $row['FullText'] . '</textarea>';
}
echo '<input name="Submit" type="submit" value="Submit" />
                      </form>';
// Check if button name "Submit" is active, do this 
if (isset($_POST['Submit'])) {
    for ($i = 0; $i < $count; $i++) {
        $queryUP = mysql_query("UPDATE $table SET Name='" . $_POST['name'] . "' WHERE id='??????????????'");
        $result  = mysql_query($queryUP);
    }
    if ($result) {
        header("location:index.php");
    }
}
?>

So far I can update the first row (if id='1') from the last <h3>Column name: </h3><input type="text" name="name"... I know that I am not passing the ID's in the right way, but I have to idea about the syntax. If anyone has an idea, please let me know :) Thanks

Try this after your $_POST['Submit'] isset test:

for($i=0;$i<sizeof($id);$i++) {
    $queryUP = mysql_query("UPDATE $table SET Name='".$_POST['name']."' WHERE id = " . $id[$i]);
    $result = mysql_query($queryUP);
}
input type="text" ids="id[]" maxlength="30" value="'.$row['id'].'"
//then submit part
for($i=0; $i<count($_POST['id'];$i++) {
$queryUP = mysql_query("UPDATE $table SET Name='".$_POST['name']."' WHERE id='$_POST['id'][$i]'");
$result = mysql_query($queryUP);
}

You may concatenate $row['ID'] and $row['Name'] to create a name you can parse later

<h3>Column name: </h3><input type="text" name="name" maxlength="30" 
     value="' . $row['ID']  . '_' . $row['Name'] . '" />

then you can use something like:

list($name, $id) = explode($_POST['name'], '_');

** also note you have a security risk using user input directly inside SQL statement

Perhaps you should add a hidden input field with IDs:

HTML part

<input type="hidden" name="id[]" value="'.$row['ID'].'" />
<h3>Column name: </h3><input type="text" name="name[]" maxlength="30" value="'.$row['Name'].'" />
<h3>Tekst: </h3><textarea name="fulltext[]" maxlength="2000">'.$row['FullText'].'</textarea>';

PHP

for($i=0; $i<count($_POST['ID']); ++$i){
//query goes here
}

SQL QUERY

UPDATE $table SET Name='{$_POST['name'][$i]}', Tekst='{$_POST['fulltext'][$i]}' WHERE id='{$_POST['id'][$i]}'

This is from top off my head, not tested, but should give you an idea. And of course, escape all the input fields.

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