Jump to content

checkbox values to database.


jushiro

Recommended Posts

:confused:

 

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. :(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.