简体   繁体   中英

how to update form data with submit button in a while loop

I am trying to figure out mysqli (I am just a starting scripter). I created the following script to grab 3 different values from my database. And it prints it on the screen in different textareas and input fields.

What I want to be able to do is when I press the update button that it'll update the records in the database for the form where the button is attached to.

Can anyone give me some tips on how to achieve something like that?

 <?php $sqlserver = <SQLSERVER>; $sqluser = <SQLUSER>; $sqlpassword = <SQLPASSWORD>; $sqldatabase = <SQLDATABASE>; $mysqli = new mysqli($sqlserver, $sqluser, $sqlpassword, $sqldatabase); $loggedinuserid= "5"; $standaardtekstlabel = $mysqli->query("SELECT standaardtekst_label FROM Standaardteksten WHERE standaardtekst_account_pID='".$loggedinuserid."'"); $standaardtekstnl = $mysqli->query("SELECT standaardtekst_tekst FROM Standaardteksten WHERE standaardtekst_account_pID='".$loggedinuserid."'"); $standaardteksten = $mysqli->query("SELECT standaardtekst_tekst_en FROM Standaardteksten WHERE standaardtekst_account_pID='".$loggedinuserid."'"); while ($NL_Tekst = mysqli_fetch_row($standaardtekstnl)) { $label_Tekst = mysqli_fetch_row($standaardtekstlabel); $EN_Tekst = mysqli_fetch_row($standaardteksten); print '<form action="" method="POST"> <input type="text" name="standaardtekst_label" value=' . $label_Tekst[0] . '> <textarea name="standaardtekst_tekst">' . $NL_Tekst[0] . '</textarea> <textarea name="standaardtekst_tekst_en">' . $EN_Tekst[0] . '</textarea> <input type="submit" value="update"> </form>'; } ?> 

First of all, there is absolutely 0.0 reason why you're using 3 queries for the information you're trying to get. You can simply have: $standaardtekst = $mysqli->query("SELECT standaardtekst_label,standaardtekst_tekst,standaardtekst_en FROM Standaardteksten WHERE standaardtekst_account_pID='".$loggedinuserid."'");

Now regarding your question that is now probably obsolete:

Make the names of the input like this: standaardtekst_tekst[] saving it in an array.

You also need to have a unique(auto increment) key in your database like: id and put it in every form. You can even use the value of this field in the name like this: standaardtekst_tekst[$id] .

You could edit your code a bit to look something like this:

<?php
$sqlserver = <SQLSERVER>;
$sqluser = <SQLUSER>;
$sqlpassword = <SQLPASSWORD>;
$sqldatabase = <SQLDATABASE>;

$mysqli = new mysqli($sqlserver, $sqluser, $sqlpassword, $sqldatabase);

$loggedinuserid= "5";

$q = $mysqli->query("SELECT standaardtekst_id, standaardtekst_label, 
                            standaardtekst_tekst, standaard_tekst_tekst_en 
                    FROM Standaardteksten 
                    WHERE standaardtekst_account_pID='".$loggedinuserid."'");


while ($NL_Tekst = mysqli_fetch_row($standaardtekstnl))
{
    $row = mysqli_fetch_row($q);
    ?>
    <form action="" method="POST">
        <input type="text" name="formData[<?= $row['id']; ?>][standaardtekst_label]" value="<?= $row['standaardtekst_label']; ?>">
        <textarea name="formData[<?= $row['id']; ?>][standaardtekst_tekst]"><?= $row['standaardtekst_tekst']; ?></textarea>
            <textarea name="formData[<?= $row['id']; ?>][standaardtekst_tekst_en]"><?= $row['standaardtekst_tekst_en']; ?></textarea>
        <input type="submit" value="update">
    </form>
    <?php
}
?>

What I've done:

  • Made your 3 queries into a single query
  • Gave each form a unique id
  • Cleaned up the code a bit

This enables you to do the following:

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formData'])) {
    foreach ($_POST['formData'] as $id => $value) {
        $stmt = $mysqli->query("UPDATE standaardtekst SET standaardtekst_label='".$value['standaadtekst_label']."', standaardtekst_tekst='".$value['standaardtekst_tekst']."', standaardtekst_tekst_en='".$value['standaardtekst_tekst_en']."' WHERE standaardtekst_id='".$id."'");
    }
}

Thx for the help everyone. Everything is working right now.

This is the script i used to get it to work :-)

 <?php $sqlserver = <SQLSERVER>; $sqluser = <SQLUSER>; $sqlpassword = <SQLPASSWORD>; $sqldatabase = <SQLDATABASE>; $mysqli = new mysqli($sqlserver, $sqluser, $sqlpassword, $sqldatabase); $loggedinuserid= "5"; $result = $mysqli->query("SELECT * FROM Standaardteksten WHERE standaardtekst_account_pID='".$loggedinuserid."'"); $row_s = $result->fetch_assoc(); do{ print '<form action="" method="POST"> <input type="text" name="standaardtekst_label" value=' . $row_s['standaardtekst_label'] . '> <textarea name="standaardtekst_tekst">' . $row_s['standaardtekst_tekst'] . '</textarea> <textarea name="standaardtekst_tekst_en">' . $row_s['standaardtekst_tekst_en'] . '</textarea> <input type="text" name="standaardtekst_ID" value="'. $row_s['standaardtekst_ID'] .'"/> <input type="submit" value="update"> </form>'; } while($row_s = $result->fetch_assoc()); if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['standaardtekst_ID'])) { $updatesql= sprintf("UPDATE Standaardteksten SET standaardtekst_label='%s', standaardtekst_tekst='%s', standaardtekst_tekst_en='%s' WHERE standaardtekst_ID='%s'", $_POST[standaardtekst_label], $_POST[standaardtekst_tekst], $_POST[standaardtekst_tekst_en], $_POST[standaardtekst_ID] ); $mysqli->query($updatesql); echo "Het volgende wordt aangepast: <br />", "Label:", $_POST[standaardtekst_label], "<br />" , "NL tekst:", $_POST[standaardtekst_tekst], "<br />" , "EN tekst:", $_POST[standaardtekst_tekst_en]; echo "<meta http-equiv='refresh' content='1;url=/form.php'>"; } ?> 

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