Title: -6ren">
gpt4 book ai didi

php - 如何通过 PHP 表单更新特定行,但我得到 undefined index : id

转载 作者:行者123 更新时间:2023-11-29 13:19:00 25 4
gpt4 key购买 nike

我想通过创建、阅读、更新和删除来创建图书库存。除了更新查询之外,我一切顺利。这是我的代码。首先是 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 行收到错误 undefined index :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>";
}
?>

请帮我解决这个错误:

undefined index: id at edit.php.

最佳答案

我对糟糕的格式感到震惊,无法阅读代码。

但是根据您的描述,您会得到 undefined index :id,因为您没有在表单中包含id字段,或者您将其包含在url中但使用$_POST 而不是 $_GET

<小时/>

编辑:

如果这正是您的代码,那么问题应该出在 proj.php 中的最后几行。

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

所做的那样

关于php - 如何通过 PHP 表单更新特定行,但我得到 undefined index : id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21105642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com