Jump to content


Photo

Deleting Records..


  • Please log in to reply
5 replies to this topic

#1 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 10 December 2005 - 12:10 PM

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

#2 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 10 December 2005 - 12:58 PM

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.

#3 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 10 December 2005 - 01:28 PM

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

.......


#4 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 10 December 2005 - 01:50 PM

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


#5 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 10 December 2005 - 11:49 PM

Get the same "not valid result resource" error.

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 12 December 2005 - 05:38 AM

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>";

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users