0

Hi I have a html form button called delete. Next to the button is an input area. The logged in user will enter a number corresponding to the ID then press delete. It should delete the row corresponding to that ID.

For some reason it doesn't work.

This is my html form:

  <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:

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.

thanks.

1
  • $idnum = $_POST['idnum']; try it.... PHP will not recognize id - you need form element name. Commented Mar 17, 2015 at 10:23

4 Answers 4

3

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 = intval($_POST['idnum']);  
$query = "delete from jobs_list where idnum = '".$idnum."'";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I was trying to prevent sql injection because the actual id is called id.
assuming you know not to use mysql_ functions and to look at pdo/mysqli those are the two issues that can be identified from the code you posted. i wrapped the input number in intval to make sure you are actually getting an integer from the input.
3

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:

$idnum = $_POST['idnum'];

You can also change the input name and don't change the php code:

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

It's a typical mistake, don't worry too much about that :P

Comments

1

You should change the POST value.. and then try

$idnum = $_POST['idnum']; 

Comments

1

Change the HTML, update the input type id to:

id="idnum"

then, update the PHP query while fetching the post value to:

$idnum = $_POST['idnum'];

It should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.