georegjlee Posted March 21, 2007 Share Posted March 21, 2007 Im trying to assign the checkbox the row id number but dont know how to go about it. Each row contains problem_id, problem_severity, user_name, password, waterway_id. I am passing the checkbox as an array but how do I assign the problem_id to the checked checkbox. The idea is to place the selected rows in a new table and delete the rest. Any ideas? <html> <head> <title>First PHP Script</title> </head> <body> <?php //connection to the database $dbhandle = mysql_connect("localhost", "root", "") or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mysql_select_db("waterways", $dbhandle) or die("Couldn't open database myDB"); //declare the SQL statement that will query the database $query = "SELECT * FROM pending_problems"; //$query = $query . "WHERE problems.canal_Id = waterways.canal_Id"; //execute the SQL query and return records $result = mysql_query($query, $dbhandle); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } ?> <h3 style = "color: blue"> Problems Table</h3> <form id="form1" name="form1" method="post" action=""> <table border = "1" cellpadding = "3" cellspacing = "2"> <?php for ( $counter = 0; $row = mysql_fetch_row($result); $counter++) { print( "<tr>"); foreach ( $row as $key => $value ) print ( "<td>$value</td>" ); print ( "<td>Check box to verify</td>"); print ( "<td><p><input type=\"checkbox\" name=\"option1[]\" value=\"?????\"></p></td>" ); print ( "</tr>" ); } //close the connection mysql_close($dbhandle); ?> </table> <input type="submit" value = "Update Problems" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/43658-checkboxs-as-arrays/ Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 <html> <head> <title>First PHP Script</title> </head> <body> <?php //connection to the database $dbhandle = mysql_connect("localhost", "root", "") or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mysql_select_db("waterways", $dbhandle) or die("Couldn't open database myDB"); //declare the SQL statement that will query the database $query = "SELECT * FROM pending_problems"; //$query = $query . "WHERE problems.canal_Id = waterways.canal_Id"; //execute the SQL query and return records $result = mysql_query($query, $dbhandle); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } ?> <h3 style = "color: blue"> Problems Table</h3> <form id="form1" name="form1" method="post" action=""> <table border = "1" cellpadding = "3" cellspacing = "2"> <?php for ( $counter = 0; $row = mysql_fetch_row($result); $counter++) { print( "<tr>"); foreach ( $row as $key => $value ) print ( "<td>$value</td>" ); print ( "<td>Check box to verify</td>"); print ( "<td><p><input type=\"checkbox\" name=\"option1[]\" value=\"".$id."\"></p></td>" ); print ( "</tr>" ); } //close the connection mysql_close($dbhandle); ?> </table> <input type="submit" name="submit" value = "Update Problems" /> </form> </body> </html> added $id to the value. Once that is done here is the second part. <?php // should be added after ////select a database to work with //$selected = mysql_select_db("waterways", $dbhandle) // or die("Couldn't open database myDB"); if (isset($_POST['submit'])) { foreach ($_POST['option'] as $id) { print $id . " should be deleted here.<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43658-checkboxs-as-arrays/#findComment-211998 Share on other sites More sharing options...
georegjlee Posted March 21, 2007 Author Share Posted March 21, 2007 What are the two periods for on .$id. ? Quote Link to comment https://forums.phpfreaks.com/topic/43658-checkboxs-as-arrays/#findComment-212011 Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 When you put a period in the string it "breaks it up" and allows for processing. IE: <?php $test2 = "myTest"; $test = "This is a test $test2 "; // prints this is a test myTest $test = "This is a test ".$test2."!"; // prints this is a test myTest! // It really just makes sure that the variable will display right. $test = 'This is a test '.$test2.'!'; // prints this is a test myTest! // But with single quotes it is necessary because single quotes take the variables literally. ?> I just use them to make sure my variables are displayed correctly. Quote Link to comment https://forums.phpfreaks.com/topic/43658-checkboxs-as-arrays/#findComment-212014 Share on other sites More sharing options...
georegjlee Posted March 21, 2007 Author Share Posted March 21, 2007 I think i follow it but the what dose the code below do exatly. if (isset($_POST['submit'])) { // Dose this check if the submit button was pressed? foreach ($_POST['option'] as $id) { // Should this be the checkbox array? print $id . " should be deleted here.<br />"; // this is where I write my SQL delete? } } Quote Link to comment https://forums.phpfreaks.com/topic/43658-checkboxs-as-arrays/#findComment-212038 Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 <?php if (isset($_POST['submit'])) { // Make sure the form was submitted if (is_array($_POST['option1'])) { // Make sure the checkbox is an array. foreach ($_POST['option1'] as $id) { // This loops through the checkbox array print $id . " should be deleted here.<br />"; // Write processing code here. } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43658-checkboxs-as-arrays/#findComment-212049 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.