Jump to content

[SOLVED] Passing info from forms for deletion


Esqulax

Recommended Posts

Hiya, if anyone can help, id be really appreciative...

 

Basically, im trying to get info from the checkboxes on delete.php, pass it to del.php and run a delete script.

It wasnt working, so in del.php i put in a count() to see if any info is actually being passed into it.. i get a big fat 0

 

This is so vexing, its been annoying me for days

 

Code:

 

delete.php:

<html>
<head>
<title>DELETED!</title>
</head>

<?php
//Standard connection scripts  
$conn=@mysql_connect("localhost","root","")
or die("error connecting to database");

$rs=@mysql_select_db("hotel", $conn)
or die("Error connecting to Hotel");

  
  $sql       = "SELECT * FROM guest_data ORDER BY guest_id, name, surname";
  $result    = mysql_query($sql,$conn) or die("error using result query");

echo ("<table border= 1>");

echo ('<form method="post" action="'.$self.'">');

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC))   
  {
    echo "<tr>\n";
echo "<td>". $row['guest_id'] . '<input name="guestid[]" type="checkbox" value="' . $row['guest_id'] . '"></td>';						
    echo "<td>" . $row['name'] ;
    echo "<td>" . $row['surname'];
  }



?>
    <tr>
      <td>
     
      <input type="submit" name="submit" value="submit"></td>
    </form>
    </tr>
</table>

<?php
  
  echo('<form method="post" action="del.php">');
	$delid = $_POST["guestid"];
	$how_many=count($delid);


        if ($how_many>0) 
	{
            echo ("You chose the following records:<br>");
        
        	for ($i=0; $i<$how_many; $i++) 
		{
                echo (($i+1) . " - " . $delid[$i] . "<br>");
        	}

		echo("Ok to delete ".$how_many." record(s)?");
		echo('<input type="submit" name="submit" value="Yes">');
		echo('</form>');
	}
?>

 

del.php:

<html>
<head>
<title>Sucessfully Deleted</title>
</head>
<?php
$delvalue=$_POST['guestid'];

	//Stanatrd connection scripts  
$conn=@mysql_connect("localhost","root","")
or die("error connecting to database");

$rs=@mysql_select_db("hotel", $conn)
or die("Error connecting to Hotel");



$amount=count($delvalue);
echo(''.$amount);

for($i=0;$i<$amount; $i++)
{
echo(''.$delvalue[$i]);
}



/*	 while ($row = mysql_fetch_array($delval))  
{
$sql2="DELETE FROM guest_data WHERE guest_id=".$delval;
$gone= mysql_query($sql2,$conn);
}
*/

?><!--
<script language="javascript"><!--
location.replace(delete.php")
//-->
</script>-->
<a href="delete.php">Return</a>
<body>
</body>
</html>

 

Link to comment
Share on other sites

Fantastic! So the form diddn't have any info to store, i see.

 

one last syntax Q

 

Is it ok to put the variable on the end of a SELECT or DELETE statement like this?

        $sql= "SELECT * FROM guest_data WHERE guest_id=".$delvalue;
  	$result    = mysql_query($sql,$conn) or die("error using result query");

 while ($row = mysql_fetch_array($result))  
{
$sql2="DELETE FROM guest_data WHERE guest_id=".$delvalue;
$gone= mysql_query($sql2,$conn);
}
[/code

Link to comment
Share on other sites

yes that fine..

 

Techie Tip:

you have an array of ID's

ie

$_POST['guestid'];

array(1,2,3,6,8,10)
etc

 

you can do this

<?php
$delvalue=implode(",",$_POST['guestid']);
//returns "1,2,3,6,8,10"

$sql2="DELETE FROM guest_data WHERE guest_id IN ($delvalue)";
//SQL statment to deletes all the records in one hit 
?>

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.