【发布时间】:2016-03-27 09:17:29
【问题描述】:
我需要一点帮助。我试图找出如何将特定查询(删除记录)与记录的 id 相关联,而不是与另一个查询(记录的选择)回显的记录相关联。
这行代码在指定 id 时完全有效,但我再次需要它用于被调用的记录,如果我删除记录,id 可以跳过数字。
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
我的 phpmyadmin 数据库中有一个表,其中包含“id”、“pagetitle”、“toevoeging”(荷兰语中的添加)、“message”列。第一个是 INT,其余的是 varchars/text。
这可能是一个愚蠢的问题,我很抱歉。我对 PHP 和一般编程仍然很陌生。
这里是代码。我已经评论了行代码以澄清。谢谢!。
<?php
if (isset($_SESSION['email'])) //if the admin is active, forms can be written out.
{
echo '</nav>
<br><br> <div class="inlogscript">
<form action="verstuurd.php" method="post">
<input type="text" placeholder="Titel" method="POST" name="pagetitle" /><br><br>
<input type="text" placeholder="Toevoeging" method="POST" name="toevoeging" /><br><br>
<textarea class="pure-input-1-2" placeholder="Wat is er nieuws?" name="message"></textarea><br>
<input type="submit" value="Bevestigen" />
</form></div>';
}
?>
<div class="mainContent">
<?php
include_once("config.php"); //this is the database connection
$query = "SELECT * FROM paginas "; //selects from the table called paginas
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
$pagetitle = $row['pagetitle'];
$toevoeging = $row['toevoeging'];
$message = $row['message'];
echo '<article class="topcontent">' . '<div class="mct">' . '<h2>' . "$pagetitle" .'</h2>' . '</div>' . "<br>" .
'<p class="post-info">'. "$toevoeging" . '</p>' . '<p class="post-text">' . '<br>'. "$message" . '</p>' .'</article>' . '<div class="deleteknop">' . '<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>' . '</div>' ;
} //This long echo will call variables $pagetitle, $toevoeging and &message along with divs so they automatically CSS styled,
//along with a Delete button per echo that has the 3 variables
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked
{
$resulttwo = $mysqli->query($querytwo);
}
?>
</div>
</div>
这里还有记录的插入 INTO 查询。再次感谢!
$sql = "INSERT INTO paginas (pagetitle,toevoeging, message)
VALUES ('$_POST[pagetitle]','$_POST[toevoeging]','$_POST[message]')";
//the insertion into the table of the database
if ($MySQLi_CON->query($sql) === TRUE) {
echo "";
} else {
echo "Error: ". $sql . "" . $MySQLi_CON->error;
}
【问题讨论】:
-
又应该与什么相关联?
-
需要编写此代码,以便知道选择了哪个查询。 "从
paginas中删除 id = 5";喜欢:“从 'paginas' 中删除 id = $idofthepost”;但是 $idofthepost 不起作用。我需要用它做点别的事情,但我就是不知道是什么。