-1

如何从数据库中搜索某个 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应该告诉我已成功删除。isset在 PHP 中使用函数

4

1 回答 1

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

题外话:我推荐使用 PDO: http: //php.net/manual/en/book.pdo.php

那应该对你有帮助。:-)

于 2013-06-15T18:32:59.363 回答