please can any1 tell me what is the problem with this delete code??

<?php
$con = mysql_connect("localhost","username","pswd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbname", $con);
$val=$_GET['teachers'];
mysql_query("DELETE FROM teacher WHERE title='$val'");
 
mysql_close($con);
?>

Recommended Answers

All 13 Replies

here teacher is the name of the combo box

It seems that there is not problem in the query. what error or message u are getting?

To actually delete the record you must pass whole title into $_GET.

When you echo

$val

what u get?

its displayin record deleted bt in the database its not deleting

Member Avatar for rajarajan2017

Test1: (echo the result of $val)

$val=$_GET['teachers'];
[B]echo $val;[/B]

Test2:

DELETE FROM teacher WHERE title='maths'

Check the query directly in your phpadmin I mean your sql editor. If record is removed then problem is in php. If not then in sql query.

test2 is workin...but not test1

Member Avatar for rajarajan2017

Post your code of where do you get the teachers from.

Your submit form should have the method="get" and should have the name "teacher" for your textbox or some element. and also you can check it in the url display have as ?teacher="radha" Post your code of html.

<html>
<body>
<table>

<tr><td>;

<font color="white">Select Teacher's Name</font></td>

<td> <font color="white"><select name="teachers" >
  <option value="1">abc </option>
  <option value="2">pqr </option>
  <option value="3">xyz </option>
 
</select></font>';
</td></tr>
<tr>
<td>


<form  method = "POST" action = "deleteteacher.php">
<p>
<input type ="submit" value="Delete"  /> 
<br />
</p>
</form>
</td>
</tr>
</table>
</body>
</html>

according to your code your final query will be like

delete from teacher where title ='1'

As you option value is 1,2,3.

Are you sure your title columns holds numerice values or you have any other id field.

Member Avatar for rajarajan2017

simple thing has to be changed:

<form  method = "POST" action = "deleteteacher.php">

instead

<form  method = "GET" action = "deleteteacher.php">

It will work.

Hey,

Do the change as said by Raja and one more thing to change.
Check out the html page.The form tag begins after the select tag.Place your form tag above your select element.

No please don't do that.
Just change: $val=$_GET['teachers']; to $val=$_POST['teachers']; Sending parts of a delete-query in the address-bar of your browser is not a good idea! What would happen if someone changed the _GET var in the address-bar to 1=1 ?? Your entire table-contents will be deleted.. Ever heard of sql-injection?

No please don't do that.
Just change: $val=$_GET['teachers']; to $val=$_POST['teachers']; Sending parts of a delete-query in the address-bar of your browser is not a good idea! What would happen if someone changed the _GET var in the address-bar to 1=1 ?? Your entire table-contents will be deleted.. Ever heard of sql-injection?

Ya... That is a gud move about $_post option...but do not forgot to place the form tag above the select element...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.