Jump to content

[SOLVED] Deleting Rows from mysql


jaybeeb

Recommended Posts

I have a table which displays a number of records, beside each row is a delete icon, which recognises the id (User_ID) and opens a new php file when I click it, which is meant to delete the row, but it just goes straight to my error.

 

<?php
mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");


if ($_POST['delete'])

{

$id = $_POST['delete'];

mysql_query("DELETE FROM itsupport WHERE id='$User_ID'") or die(mysql_error());

echo "The Row Number $User_ID has been Successfully Removed";

  
    }
else 

{ 

mysql_query($query) or die ('Delete Query Failed. Error '. mysql_error(). ' with query '. $query);

}

?>

 

Novice at PHP so could be some silly mistakes.

 

Link to comment
Share on other sites

what errors do u get

No PHP error, just "Delete Query Failed. Error Query was empty with query"

 

You aren't referencing the right variable.

 

Should be:

$id = $_POST['delete'];
mysql_query("DELETE FROM itsupport WHERE id='$id'") or die(mysql_error());

 

My variable is "User_ID" should I leave this the way I had it and change

$id = $_POST['delete'];

to

$User_ID = $_POST['delete'];

 

It still doesnt work either way though?

Link to comment
Share on other sites

You said that there's a delete link at the end of the table, right?  So that would mean that you're using GET and appending the user id to the end of the link.  Use $_GET['id'] instead. 

 

Also, it would help if you'd tell us which error you're getting.

Link to comment
Share on other sites

You said that there's a delete link at the end of the table, right?  So that would mean that you're using GET and appending the user id to the end of the link.  Use $_GET['id'] instead. 

 

Also, it would help if you'd tell us which error you're getting.

 

I dont get a php error, I just get this "Delete Query Failed. Error Query was empty with query"

 

I have changed around what you said, but still get the same error. I also changed one or two other things. Here is my current code:

<?php

mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");

if ($_GET['User_ID'])

{

$User_ID = $_GET['User_ID'];
mysql_query("DELETE FROM itsupport WHERE User_ID='$User_ID'") or die(mysql_error());

echo "The Row Number $User_ID has been Successfully Removed";

}
else 
{ 

mysql_query($query) or die ('Delete Query Failed. Error '. mysql_error(). ' with query '. $query);
}

?>

 

Thanks,

Link to comment
Share on other sites

If your form is passing the delete value as User_ID using the POST method , then you should be using:

$id = $_POST['User_ID'];

 

You only need to worry that the value is passed correctly from the user form to your php page.  $id can be anything you want as long as it is consistent on your php page.

Link to comment
Share on other sites

I dont get a php error, I just get this "Delete Query Failed. Error Query was empty with query"

 

Exactly!! Your error message is telling you that the variable $query has no value. A quick look at your code shows that the variable $query is never defined.

Link to comment
Share on other sites

I dont get a php error, I just get this "Delete Query Failed. Error Query was empty with query"

 

Exactly!! Your error message is telling you that the variable $query has no value. A quick look at your code shows that the variable $query is never defined.

 

Sorry for the noob question but how should I define it?

Link to comment
Share on other sites

mysql_query("DELETE FROM itsupport WHERE User_ID='$User_ID'") or die(mysql_error());
echo "The Row Number $User_ID has been Successfully Removed";
}
else 
{ 
mysql_query($query) or die ('Delete Query Failed. Error '. mysql_error(). ' with query '. $query);
}

 

The query string isn't defined for the second query there.  Try this version:

 

$query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
mysql_query(query) or die(mysql_error());
echo "The Row Number $User_ID has been Successfully Removed";
}
else 
{ 
mysql_query($query) or die ('Delete Query Failed. Error '. mysql_error(). ' with query '. $query); //$query now has a value
}

Link to comment
Share on other sites

Getting an error now

Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\delete1.php on line 22

 

<?php


$User_ID = $_GET['id'];

if (!($conn = mysql_connect('localhost', 'root', '')))
{
	showError();
}

if (!(mysql_select_db('itsupport', $conn)))
{
	showError();
}
{


$query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
mysql_query(query) or die(mysql_error());
echo "The Row Number $User_ID has been Successfully Removed";
}
else    //Line 22
{ 
mysql_query($query) or die ('Delete Query Failed. Error '. mysql_error(). ' with query '. $query); //$query now has a value
}

?>

Link to comment
Share on other sites

Sorry, used the wrong bit of code, when I replaced the bit that andy told me.

Here is my code now.

 

But it still just says "Delete Query Failed. Error Query was empty with query"

 

<?php

mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");

if ($_POST['User_ID'])

