简体   繁体   English

按钮调用PHP以通过ID删除MySQL行

[英]button to call php to delete mysql row via ID

Hi I have a html form button called delete. 嗨,我有一个HTML表单按钮,称为删除。 Next to the button is an input area. 该按钮旁边是输入区域。 The logged in user will enter a number corresponding to the ID then press delete. 登录的用户将输入与ID对应的数字,然后按Delete键。 It should delete the row corresponding to that ID. 它应该删除与该ID对应的行。

For some reason it doesn't work. 由于某种原因,它不起作用。

This is my html form: 这是我的html形式:

  <h3 class="block-head">Delete Job</h3>
<form action="deletejob.php" method="post" class="basic-grey">
<label>Please Enter Job ID</label><input type="text" name="idnum" id="id"/><br>

     <input type="submit" name="delete" value="delete"/>
</form>

This is my php code: 这是我的PHP代码:

mysql_connect("********", "root", "******")
 or die("Connection Failed");
 mysql_select_db("jobslist")or die("Connection Failed");
 $idnum = $_POST['id'];
 $query = "delete from jobs_list where idnum = '".$id."'";
 if(mysql_query($query)){ echo "deleted";}
 else{ echo "fail";}

I know about switching to mysqli and that...I just want to see if this method works first. 我知道有关切换到mysqli的知识,我只是想看看这种方法是否先行。

thanks. 谢谢。

the name of your input is idnum yet you are looking in the $_POST array for id and in your query using the variable $id when you declare $idnum in the line previous. 输入的名称是idnum但是在上一行中声明$idnum时,您正在$_POST数组中查找id并在查询中使用变量$id

$idnum = intval($_POST['idnum']);  
$query = "delete from jobs_list where idnum = '".$idnum."'";

PHP receive the data via $_POST as an array of input elements with the key as the NAME of the input field, so you have to change this line: PHP通过$ _POST作为输入元素的数组接收数据,其键为输入字段的NAME,因此您必须更改以下行:

$idnum = $_POST['idnum'];

You can also change the input name and don't change the php code: 您还可以更改输入名称,而不更改php代码:

<input type="text" name="id" id="id"/>

It's a typical mistake, don't worry too much about that :P 这是一个典型的错误,不必太担心:P

您应该更改POST值。然后尝试

$idnum = $_POST['idnum']; 

Change the HTML, update the input type id to: 更改HTML,将输入类型ID更新为:

id="idnum"

then, update the PHP query while fetching the post value to: 然后,在获取以下值的同时更新PHP查询:

$idnum = $_POST['idnum'];

It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM