php,MySQL store checkbox values in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kokotas
Forum Newbie
Posts: 2
Joined: Sat Jan 08, 2011 2:32 am

php,MySQL store checkbox values in database

Post by kokotas »

Hi,

I have this form which will create a checkbox list using data from my database and also determin if a checkbox had been checked before and check if it had.

<form style="text-align:center" name="PrefRestaurant" id="PrefRestaurant" action="preferances_check.php" method="post"><table align="center"> <?php checkbox(id, name, restaurants, id); ?></table>
<input type="submit" name="Prefer" id="Prefer" value="Επιλογή"/></form>

Code: Select all

function checkbox($intIdField, $strNameField, $strTableName, $strOrderField, $strMethod="asc") {
	$strQuery = "select $intIdField, $strNameField
               from $strTableName
               order by $strOrderField $strMethod";
    $rsrcResult = mysql_query($strQuery);
	
	
	while ($arrayRow = mysql_fetch_assoc($rsrcResult)) {
		$testqry = "SELECT * FROM user_restaurant WHERE user_id = $_SESSION[UserId] AND restaurant_id = $arrayRow[id]";
		$rsltestqry = mysql_query($testqry);
		$numrows = mysql_num_rows($rsltestqry);
		if ($numrows == 1) {
			echo "<tr align=\"left\"><td><input type=\"checkbox\" name=\"restaurant[]\" value=\"$arrayRow[id]\" checked/>$arrayRow[name]</td></tr>";
		}
		else{
		echo "<tr align=\"left\"><td><input type=\"checkbox\" name=\"restaurant[]\" value=\"$arrayRow[id]\" />$arrayRow[name]</td></tr>";
		}
	}
			
}			

Now the part which I can't get to work is when I'm trying to store the new values in my database. When I click the submit button I clear my database of any row that is related to the currently loggedin user and I want to store his new preferences (checked cheboxes). I've read that only the cheked checkboxes' values are POSTed so I did this (preferances_check.php)

Code: Select all

foreach($_POST['restaurant']  as  $value)  {
 
 $query="INSERT INTO user_restaurant VALUES ('$_SESSION[UserId]','$value')";
 
}   
But it is not working, nothing gets written in my table :( Could someone please enlighten me on this? Thnks!
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: php,MySQL store checkbox values in database

Post by Neilos »

Code: Select all

$query="INSERT INTO user_restaurant VALUES ('$_SESSION[UserId]','$value')";
Here you are just assigning a string to a variable name. You need to then use the line;

Code: Select all

mysql_query($query);
You've done this in your first block of code but I cannot see it in the second.

also correct me if I am wrong but;

Code: Select all

$_POST['restaurant']
is a regular variable not an array. The foreach will only work with an array datatype in the argument.
kokotas
Forum Newbie
Posts: 2
Joined: Sat Jan 08, 2011 2:32 am

Re: php,MySQL store checkbox values in database

Post by kokotas »

thanks but i found the answer

Code: Select all

$restaurant=$_POST['restaurant'];
foreach ($restaurant as  $value)  {
  $query="INSERT INTO user_restaurant (user_id, restaurant_id) VALUES ('$_SESSION[UserId]','$value')";
 $res=mysql_query($query);
}
Post Reply