gevo12321 Posted July 12, 2007 Share Posted July 12, 2007 i have a script here for a form. <?php require_once('../connect.php'); $query = mysql_query("SELECT * FROM layout") or die(mysql_error()); echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <title>E Layout</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"> </head> <body> <a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a> <hr> <form action=\"esubmiteditlayout.php\" method=\"post\"> <table width=\"100%\" border=\"0\">"; while($row=mysql_fetch_object($query)) { $id=$row->id; $name=$row->name; $source=$row->source; echo " <tr> <td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox"; echo $id; echo "\"></td> <td valign=\"top\" width=\"100\">Button Name:</td> <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"button[]\" value=\""; echo $name; echo "\"/></td> <td valign=\"top\" width=\"150\">Button Source:</td> <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"buttonsrc[]\" value=\""; echo $source; echo "\"/> <td valign=\"top\" align=\"left\">ID:<input type=\"text\" readonly size=\"3\" name=\"id[]\" value=\""; echo $id; echo "\"></td> </tr>"; } echo " </table> <input type=\"submit\" name=\"edit\" value=\"Edit\"> <input type=\"submit\" name=\"insert\" value=\"Insert\"> <input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\"> <script language=\"javascript\"> function deldel() { var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\"); if(agree) return true; else return false; } </script> </form> </body> </html>"; mysql_close($link); ?> the form has checkboxes for each row now how can i make a submit script so that pressing the delete button deletes the row that is checked? Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted July 12, 2007 Share Posted July 12, 2007 Do something like this: <td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox" .$id. "\" value=\"1\" /></td> then on your processor: $query = mysql_query("SELECT * FROM layout") or die(mysql_error()); while($row=mysql_fetch_object($query)) { $id=$row->id; if($_POST['checkbox'.$id] == 1){ //Delete $id from database } } Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 If you name your checkboxes using an array... eg; echo "<td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox[]\" value=\"$id\" /></td> A simple.... <?php $sql = "DELETE FROM tbl WHERE id IN(" implode(',',$_POST['checkbox']) . ")"; ?> In your process page should suffice. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 12, 2007 Author Share Posted July 12, 2007 ok when i do what cmgmyr suggested, i just get a blank page with no error but it doesnt work when i do what thorpe suggested, i get a Parse error: parse error, unexpected T_STRING in the following line: $sql = "DELETE FROM layout WHERE id IN(" implode(',',$_POST['checkbox']) . ")"; can someone help plz thx Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 12, 2007 Share Posted July 12, 2007 Thorpe's code is just missing a fullstop - the concatenation operator. Try: <?php $sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")"; ?> Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 12, 2007 Author Share Posted July 12, 2007 ok now it acts the same way as cmgmyr's does. it just doesn't give any errors nor does it delete. isn't there supposed to be an id='something' for the code to work or am i wrong plz help Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 Did you change your form as I suggested? can you post your current code? isn't there supposed to be an id='something' for the code to work There is, just slightly different syntax that enables a group of records to be deleted in one query. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 12, 2007 Share Posted July 12, 2007 To answer, the question, no, there does not need to be an id='something' statement. We are using a list of possible values, and the IN clause. It is effectively like writing id='something' or id='somethingelse' or id='somethingmore' etc, but much easier to work with. Whenever you have a query that's not working try some debugging: <?php $sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")"; mysql_query($sql) or die(mysql_error().'<br />query'.$sql); echo '<br />.$sql.'<br />'; ?> Kind of a double check here, since we dont know if the query is failing or not as yet. If it does, we will get the mysql error and the contents of your query, if it doesn't we will just get exactly whats being passed into the query. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 12, 2007 Author Share Posted July 12, 2007 this is the form code <?php require_once('../connect.php'); $query = mysql_query("SELECT * FROM layout") or die(mysql_error()); echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <title>E Layout</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"> </head> <body> <a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a> <hr> <form action=\"esubmiteditlayout.php\" method=\"post\"> <table width=\"100%\" border=\"0\">"; while($row=mysql_fetch_object($query)) { $id=$row->id; $name=$row->name; $source=$row->source; echo " <tr> <td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox[]\" value=\""; echo $id; echo "\"></td> <td valign=\"top\" width=\"100\">Button Name:</td> <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"button[]\" value=\""; echo $name; echo "\"/></td> <td valign=\"top\" width=\"150\">Button Source:</td> <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"buttonsrc[]\" value=\""; echo $source; echo "\"/> <td valign=\"top\" align=\"left\">ID:<input type=\"text\" readonly size=\"3\" name=\"id[]\" value=\""; echo $id; echo "\"></td> </tr>"; } echo " </table> <input type=\"submit\" name=\"edit\" value=\"Edit\"> <input type=\"submit\" name=\"insert\" value=\"Insert\"> <input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\"> <script language=\"javascript\"> function deldel() { var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\"); if(agree) return true; else return false; } </script> </form> </body> </html>"; mysql_close($link); ?> and this is the submit code <?php require_once('../connect.php'); $edit=$_POST[edit]; $insert=$_POST[insert]; $delete=$_POST[del]; if (isset($delete)) { $sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")"; } else if (isset($insert)) { mysql_query("INSERT INTO layout (id, name, source) VALUES ('', '', '')"); echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <meta http-equiv=\"Refresh\" content=\"1; url=eeditlayout.php\"/> </head> <body> New Button has been Inserted.<br> Thank You </body> </html> "; } else if (isset($edit)) { $i = 0; $info = array(); foreach ($_POST['button'] as $namekey => $namevalue) { $info[$i++]['name'] = $namevalue; } $i=0; foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue) { $info[$i++]['source'] = $sourcevalue; } $i=0; foreach ($_POST['id'] as $idkey => $idvalue) { $info[$i++]['id'] = $idvalue; } for($i =0;$i<count($info);$i++) { mysql_query("UPDATE layout SET name='".$info[$i]['name']."', source='".$info[$i]['source']."' WHERE id='".$info[$i]['id']."'"); } echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <meta http-equiv=\"Refresh\" content=\"3; url=eeditlayout.php\"/> </head> <body> The layout has been updated.<br> You will be redirected back to the E Layour page in 3 seconds.<br> Thank You </body> </html> "; } mysql_close($link) ?> thx Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 All these... if (isset($delete)) statements will evaluate to true because you set these variables at the top of your page. Not the problem, but something else that will cause issues. As for your problem. You never execute the query. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 12, 2007 Share Posted July 12, 2007 All these... if (isset($delete)) statements will evaluate to true because you set these variables at the top of your page. Not the problem, but something else that will cause issues. Thats not actually true is it? Tested with: <?php $submit = $_POST['submit']; if(isset($submit)){ echo 'form has been submitted'; }else{ echo 'form has not been submited'; } ?> <form action="phpfreaks.php" method="post"> <input type="submit" name="submit" /> </form> The first time you load the page, you get the next 'form has not been submitted'. Once you submit, it displays 'form has been submitted'. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 12, 2007 Author Share Posted July 12, 2007 i thought just typing it executes it so how would i execute it? oh and i know that if (isset($delete)) is not causing the problem because the edit ans insert buttons work and they work the same way Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted July 12, 2007 Share Posted July 12, 2007 mysql_query($sql); Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 12, 2007 Author Share Posted July 12, 2007 thank you very much it worked rock on Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 i thought just typing it executes it Oh man.. this is something you really need to get your head around. All this... $sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")"; does is assigns a string to $sql. Exactly the same as... $a = 'this is foo'; its that string (an sql statement) that needs to be passed to mysql_query() to actually execute the query.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.