Jump to content

Deleting Records..


angel_cowgirl

Recommended Posts

I think programming just has a curse against me or something. I seem to get the weird errors, being that for this one my line of code that I feel is sure is causing the problem is exactly like all the examples I've been able to find on the net...so this is very frustrating.

 

I have two pages for Deleting the first is the selection page, lists the records with checkboxes next to them for the user to select and then a Delete button that passes to the second page. My first page shows up just fine.

 

So here's a line from my first page just so ya can see the name i'm using for the checkboxes..

 

   echo "<input type='checkbox' name='id[".$i."] value='".$arrHorses['pk_horse_id']."'>";

 

The second page is suppose to get info for which records to delete from the first page and execute such a query. Then display the name of the records that were selected and say they were successfully deleted. Its connecting to the database just fine. But I'm getting a "not a valid result resource" error. arg, seems to be the error I always get.

 

Here's the important code for that page that I have thus far:

 

//store the Horse_ID passed from the previous page in a variable
$intDelete = $_POST['id'];

//Query to delete the record passed to it from the previous page
$strDeleteQuery = "DELETE FROM horses WHERE pk_horse_id = $intDelete";

//execute the query and save the result set in $rsHorse
$rsHorses = ConnectDatabase($strDeleteQuery);


for( $i = 0; $i < count($id); $i++)
{
  //get a row of data and store it in $arrHorses
  $arrHorses = mysql_fetch_array($rsHorses);
  //echo records that were deleted
  echo $id[$i];
  echo "<br>";
  //Give confirmation it was completed
  echo "<h3>Successfully deleted selected record(s).</h3>";
  echo "<br>";
}

 

Please help me figure out how to get it to delete the records.

I have a strong feeling this line:

$strDeleteQuery = "DELETE FROM horses WHERE pk_horse_id = $intDelete";

is the problem but that is exactly how all the examples I looked up were structured so I can't figure out why its causing problems.

 

Thanks!!!

Link to comment
Share on other sites

First this code is wrong:

$intDelete = $_POST['id'];

because you have this in your form:

name='id[".$i."] 

therefore you will have POST variables looking like:

$intDelete = $_POST['id4'];

or any other value of $i as digits after 'id'.

 

Second, you should check what does the function ConnectDatabase exactly. Probably this function does not execute a query.

Link to comment
Share on other sites

ConnectDatabase is my function (which is in a file im "requiring") to connect to the database. And it is connecting, know so because theres a line in it saying "Successfully connected to the datbase.

 

Ok the explaination makes sense of what $intDelete would = but I need it to be one line and not havta list all the numbers...I had tried making it

 

$intDelete = $_POST['id[".$i."]'];

 

but of course that didnt work..

 

.......

 

Link to comment
Share on other sites

You could loop over all your $_POST variables and use only those that have a name starting with 'id':

foreach ($_POST as $key => $value) {
    if (substr($key, 0, 2) == 'id') {
        $intDelete = $value;
        // do whatever you have to do, probably execute a DELETE statement
    }
}

Link to comment
Share on other sites

foreach($_POST['id'] as $k=>$v)
{
  //Query to delete the record passed to it from the previous page
  $strDeleteQuery = "DELETE FROM horses WHERE pk_horse_id = $v";

  //execute the query and save the result set in $rsHorse, a DELETE query returns true or false
  $rsHorses = ConnectDatabase($strDeleteQuery);

  // if successfully deleted, store the id in an array
  if($rsHorses) $deletedID[] = $v;
}

for( $i = 0; $i < count($id); $i++)
{
  //echo records that were deleted
  echo $deletedID[$i];
  echo "<br>";
}
//Give confirmation it was completed
echo "<h3>Successfully deleted selected record(s).</h3>";
echo "<br>";

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.