kciren
kciren

Reputation: 1

How to update specific rows through PHP form but I get undefined index: id

I want to create a book inventory with Create, Read, Update and Delete. I got it all going except for the Update query. Here is my code. First is the proj.php

<html>
<body>

<b>BOOK INVENTORY</b>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Title: <input type="text" name="title">
Subtitle: <input type="text" name="subtitle">
Author: <input type="text" name="author">
Genre/Type: <select name="genre">
            <option value="Business">Business</option>
            <option value="Family">Family</option>
            <option value="Romance">Romance</option>
            <option value="Education">Education</option>
            <option value="Self-help">Self-help</option>
            <option value="Spiritual">Spiritual</option>
            <option value="Music">Music</option>
            </select>
Publish Date: <input type="date" name="publishdate">
Publisher: <input type="text" name="publisher">
Series: <input onkeypress="return isNumberKey(event)" type="text" name="series">
ISBN: <input type="text" name="isbn">
Pages: <input onkeypress="return isNumberKey(event)" type="text" name="pages">
Price: <input type="text" value="$" name="price">
<input type="submit" value="Create" name="submit">
</form>

<!--create new book entry-->    
<?php

include 'connection.php';

if (!isset($_POST['submit'])) {

// form not submitted
}

else {


// get form input

// check to make sure it's all there

// escape input values for greater safety

$title = empty($_POST['title']) ? die ("ERROR: Enter a Title") : mysql_escape_string($_POST['title']);
$subtitle = empty($_POST['subtitle']) ? die ("ERROR: Enter an Subtitle") : mysql_escape_string($_POST['subtitle']);
$author = empty($_POST['author']) ? die ("ERROR: Enter an Author") : mysql_escape_string($_POST['author']);
$genre = empty($_POST['genre']) ? die ("ERROR: Enter a Genre/Type") : mysql_escape_string($_POST['genre']);
$publishdate = empty($_POST['publishdate']) ? die ("ERROR: Enter a Publish Date") : mysql_escape_string($_POST['publishdate']);
$publisher = empty($_POST['publisher']) ? die ("ERROR: Enter a Publisher") : mysql_escape_string($_POST['publisher']);
$series = empty($_POST['series']) ? die ("ERROR: Enter a Series") : mysql_escape_string($_POST['series']);
$isbn = empty($_POST['isbn']) ? die ("ERROR: Enter an ISBN") : mysql_escape_string($_POST['isbn']);
$pages = empty($_POST['pages']) ? die ("ERROR: Enter a Pages") : mysql_escape_string($_POST['pages']);
$price = empty($_POST['price']) ? die ("ERROR: Enter a Price") : mysql_escape_string($_POST['price']);

// create query
$insert = "INSERT INTO books (Title, Subtitle, Author, Genre, Publishdate, Publisher, Series, ISBN, Pages, Price) 
VALUES ('$title', '$subtitle', '$author', '$genre', '$publishdate', '$publisher', '$series', '$isbn', '$pages', '$price')";

// execute query
$result = mysql_query($insert) or die ("Error in query: $insert. ".mysql_error());

// print message of the new book inserted
//echo "New book record created entitled "."$     ";


} 
if (!isset($_POST['submit'])) {
}

$query = "SELECT * FROM symbols";
?> 

<!------------><!------------><!------------><!------------><!------------>
<!------------><!------------><!------------><!------------><!------------>
<!--Delete Record-->
<?php

// set server access variables


$host = "localhost";

$user = "root";

$pass = "";

$db = "david";

// create mysqli object
// open connection
$mysqli = new mysqli($host, $user, $pass, $db);
// check for connection errors
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
// if id provided, then delete that record

