简体   繁体   中英

PHP not updating rows in Database

What am I doing wrong here. I can't figure out how to make the Update block work. When I hit "Update" button, it goes back to edit.php but with

errors. Errors are: Notice: Undefined variable: POST in C:\\xampp\\htdocs\\libSys\\edit.php on line 45

Notice: Undefined variable: POST in C:\\xampp\\htdocs\\libSys\\edit.php on line 46

Notice: Undefined variable: id in C:\\xampp\\htdocs\\libSys\\edit.php on line 48

Successfully Updated

Lines 45, 46, 48 has

        echo $author = $POST['author']; //line 45
        echo $isbn = $POST['isbn']; //line 46

        $sql = "UPDATE booklist SET Author='$author', Title='title', ISBN = '$isbn' WHERE ID = '$id' ";  //line 48

The complete code of edit.php are below:

<?php
include('databaseConnection.php');
?>

<html>
<head>
    <link rel="stylesheet" type="text/css" href="./css/librarySystem.css">
    <title> Edit Book Information </title>
</head>

<div>

<?php
if(isset($_REQUEST['edit']))   //FIRST IF
{

    $id = $_REQUEST['edit'];
    $sql_select = "SELECT * FROM booklist WHERE ID=$id";
    $result=mysql_query($sql_select);

    // collect all the data using mysql_fetch_array and assign to our $variables

    while($row = mysql_fetch_array($result))
    {
        $id = $row['ID'];
        $title = $row['Title'];
        $author= $row['Author'];
        $yearLevel = $row['YearLevel'];
        $isbn = $row['ISBN'];
    }

    //displays form for user to edit book information
    echo "<form method='POST' action='edit.php'>";
        echo "Enter New or Correct Title: <input type='text'  name='title'  value='$title' >  <br />";
        echo "Enter New or Correct Author: <input type='text'   name='author'  value='$author'> <br />";
        echo "Enter New or Correct ISBN: <input type='text'  name='isbn'  value='$isbn' >  <br / >";
        echo "<input type='submit' name='save' value='Update' > <br />";
    echo "</form>";
}    //END 1st IF

else if( isset($_POST['save'] ) )    
{

        echo $title = $_POST['title'];
        echo $author = $POST['author'];
        echo $isbn = $POST['isbn'];

        $sql = "UPDATE booklist SET Author='$author', Title='title', ISBN = '$isbn' WHERE ID = '$id' ";
        $result = mysql_query($sql);
            if($result == true)
            {
                echo "Successfully Updated" ;
            }
                //header('Location: ./booklist.php'); 
}

?>

How can I correct the update block. It seems to be going to the if(isset($_POST['save']) when I hit "Update" but won't really update the row. Please help. I'm stuck. I don't understand why the "Undefined variable" error pops when I have the variables assigned with $_POST['author']...

I'd appreciate any help or explanation. Thanks.

Use super global variables as $_POST[]

 echo $author = $_POST['author'];
 echo $isbn = $_POST['isbn'];

Before updated into database use mysql_real_escape_string

$author=mysql_real_escape_string($author),
$isbn=mysql_real_escape_string($isbn));

 $sql = "UPDATE `booklist` SET Author='".$author."', Title='title', ISBN = '".$isbn."' WHERE ID = $id";

Note

mysql is deprecated instead use mysqli OR PDO

Change this line

echo $author = $POST['author'];
 echo $isbn = $POST['isbn'];

to

echo $author = $_POST['author'];
echo $isbn = $_POST['isbn'];

you have a syntax error, you use $POST instead of $_POST.

 echo $author = $_POST['author']; //line 45
 echo $isbn = $_POST['isbn']; //line 46

 $sql = "UPDATE booklist SET Author='$author', Title='title', ISBN = '$isbn' WHERE ID = '$id' ";  //line 48

The syntax for php POST global variable is $_POST not $POST that you are using.

And I guess you are getting $id from a form using $_GET or $_POST method only. So check out those syntaxes as well.

There are a couple of errors in the code. Firstly, $_POST is a superglobal in php. Superglobals start with a dollar sign and an underscore. You forgot the underscore that's why it's undefined. rewrite as:

echo $author = $_POST['author'];
echo $isbn = $_POST['isbn'];

Secondly, you're using the $id variable inside the quotes. Rewrite line 48 as:

$sql = "UPDATE booklist SET Author='{$author}', Title='title', ISBN = '{$isbn}' WHERE ID = '{$id}' "; //line 48

To use variables inside quotes without concatenation, you can use the '{' bracket. Keep in mind though, you can't do the same with constant values. Hope this helps :)

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