如何通过PHP表单更新特定的行,但我得到未定义的索引:id

时间:2014-01-14 03:33:17

标签: php mysql sql-update

我想创建一个包含创建,读取,更新和删除的图书清单。除了更新查询之外,我完成了所有工作。这是我的代码。首先是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'])) {

}
?>

这是我的edit.php,我在第5行得到错误未定义的索引:id

<?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{
}
?>

这是我的update.php及更新查询

<?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>";
}
?>

请帮助我摆脱这个错误:

  

未定义的索引:编辑时的ID。

1 个答案:

答案 0 :(得分:1)

我对可怕的格式化感到震惊,无法阅读代码。

但根据您的说明,您获得了undefined index: id,因为您未在表单中包含ID字段,或者您将其包含在网址中,但使用$_POST代替$_GET


编辑:

如果这正是您所拥有的代码,则问题应该是proj.php中的最后几行。

include 'edit.php'不是调用编辑页面的正确方法,但您应该使用链接到edit.php?id=some_id的链接标记,就像使用show.php

一样