if (isset($_GET['id'])) {

// create query to delete record
$query = "DELETE FROM books WHERE id = ".$_GET['id'];

// execute query
if ($mysqli->query($query)) {

// print number of affected rows
echo $mysqli->affected_rows." row(s) affected";
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
}
// query to get records
$query = "SELECT * FROM books";

// execute query
if ($result = $mysqli->query($query)) {

// see if any rows were returned
if ($result->num_rows > 0) {

    // yes

    // print them one after another

    echo "<table cellpadding=10 border=1>";

    while($row = $result->fetch_array()) {

        echo "<tr>";
        echo "<td>".$row[0]."</td>";
        echo "<td>".$row[1]."</td>";
        echo "<td><a href=".$_SERVER['PHP_SELF']."?id=".$row[0].">Delete</a></td>";
        echo "<td><a href=".'show.php'."?id=".$row[0].">More...</a></td>";
        echo "</tr>";

    }

}

// free result set memory

$result->close();

}

else {

// print error message

echo "Error in query: $query. ".$mysqli->error;

}

// close connection

$mysqli->close();

?>

<?php
include 'edit.php';
if (!isset($_POST['submit'])) {

}
?>

And here is my edit.php where I get the error undefined index : id at line 5

<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("david") or die(mysql_error());

$UID = (int)$_GET['id'];

$query = mysql_query("SELECT * FROM books WHERE 'id' = '$UID'") or die(mysql_error());

if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
    $title = $row['title'];
    $subtitle = $row['subtitle'];
    $author = $row['author'];
    $genre = $row['genre'];
    $publishdate = $row['publishdate'];
    $publisher = $row['publisher'];
    $series = $row['series'];
    $isbn = $row['isbn'];
    $pages = $row['pages'];
    $price = $row['price'];
}
?>
<form action="update.php" method="get">
<input type="hidden" name="id" value="<?=$UID;?>">
Title: <input type="text" name="ud_title" value="<?=$title?>"><br>
Subtitle: <input type="text" name="ud_subtitle" value="<?=$subtitle?>"><br>
Author: <input type="text" name="ud_author" value="<?=$author?>"><br>
Genre: <input type="text" name="ud_genre" value="<?=$genre?>"><br>
Publish Date: <input type="text" name="ud_publishdate" value="<?=$publishdate?>"><br>
Publisher: <input type="text" name="ud_publisher" value="<?=$publisher?>"><br>
Series: <input type="text" name="ud_series" value="<?=$series?>"><br>
ISBN: <input type="text" name="ud_isbn" value="<?=$isbn?>"><br>
Pages: <input type="text" name="ud_pages" value="<?=$pages?>"><br>
Price: <input type="text" name="ud_price" value="<?=$price?>"><br>
<input type="Submit">
</form>
<?php
}else{
}
?>

and here is my update.php with the update query

<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("david") or die(mysql_error());

$ud_ID = (int)$_POST["id"];

$ud_title = mysql_real_escape_string($_POST["ud_title"]);
$ud_subtitle = mysql_real_escape_string($_POST["ud_subtitle"]);
$ud_author = mysql_real_escape_string($_POST["ud_author"]);
$ud_genre = mysql_real_escape_string($_POST["ud_genre"]);
$ud_publishdate = mysql_real_escape_string($_POST["ud_publishdate"]);
$ud_publisher = mysql_real_escape_string($_POST["ud_publisher"]);
$ud_series = mysql_real_escape_string($_POST["ud_series"]);
$ud_isbn = mysql_real_escape_string($_POST["ud_isbn"]);
$ud_pages = mysql_real_escape_string($_POST["ud_pages"]);
$ud_price = mysql_real_escape_string($_POST["ud_price"]);


$query="UPDATE books
        SET title = '$ud_title', subtitle = '$ud_subtitle', author = '$ud_author', genre = '$ud_genre', publishdate = '$ud_publishdate', 
        publisher = '$ud_publisher', series = '$ud_series', isbn = '$ud_isbn', pages = '$ud_pages', price = '$ud_price'
        WHERE id='$ud_ID'";


mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($ud_ID) Record Updated<p>";
}else{
echo "<p>($ud_ID) Not Updated<p>";
}
?>

Please kindly help me get rid of this error:

undefined index: id at edit.php.

Upvotes: 0

Views: 349

Answers (1)

msg7086
msg7086

Reputation: 461

I'm shocked by the terrible formating and not able to read the code.

But according to your description, you get undefined index: id because you either did not include the id field in the form, or you include it in the url but use $_POST instead of $_GET.


EDIT:

If this is exactly the code you have, then the problem should be the last few lines in proj.php.

include 'edit.php' is not the correct way to call edit page, but you should use a link tag which links to edit.php?id=some_id, just like you did with show.php

Upvotes: 1

Related Questions