简体   繁体   中英

php $_POST array and update MySQL

Here is my challange:

I want to update a MySQL table with inputs from a $_POST array.

How is this done? (Spend hours upon hours looking for a solution to this).

In my “table.php” I get the data from MySQL and place it in input forms.

In my “updatesfields.php” I can't figure out how to update the fields in MySQL. (I might be way off, but that's no news)

Table.php:

<form method="POST" action="updatefields.php" enctype="multipart/form-data" >
<table border="1"><tr>
<td>ID</td>
<td>Text</td>
</tr>

<?php
$host = "xxx";
$username1 = "xxx";
$password1 = "xxx";
$db_name = "xxx";
$tbl_name = "xxx";

$conn = new PDO("mysql:host=$host;dbname=$db_name",$username1,$password1);
$sql = "SELECT * FROM $tbl_name ORDER BY bilag ASC";
$q = $conn->prepare($sql);
$q->execute(array($title));
$q->setFetchMode(PDO::FETCH_BOTH);

// fetch
while($data = $q->fetch()){

echo "<tr><td>";
// --------------------- ID -----------------------------
$id = $data[0];
if ($id != 0)
{ echo "<center><input type='text' style='font-weight:bold;' value='$id' name='id[]' size='10'>";}
else { echo "<center><input type='text' style='font-weight:bold;' value='' name='id[]' size='10'>"; }
echo "</td>";
// --------------------- ID -----------------------------


echo "<td>";
// --------------------- Text -----------------------------
$text = $data[3];
if ($text != null)
{ echo "<center><input type='text' style='font-weight:bold;' value='$text' name='text[]' size='10'>";}
else { echo "<center><input type='text' style='font-weight:bold;' value='' name='text[]' size='10'>"; }
// --------------------- Text -----------------------------
echo "</td></tr>";
}

?>
</table>
<br>
<input type="submit" value="Update">
</form>

updatefields.php:

<?php

$host = "xxx";
$username1 = "xxx";
$password1 = "xxx";
$db_name = "xxx";
$tbl_name = "xxx";

foreach ($_POST as $number => $text)
{

$conn = new PDO("mysql:host=$host;dbname=$db_name",$username1,$password1);
$sql = "UPDATE $tbl_name SET text=? WHERE id=?]";
$q = $conn->prepare($sql);
$q->execute(array($indsæt,$id)); 

}
?>

First, in the HTML, we need to change this:

<input type="submit" value="Update">

to this. Names are important attributes because they become keys in the $_POST array.

<input type="submit" name="submit" value="Update">

Then, in updatefields.php:

if (isset($_POST['submit'])){

    //how many ids came through in the $_POST array?
    $id_count = count($_POST['id']);

    //connect only once, before the loop
    $conn = new PDO("mysql:host=$host;dbname=$db_name",$username1,$password1);

    //this runs once for each id we have
    for ($i=0; $i<$id_count; $i++){
        $sql = "UPDATE $tbl_name SET text=? WHERE id=?";
        $q = $conn->prepare($sql);
        $q->bindParam(1, $_POST['text'][$i]);
        $q->bindParam(2, $_POST['id'][$i]);
        $q->execute(); 
        if ($q) {//execute() returns TRUE on success
            //insert success
        } else {
            //insert failed
        }
    }//for loop

} else {//submission did not come from form
     echo "There was a problem processing this request. <a href="Table.php">Please click here to try again.</a>";
}

You can read more about binding parameters in the PHP documentation .

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