jushiro Posted September 19, 2011 Share Posted September 19, 2011 Basically i made a form with checkbox using post.. here's the form <?php echo '<form name="formupload" method="post" action="val3.php">'; echo '<input type="image" src="images/reject.png" name="test" width="170" height="35" border="0" value="reject">'; echo "<table border=\"5\" >"; echo "<tr><th>List of Instructor</th>"; if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo "<tr><td>"; echo "<input type='checkbox' name='list[]' value='".$row['fullname']."'>" .$row['fullname']."<br/></td>"; echo "</tr>"; } } else { echo "<tr><td>No one to Approve</td></tr>"; } echo '</form>'; ?> Then my val3.php will output the values of the checked checkbox. here's the code. <?php foreach ($_REQUEST['list'] as $checkbox) { echo $checkbox; echo "<br/>"; } ?> Now i don't know how to insert those values to my database.. help pls. Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/ Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 1. if(mysql_num_rows($result)) in the above code you are checking if mysql_num_rows returns TRUE or FALSE, this is an incorrect usage, mysql_num_rows returns an integer of the number of rows grabbed from the query.. you will want to do something like.. if(mysql_num_rows($result) > 0) to check and make sure that at least one row was grabbed from your query statement. 2. your checkbox name does not need to be in array format.. as this accomplishes nothing.. "list" is fine 3. since I have no clue how you have your database table for this information setup.. I will assume that this information is to be stored in one row with multiple fields., which is how I would go about this.. now what I would do is store the check box values into an array, then use that array to store that values into one row in your table.. or you can use json_encode or serialize to store that array in one field.. which I will show below. <?php foreach ($_POST['list'] as $checkbox) { $checkbox_array[] = $checkbox; //store values in array } $serialize_checkbox_array = serialize($checkbox_array()); $sql = "INSERT INTO table (checkbox_array) VALUES('$serialize_checkbox_array')"; $query = mysql_query($sql) or die(mysql_error()); //use die() for debugging purposes only ?> then use unserialize to sort the data back into normal array format Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/#findComment-1270609 Share on other sites More sharing options...
jushiro Posted September 19, 2011 Author Share Posted September 19, 2011 my database works like this.. i have a db w/c contains fullname .. then i made a checkbox with their full name.. once they were check they will a option to delete or insert values.. What should my sql if i would delete? i used this code.. <?php foreach ($_POST['list'] as $checkbox) { $checkbox_array[] = $checkbox; //store values in array } $serialize_checkbox_array = serialize($checkbox_array()); $sql="SELECT * FROM teacher WHERE fullname='$serialize_checkbox_array'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ mysql_query("DELETE FROM teacher WHERE fullname='$serialize_checkbox_array')"); ?> .. it wont work. help pls? Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/#findComment-1270616 Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 didn't know you were using the checkboxes in that way.. this method should work for you then.. <?php foreach ($_POST['list'] as $checkbox) { mysql_query("DELETE FROM teacher WHERE fullname='$checkbox')") or die(mysql_error); ?> } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/#findComment-1270623 Share on other sites More sharing options...
jushiro Posted September 21, 2011 Author Share Posted September 21, 2011 yay! it works! thx!... but how bout for inserting values? i tried to change the query here.. <?php foreach ($_POST['list'] as $checkbox) { mysql_query("DELETE FROM teacher WHERE fullname='$checkbox')") or die(mysql_error); ?> } ?> TO. <?php foreach ($_POST['list'] as $checkbox) { mysql_query("INSERT INTO registeredprof (Username, Password, gname, mname, famname, fullname) VALUES ('$username', '$password', '$gname', '$mname', '$famname', '$fullname')") or die(mysql_error); } ?> it did'nt insert values.. what should i do? Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/#findComment-1271288 Share on other sites More sharing options...
jushiro Posted September 21, 2011 Author Share Posted September 21, 2011 weee. i edited my code. its working now. thx! Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/#findComment-1271289 Share on other sites More sharing options...
AyKay47 Posted September 21, 2011 Share Posted September 21, 2011 I will double back on what I said previously.. the above method will work as noted, however its not the best decision to have a query in a loop, as this will use more memory... now in this case I don't think it will be an issue seeing as you don't have too many iterations.. however this method is a better practice.. $list_arr = $_POST['list']; $query = mysql_query("DELETE FROM teacher WHERE fullname IN ($list_arr)") or die(mysql_error); Quote Link to comment https://forums.phpfreaks.com/topic/247423-checkbox-values-to-database/#findComment-1271383 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.