我的问题:
<?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,但它仍然显示出错误的输出。/
3条答案
按热度按时间2cmtqfgy1#
显示isbn是因为您有拼写问题。你在第七部分用3d写的。希望这有帮助:)
rjee0c152#
nue99wik3#
对于#1,您在
hiddden
(正确答案应该是hidden
).对于#2,学习如何使用css设计表单样式。同时学习如何使用html标签标签。
有些人建议使用表格进行格式化,这不是最好的做法,应该避免。
一般来说,html应该只包含有关内容的信息,css负责内容的表示。这称为关注点分离。