我想用MySQL和PHP从我的数据库中删除一个表行。我搜索了我的代码,我无法弄清楚我做错了什么。我认为我接近它,可能是一些我没有意识到的简单。
如果我将鼠标悬停在删除链接上,则会显示一个链接,显示要删除的行的正确ID号。但如果我点击它,它就无法正常工作。它只是刷新屏幕。
这是我的index.php代码:
<!-- Table -->
<form action="index.php" method="get" id="dispatch">
<div class="col-md-8 column">
<fieldset>
<legend>Incident Board (Incidents in red are active)</legend>
<div class="scroll-bar">
<table>
<thead>
<tr>
<th>Incident #</th>
<th>Town</th>
<th>Location</th>
<th>Incident Type</th>
<th>Time/Date</th>
<th>Admin</th>
<th>Delete Entry</th>
</tr>
</thead>
<tbody>
<?php
if( isset($_POST['town']) )
{
$town = $_POST['town'];
}
if( isset($_POST['location']) )
{
$location = $_POST['location'];
}
if( isset($_POST['incident_type']) )
{
$incident_type= $_POST['incident_type'];
}
if( isset($_POST['time_date']) )
{
$time_date= $_POST['time_date'];
}
if( isset($_POST['admin']) )
{
$admin = $_POST['admin'];
}
if( isset($_POST['id']) )
{
$id = $_POST['id'];
}
$db = mysqli_connect('localhost','root','') or die("Database error");
mysqli_select_db($db, 'cad');
$result= mysqli_query($db, "SELECT * FROM `cad` ORDER BY `time_date` DESC LIMIT 20");
while($row = mysqli_fetch_array($result))
{
$town = $row['town'];
$location = $row['location'];
$incident_type = $row['incident_type'];
$time_date = $row['time_date'];
$admin = $row['admin'];
$id = $row['id'];
echo "
<tr>
<td class=\"id-center\">
".$id."
</td>
<td >
".$town."
</td>
<td>
".$location."
</td>
<td >
".$incident_type."
</td>
<td >
".$time_date."
</td>
<td >
".$admin."
</td>
<td>
<a href=\"delete.php?id=$id\" name=\"delete\" value=\"$id\" class=\"btn btn-primary btn-default center-1\"><span class=\"glyphicon glyphicon-trash\"></span></a>
</td>
</tr>";
}
mysqli_close($db);
?>
</tbody>
</table>
</div>
</fieldset>
</div>
</form>
<!-- End -->
这是我的delete.php代码:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("cad", $dbhandle);
mysql_query("DELETE FROM cad WHERE id = ".$_GET['id']."");
header('location: index.php');
?>
答案 0 :(得分:1)
不推荐使用mysql,而不是使用mysqli
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$con=mysqli_connect($hostname, $username, $password,"cad");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"DELETE FROM cad WHERE id = ".$_GET['id']."");
mysqli_close($con);
?>