mysqli - php update dynamic sql rows through form

701

My goal is to retrieve a dynamic SQL list of rows and edit every item in the list within the same page using POST (no multiple changes at once required). Can this be achieved within the same page? I made the script below which displays everything the way i want to, however the switch statement doesn't seem to be fit for purpouse.

I am aware this code might not be best practice (not to mention secure). Sorry for being inflexible, but i'm not allowed to use anything other then PHP/HTML

Code:

<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
  echo '<tr> <td >' .$row["id"]. '</td>' . "\n";
  echo '<td > <form action= "" method="post"> <input type="text" name="voornaam"   value="'.$row["voornaam"].'" > <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
  echo '<td > <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" > <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n"; 
  echo '<td > <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" > <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
} else {
echo "0 resultaten";
}
switch(!empty($_POST))
{
case !empty($_POST[$row["voornaam"]]):
   // update SQL query 'firstname' here
  break;

}
$conn->close();
?>
516

Answer

Solution:

By this way you actualy see what you are editing and it's more clean.

// PDO DB conecction (secure way)
    try {
        $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        }catch(PDOException $exception){ //to handle connection error
        echo "Connection error: " . $exception->getMessage();
        }

if isset($_POST["voornaam"]){
 $stmt = $con->prepare(" QUERY");
        $stmt->execute();
echo "YEAH you did it";
} else {
stmt = $con->prepare(" QUERY");
        $stmt->execute();
        $rows = $stmt->fetchAll();

    foreach ($rows as $rs)
        {
            $voornaam = $rs["voornaam"]
    $achternaam = $rs["achternaam"]
    $omschrijving = $rs["omschrijving"]
            }
}

    // Just an example
    echo '<form action= "" method="post"> <input type="text" name="voornaam"      value="'. $voornaam .'" class="page_speed_780594453"><input name="submit" type ="submit"  value="Submit"><br/><br/>';

Value is also the input so you can have only one Submit button at the end cause all other data will be overwritten by itself.

354

Answer

Solution:

<?php
if ($result->num_rows > 0) 
{
  // output data of each row
    while($row = $result->fetch_assoc()) 
    {
      echo '<tr> <td >' .$row["id"]. '</td>' . "\n";
      echo '<td > <form action= "" method="post"> <input type="text" name="voornaam"   value="'.$row["voornaam"].'" > <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
      echo '<td > <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" > <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n"; 
      echo '<td > <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" > <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
    }
}
else 
{
  echo "0 resultaten";
}


if( isset( $_POST[$row["voornaam"]] ) && !empty( $_POST[$row["voornaam"]] ) )
{
  // update SQL query 'firstname' here
}
  $conn->close();
?>
997

Answer

Solution:

In your switch you're sending!empty($_POST) so boolean (true or false) so unique cases would be: True or False...

It's better if you change your switch to:

if(isset($_POST[$row["voornaam"]]) && !empty($_POST[$row["voornaam"]])){
  // update SQL query 'firstname' here
}

People are also looking for solutions to the problem: php - How do I open an mp3 file and check for frames to validate it?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.