1

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

}
?>
3
  • and what's the problem? any error messages? Commented Mar 25, 2014 at 19:06
  • 3
    You almost certainly shouldn't be establishing a database connection within a foreach loop. Where do you believe is '$id', and '$indsæt' are coming from in your code? Commented Mar 25, 2014 at 19:08
  • Félix Gagnon-Grenier: The problem is, that it does not update. No error messages. Zoredache: If you have a better solution, I am willing to try. This is the best I have come up with so far... $indsæt is wrong, it should be $text Commented Mar 25, 2014 at 19:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OMFG! It freakings works!! Where were you four days ago ;) Thank you SO much :D :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.