{

$User_ID = $_POST['User_ID'];

$query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
mysql_query(query) or die(mysql_error());
echo "The Row Number $User_ID has been Successfully Removed";
}
else 
{ 
mysql_query($query) or die ('Delete Query Failed. Error '. mysql_error(). ' with query '. $query); //$query now has a value
}

?>

Link to comment
Share on other sites

Sorry, but all you need is this:

 

<?php
mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");
if ($_POST['User_ID']) {
    $User_ID = $_POST['User_ID'];
    $query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
    $result = mysql_query(query) or die('Delete Query Failed. Error '. mysql_error(). ' with query '. $query);
    echo "The Row Number $User_ID has been Successfully Removed";
}
?>

Link to comment
Share on other sites

can u show the form ur using to post userid ...

Hi, I am using a table which shows my database with an update and a delete column on it. It updates perfectly.

 

<?php
while ($row =mysql_fetch_array($result))
{

	echo "<tr>";
	echo "<td>" . $row[user_ID] . "</td>
	      <td>" . $row[Problem] . " </td>
		  <td>" . $row[Problem_ID] . "</td>
	      <td>" . $row[user_email] . "</td>
		  <td>" . $row[Office_Number] . " </td>
		  <td>" . $row[Phone_Number] . " </td>
		  <td>" . $row[user_Name] . " </td>";
		  			   
	echo "<td><a href=\"selectedit.php?id=$row[user_ID]\"><img border=\"0\" src=\"images/edit.gif\"></a>";
       echo "<td><a href=\"delete1.php?id=$row[user_ID]\"><img border=\"0\" src=\"images/delete.gif\"></a>";
	echo "</tr>";





}

mysql_close($conn);
?>

Link to comment
Share on other sites

SINCE UR PASSING USERID THROUGH LINK U CANT POST USERID U CAN ONLY GET USERID ...TRY THIS....


<?php
mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");
if ($_GET['User_ID']) {
   $User_ID = $_GET['User_ID'];
   $query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
   mysql_query(query) or die(mysql_error());
echo "The Row Number $User_ID has been Successfully Removed";
}
?>

Link to comment
Share on other sites

I have noticed that the reason why $query is not being used in the mysql_query statement is because you missed out the php $ variable assignment so it looks like mysql_query(query) not mysql_query($query)

<?php
mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");
if ($_POST['User_ID']) {
    $User_ID = $_POST['User_ID'];
    $query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
    $result = mysql_query( //HERE--->  $query) or die('Delete Query Failed. Error '. mysql_error(). ' with query '. $query);
    echo "The Row Number $User_ID has been Successfully Removed";
}
?>

Link to comment
Share on other sites

Show us the whole code as it stands now.

 

Ok, It doesnt show any errors just a blank screen , but it still doesnt delete.

Here is my Delete code

<?php
mysql_connect("localhost", "root", "") or die ("Could not connect");
mysql_select_db("itsupport") or die ("Could not connect to DB");
if ($_GET['User_ID']) {
    $User_ID = $_GET['User_ID'];
    $query = "DELETE FROM itsupport WHERE User_ID='$User_ID'"; // define the query string
    $result = mysql_query($query) or die('Delete Query Failed. Error '. mysql_error(). ' with query '. $query);
    echo "The Row Number $User_ID has been Successfully Removed";
}
?>

Here is the code for my table (works perfectly)

<table border="0" align="left">

<?php
require_once 'library/db.php';



if (!($conn = mysql_connect('localhost', 'root', '')))
{
	showError();
}


if (!(mysql_select_db('itsupport', $conn)))
{
	showError();
}



if (!($result = mysql_query('select * from itsupport', $conn)))
{
	showError();
}

	if(!isset($cmd))

echo "<tr>
<td>User ID</td>
<td>Problem</td>
<td>Problem ID</td>
<td>User Email</td>
<td>Office Number</td>
<td>Phone Number</td>
<td>User Name</td>
</tr>";


while ($row =mysql_fetch_array($result))
{

	echo "<tr>";
	echo "<td>" . $row[user_ID] . "</td>
	      <td>" . $row[Problem] . " </td>
		  <td>" . $row[Problem_ID] . "</td>
	      <td>" . $row[user_email] . "</td>
		  <td>" . $row[Office_Number] . " </td>
		  <td>" . $row[Phone_Number] . " </td>
		  <td>" . $row[user_Name] . " </td>";
		  			   
	echo "<td><a href=\"selectedit.php?id=$row[user_ID]\"><img border=\"0\" src=\"images/edit.gif\"></a>";
       echo "<td><a href=\"delete1.php?id=$row[user_ID]\"><img border=\"0\" src=\"images/delete.gif\"></a>";
	echo "</tr>";





}

mysql_close($conn);
?>
</table>


</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.