andrew_biggart Posted May 28, 2008 Share Posted May 28, 2008 can anyone please find a solition to this my assignment deadline is in 24 hours <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <? // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 28, 2008 Share Posted May 28, 2008 How are we supposed to know what the problem is if you don't tell us? Link to comment Share on other sites More sharing options...
craygo Posted May 28, 2008 Share Posted May 28, 2008 How about a little description of what your problem is?? Ray Link to comment Share on other sites More sharing options...
mushroom Posted May 28, 2008 Share Posted May 28, 2008 irish21 is trying to use php variables outside of php Link to comment Share on other sites More sharing options...
discomatt Posted May 28, 2008 Share Posted May 28, 2008 I think they wants us to do homework for them... Link to comment Share on other sites More sharing options...
jonsjava Posted May 28, 2008 Share Posted May 28, 2008 I'm feeling generous (I usually tell them to go study): <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_assoc($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <?php // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> Oh, and he wasn't using php outside of php. After revewing his code further, I notice he doesn't have variables set, so it still won't work. Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 28, 2008 Share Posted May 28, 2008 The code is dependent on register globals being on to "magically" populate the $delete and $checkbox variables. Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 lol sorry everyone just to clarify i have taken this staright from a tutorial website and changed the database connection to check it worked first so i havnt even written this code !!! blame the author lol can anyone please offer some advice as to what path to follow to do this and i will write my own code !!! POSSIBLY lol i know the delete variable is undefined and i have tried changing it to if $_POST('delete'); and things like that but ive been staring at php staright for about a week my head is wedged between my arse and i cant make heads or tails of anything anyone that can point me in the right direction it would much appreciated thanks in advance Link to comment Share on other sites More sharing options...
craygo Posted May 28, 2008 Share Posted May 28, 2008 Unless globals are on, you are going to get error with your code. what you need to do is check to see if the form hase been submitted first then run your code. also there is a better way to loop through your checkboxes. <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select databse. $conn = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // check to see if form has been submitted if(!isset($_POST['delete'])){ $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> </table> </form> </td> </tr> </table> <?php } else { $result = 1; // Check if delete button active, start this foreach($_POST['checkbox'] as $id){ $del_id = $id; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $res = mysql_query($sql); if(!$res){ // set result to 0 if failure $result = 0; } } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?> Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 aghhhhh this is still not workin :?( when i press delete it returns a blanc screen this is what im using <?php include("config.php"); if(!isset($_POST['delete'])){ $sql="SELECT * FROM biggart_members"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?><table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_assoc($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['user_id']; ?>"></td> <td bgcolor="#FFFFFF"><?php echo $rows['user_id']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <?php } else { $result = 1; // Check if delete button active, start this foreach($_POST['checkbox'] as $id){ $del_id = $id; $sql = "DELETE FROM biggart_members WHERE id='$del_id'"; $res = mysql_query($sql); if(!$res){ // set result to 0 if failure $result = 0; } } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?></table> </form> </td> </tr> </table> </div> Link to comment Share on other sites More sharing options...
craygo Posted May 28, 2008 Share Posted May 28, 2008 try some debugging. $res = mysql_query($sql) or die(mysql_error()); also I noticed you are using user_id to populate the checkbox but using id for the delete query. Which one is it??? Ray Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 Read the rules. We don't provide a free customisation service for code that isn't yours. We don't do homework. Link to comment Share on other sites More sharing options...
Recommended Posts