如何从数据库中搜索某个ID?然后,如果它不存在,它应该告诉我找不到id,如果它存在,它应该显示我,例如“id已被成功删除”。
这是我的代码: 这是我用来删除项目的代码,它正在删除但是当我输入一个不存在的ID时,根本没有任何内容:
<!-- THIS IS MY DATABASE CONNECTOR/I SAVED IT AS (dbconn.php) -->
<?php
$hostname="localhost";
$username="root";
$password="";
$conn=mysql_connect($hostname,$username,$password);
if(!conn){
die("There is no connection to the mysql server".mysql_error());
}
$mysql_db=mysql_select_db("djwriters", $conn);
if(!$mysql_db){
die("There is no database onnection". mysql_error());
}
?>
<!-- THIS IS WHERE IS INPUT A TEXTBOX AND A DELETE BUTTON.
I saved it as (tash.php) -->
<body>
<table width="840">
<tr>
<td>
<form action="reason.php" method="post" name="frmreason">
<label>ID:</label><input name="ID" type="text" />
<input type="submit" name="delete" id="Delete" value="Delete" />
</form>
</td>
</tr>
</table>
</body>
<!-- THIS IS NOW THE CODE THAT I USED TO DELETE THE ROW.
I saved it as (delete.php) -->
<?php
include("dbconn.php");
if(isset($_POST['delete'])and $_POST['ID']){
$sql="DELETE FROM `djwriters`.`personal` WHERE `personal`.`ID` ='$_POST[ID]'";
mysql_query($sql) or die("The id was not found".mysql_error());
}
?>
有人会帮助我吗?当您单击删除按钮时,代码应该从数据库中搜索项目,如果enter code here
不存在,它应该显示我或显示错误,如果有enter code here
应该告诉我删除成功。在PHP中使用isset
函数
答案 0 :(得分:1)
如果您想通过其ID获取一行,请使用 SELECT :
http://dev.mysql.com/doc/refman/5.1/en/select.html
SELECT `field`, `field2`, `...` FROM `my_table` WHERE id = $id
如果您要按ID删除行,请使用 DELETE :
http://dev.mysql.com/doc/refman/5.1/en/delete.html
DELETE FROM `my_table` WHERE id = $id
要检查受影响的行数,请使用PHP函数 mysql_affected_rows():
http://php.net/manual/en/function.mysql-affected-rows.php
<强> Offtopic 强>: 我建议使用PDO:http://php.net/manual/en/book.pdo.php
那应该对你有所帮助。 : - )