Problem removing rows out of database!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Problem removing rows out of database!

Post by Skywalker »

This should work I thoughed, but in stead of that, it lets my browser crach en doesn't send any error messages!

Code: Select all

<?php


                       

                            $aantal_rijen = $_POST['rows']; // How many rows in database
                                while ($i < $aantal_rijen){ 
                                    if ($_POST['id' . $i] == "on"){ // check every checkbox that has the value on

                                    $Host = "localhost"; 
                                    $Gebruiker = "Test"; 
                                    $Wachtwoord = "Test"; 
                                    $DBNaam = "Test"; 
                                     
                                    $Verbinding = mysql_connect ($Host, $Gebruiker, $Wachtwoord); 
                                    $Opdracht = "DELETE * FROM info WHERE id=='$i'"; 
                                        if ( mysql_db_query ($DBNaam, $Opdracht, $Verbinding)){ 
                                            echo "Ok $i"; 
                                        } else { 
                                            echo "Mislukt";} 
                                        mysql_close ($Verbinding); 
                                     
                                        //mysql_select_db($database_connect, $connect); 
                                        //$sql = "DELETE * FROM info WHERE id='" . $i . "'"; 
                                        //$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error()); 
                                    } 
                                } 
                             




?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you haven't incremented $i anywhere

you need to add this

Code: Select all

$i++;
Browser crash is due to a never ending while loop.

Mark
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

ahhh yes, the simple yet fustrating infinity loop. That's why I rather use for() rather than while() most of the time for an SQL query.

-Nay
tumbleweed
Forum Newbie
Posts: 3
Joined: Wed Nov 19, 2003 10:02 am

Post by tumbleweed »

<?php




$aantal_rijen = $_POST['rows']; // How many rows in database
while ($i < $aantal_rijen){
if ($_POST['id' . $i] == "on"){ // check every checkbox that has the value on

$Host = "localhost";
$Gebruiker = "Test";
$Wachtwoord = "Test";
$DBNaam = "Test";

$Verbinding = mysql_connect ($Host, $Gebruiker, $Wachtwoord);

mysql_select_db($DBNaam,$Verbinding); //try this as this is the only thing missing from the way i conncet to mysql databases. I take it that $DBNaam is the database name if not change it to the one that is.

$Opdracht = "DELETE * FROM info WHERE id=='$i'";
if ( mysql_db_query ($DBNaam, $Opdracht, $Verbinding)){
echo "Ok $i";
} else {
echo "Mislukt";}
mysql_close ($Verbinding);

//mysql_select_db($database_connect, $connect);
//$sql = "DELETE * FROM info WHERE id='" . $i . "'";
//$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
}
}





?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i don't understand this line

Code: Select all

if ($_POST['id' . $i] == "on"){
Surely that is incorrect!?

Mark
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

$Opdracht = "DELETE * FROM info WHERE id=='$i'";

I've never used equality for sql where statements.

Shouldn't it read as:

$Opdracht = "DELETE * FROM info WHERE id='$i'";
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

try this

Code: Select all

<?php
	$aantal_rijen = $_POST['rows']; // How many rows in database 
	for($i=0;$i < $aantal_rijen; $i++)
	{
		if ($_POST['id' . $i] == "on"){ // check every checkbox that has the value on 
		{
			$Host = "localhost"; 
			$Gebruiker = "Test"; 
			$Wachtwoord = "Test"; 
			$DBNaam = "Test"; 
			mysql_select_db($DBNaam);
			$Verbinding = mysql_connect ($Host, $Gebruiker, $Wachtwoord); 
			$Opdracht = "DELETE * FROM info WHERE id=='".$i."'"; 
			$Result = mysql_query($Opdracht) or die('Mislukt : '.mysql_error());
			if (!$Result)
			{
				mysql_close ();
			}
			echo "Ok ".$i.""; 
		}
	}
?>

Note, this is untested but should work without any problems :P
Post Reply