2 Replies - 311 Views - Last Post: 13 January 2013 - 09:14 PM Rate Topic: -----

#1 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

PHP delete problem

Posted 13 January 2013 - 09:01 PM

Hi, I have a problem with deleting an entry from mysql table. whenever I click the delete button it deletes the last entry from my table and not the one that I've chosen.

here is the codes
echo "<font face='Arial' size='1'>";


	$conn=mysql_connect("localhost","root","");
	if(!$conn)
	{
	die('could not connect!' . mysql_error());
	}

	mysql_select_db("people", $conn);

	
	$result=mysql_query("select * from usersinfo");
	
		echo "<table border='1' align='center'>
		<tr>
		<th> </th>
		<th> username</th>
		<th> password </th>
		<th> action </th>

		</tr>";
	while ($row=mysql_fetch_array($result))
		{
		echo "<tr>";
		echo "<td>";
		echo " <input type='checkbox' value='check'> ";
		echo "</td>";
		echo "<td>" . $row['USERNAME'] . "</td>";
		echo "<td>" . $row['LASTNAME'] . "</td>";
		echo "<td>" . $row['FIRSTNAME'] . "</td>";
		echo "<td>" . $row['MI'] . "</td>";
		echo "<td>" . $row['COMPANY'] . "</td>";
		echo "<td>" . $row['POSITION'] . "</td>";
		echo "<td>" . $row['DEPARTMENT'] . "</td>";
		echo "<td>";
		echo "<form method='post' action='ADD.php'>";
		echo "<input type='hidden' name='ACE_id' value=". $row['ID'] .">";
		echo "<input type='submit'>";
		echo "</td>";
		echo "</tr>";
		}
		

		echo "</table>";
	mysql_close($conn);

echo "</td>"; ]


and the delete code

<?php
function del_userinfo( ) {

$DELid=$_POST["ACE_id"];


$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("people", $con);

mysql_query("DELETE FROM usersinfo WHERE ID='" . $DELid . "'");

mysql_close($con);


}
?>


help anyone .., Thank You in advance :)/>

This post has been edited by macosxnerd101: 13 January 2013 - 09:02 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: PHP delete problem

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 801
  • View blog
  • Posts: 1,700
  • Joined: 30-January 09

Re: PHP delete problem

Posted 13 January 2013 - 09:12 PM

The problem is with your HTML form, not server-side or DB. Your submit button is gathering the data from the form and submitting that. There is one input element with name "ACE_id" for each row that you display. So when it comes to determining the data to submit, it sequentially looks through the form, setting "ACE_id" from the first row, then clobbering it with the second, clobbering it with the third, and so on, all the way down to the last row in your HTML table.

You need to determine which row has the checkbox checked, and then retrieve just that value to pass over to PHP.
Was This Post Helpful? 2
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: PHP delete problem

Posted 13 January 2013 - 09:14 PM

Some issues I see:
  • $x = $_POST['x']; is always bad.
  • mysql* functions are outdated and insecure. Use PDO prepared statements.
  • Mixing languages is not advisable for beginners.

Lastly, you need to learn the difference between client and server side code. PHP is run on the server, before the HTML is processed.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1