我的问题:
<?php //sqltest.php
//Part 01 - The first part of the code will establish connection with DB using mysqli method
require_once 'login.php';
$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error) die ($conn->connect_error);
// Part - 02 - Here is the method to delete some data using query by taking input and later checking using isset
if (isset($_POST['delete']) && isset ($_POST['isbn'])){
$isbn = get_post($conn,'isbn');
$query ="DELETE FROM classics WHERE isbn = '$isbn'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>". $conn->error . "<br><br>";
}
//Part 04 - Here is the method to insert some data using query by taking input by get_post method-(see the last code) and checking using isset
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn'])){
$author = get_post($conn,'author');
$title = get_post($conn,'title');
$category = get_post($conn,'category');
$year = get_post($conn,'year');
$isbn = get_post($conn,'isbn');
$query = "INSERT INTO classics VALUES" . "('$author','$title','$category','$year','$isbn')";
$result = $conn->query($query);
if (!$result) echo "INSERT failed: . $query<br> ". $conn->error. "<br><br>";
}
//Part - 05 - FORM handler
echo <<<_END
<form action="sqltest.php"
method="post">
<pre>
Author <input type = "text" name ="author">
Title <input type = "text" name = "title">
Category <input type = "text" name = "category">
Year <input type = "text" name = "year">
ISBN <input type = "text" name = "isbn">
<input type = "submit" value = "ADD RECORD">
</pre>
</form>
_END;
// Part - 06 -A new query for showing the whole classics table from DB
$query = "SELECT * FROM classics";
$result = $conn->query($query);
if(!$result) die ("Database access failed: ". $conn->error);
$rows = $result->num_rows;
for ($j=0; $j<$rows; ++$j){
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
// Part - 07 The following html code will take the iput for deleting any entry using isbn - refers to 1st part of the code
echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Category $row[2]
Year $row[3]
ISBN $row[4]
</pre>
<form action = "sqltest.php" method = "post">
<input type ="hidden" name = "delete" value = "yes">
<input type = "hiddden" name = "isbn" value = "$row[4]">
<input type="submit" value = "DELETE RECORD">
</form>
_END;
}
$result->close();
$conn->close();
//Part 08 - actually the code begins from here
function get_post($conn,$var)
{
return $conn->real_eascape_string($_POST[$var]);
//to avoid special charecter
}
?>
/** 代码工作正常。除了两件事:1.在代码的第7部分中,我提到了要隐藏的ISBN号,只显示删除按钮。但在输出中,它同时显示数字和按钮。2. 带有记录字段的框未根据其设置,这看起来不如预期 - 我使用了 pre,但它仍然显示损坏的输出。
对于#1,您在hiddden
中有一个拼写错误(正确的应该是hidden
(。
对于 #2,了解如何使用 css 设置表单样式。另请了解如何使用 html 标签标签。
有些人建议使用表格进行格式化,这不是最佳实践,应该避免。
通常,HTML 应该只包含有关您的内容的信息,而 CSS 负责内容的呈现。这称为关注点分离。
isbn 是因为您有拼写问题。您在第 3 部分中用 7 d 隐藏了。希望这对:)有所帮助<</p>
<?php //sqltest.php
//Part 01 - The first part of the code will establish connection with DB using mysqli method
require_once 'login.php';
$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error) die ($conn->connect_error);
// Part - 02 - Here is the method to delete some data using query by taking input and later checking using isset
if (isset($_POST['delete']) && isset ($_POST['isbn'])){
$isbn = get_post($conn,'isbn');
$query ="DELETE FROM classics WHERE isbn = '$isbn'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>". $conn->error . "<br><br>";
}
//Part 04 - Here is the method to insert some data using query by taking input by get_post method-(see the last code) and checking using isset
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn'])){
$author = get_post($conn,'author');
$title = get_post($conn,'title');
$category = get_post($conn,'category');
$year = get_post($conn,'year');
$isbn = get_post($conn,'isbn');
$query = "INSERT INTO classics VALUES" . "('$author','$title','$category','$year','$isbn')";
$result = $conn->query($query);
if (!$result) echo "INSERT failed: . $query<br> ". $conn->error. "<br><br>";
}
//Part - 05 - FORM handler
echo <<<_END
<form action="sqltest.php"
method="post">
<pre>
Author <input type = "text" name ="author">
Title <input type = "text" name = "title">
Category <input type = "text" name = "category">
Year <input type = "text" name = "year">
ISBN <input type = "text" name = "isbn">
<input type = "submit" value = "ADD RECORD">
</pre>
</form>
_END;
// Part - 06 -A new query for showing the whole classics table from DB
$query = "SELECT * FROM classics";
$result = $conn->query($query);
if(!$result) die ("Database access failed: ". $conn->error);
$rows = $result->num_rows;
for ($j=0; $j<$rows; ++$j){
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
// Part - 07 The following html code will take the iput for deleting any entry using isbn - refers to 1st part of the code
echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Category $row[2]
Year $row[3]
ISBN $row[4]
</pre>
<form action = "sqltest.php" method = "post">
<input type ="hidden" name = "delete" value = "yes">
<input type = "hidden" name = "isbn" value = "$row[4]">
<input type="submit" value = "DELETE RECORD">
</form>
_END;
}
$result->close();
$conn->close();
//Part 08 - actually the code begins from here
function get_post($conn,$var)
{
return $conn->real_escape_string($_POST[$var]);
//to avoid special charecter
}
?>