james
james

Reputation: 25

Updating data from submission form into MySQL database

I am trying to update data from a submission form into the MySQL database using PHP. When I click on the button to update the value in the database, it becomes empty.

Additionally, I also receive the following two error messages :

Notice: Undefined variable: id in C:\xampp\htdocs\test1\edit.php on line 64

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test1\edit.php on line 66

The following is my PHP Code:

<?php

  $name = '';

  if (isset($_GET['editQ'])) {
    $ok = true;
    $id = $_GET['editQ'];

    if ($ok) {
      // add database code here
      $db = mysqli_connect('localhost', 'root', '', 'test2015');
      $sql = sprintf("UPDATE question SET question_body='%s'
              WHERE question_id=%s",
      mysqli_real_escape_string($db, $name),$id);
      mysqli_query($db, $sql);
      echo '<p>User updated.</p>';
      mysqli_close($db);
    }
    } else {
      $db = mysqli_connect('localhost', 'root', '', 'test2015');
      $sql = sprintf('SELECT * FROM question WHERE question_id=%s', $id);
      $result = mysqli_query($db, $sql);
      foreach ($result as $row) {
        $name = $row['question_body'];

      }
      mysqli_close($db);
    }
  ?>

  <form name="editQ" method="POST" action="edit.php" > 
    <td>Please Edit the Question</td> 
    <input type="text" name="<?php echo ($q)?>" value="<?php
            echo htmlspecialchars($name);?>" /> 
    <input type="submit" name="submit" value="Edit">
  </form>

Any help/advice would be much appreciated. Thanks in advance.

Upvotes: 1

Views: 71

Answers (4)

CT14.IT
CT14.IT

Reputation: 1737

$id is not being set before the SQL statement is being called. It's in the else section of the if statement isset($_GET['editQ']) which defines $id;

Also instead of foreach use the following with a mysqli result

while($row = $result->fetch_assoc())
{
    // STUFF
}

Upvotes: 0

user1717828
user1717828

Reputation: 7225

Try pulling the $id assignment outside the if statement:

<?php
  $name = '';


  if (isset($_GET['editQ'])) {
    $ok = true;
    $id = $_GET['editQ'];

    if ($ok) {
        // add database code here
        $db = mysqli_connect('localhost', 'root', '', 'test2015');
        $sql = sprintf("UPDATE question SET question_body='%s'
          WHERE question_id=%s",
          mysqli_real_escape_string($db, $name),
          $id);
        mysqli_query($db, $sql);
        echo '<p>User updated.</p>';
        mysqli_close($db);
    }
    else {
      $db = mysqli_connect('localhost', 'root', '', 'test2015');
      $sql = sprintf('SELECT * FROM question WHERE question_id=%s', $id);
      $result = mysqli_query($db, $sql);
      foreach ($result as $row) {
        $name = $row['question_body'];
      }
      mysqli_close($db);
    }
  }
?>


Edit: the above code clears the unset variable error, but the user is still working on distinguishing $id from $editQ.

Upvotes: 0

Mateusz Majewski
Mateusz Majewski

Reputation: 280

Your form is sending POST and you're trying to get values using GET.

Upvotes: 2

n-dru
n-dru

Reputation: 9430

You don't initialize $id if it is not set in $_GET['editQ']. If however in that case you still need it, I guess you know some default id for that instance. Define it then at the top of the script in case it is later not overwritten by $_GET value.

Upvotes: 0

Related Questions