Jump to content

inserting value from checkbox into the db, not giving required value


simflex

Recommended Posts

Greetings again, good helpers.

 

I have been trying for over 3 hours trying to figure out why the code I am using to insert checkbox value into the db isn't working.

 

It doesn't matter whether I check the box or not, the value is always 0.

 

Can you please assist again?

 

Many thanks as always.

 

//This works with radio button
<input type=radio value=more name=repeat>

//But the codes below don't work

//var assignment
$mainpage = (isset($_POST['mainpage']) && $_POST['mainpage'] == '1')? 1 : 0;
  
//insert statement 
            $query = "insert into ".$EVENTS_TB." values
  (null,'$timestamp','$title','$description','$url','$email','$cat','$stime','$etime','$bday','$bmonth','$byear','$approve','0','$nobody','','$loc','$mainpage')";
            $result = mysql_query($query);

//<form field
echo translate("Main Page")."\n";
echo "<input type=checkbox name=mainpage value=1><br/>\n";

This test case works fine:

<?php
if (isset($_POST['submit'])) {
  echo '<pre>' . print_r($_POST,true) . '</pre>';
  $mainpage = (isset($_POST['mainpage']) && $_POST['mainpage'] == '1')? 1 : 0;
  echo $mainpage . "<br>\n";
}
?>
<html>
<head>
	<title>test checkbox</title>
</head>
<body>
	<form action="" method="post">
		Main Page: <input type="checkbox" name="mainpage" value="1" />
		<input type="submit" value="Test" name="submit">
	</form>
</body>
</html>

 

I notice that you don't check to make sure your query is really working, so change

<?php
$result = mysql_query($query);
?>

to

<?php
$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

to see if there's a problem there.

 

Ken

Thanks for the response.

Funny after trying the code you suggested, I don't get any warnings or errors and 0 is being getting inserted into the db.

 

Just for starters, the datatype I am using for the mainpage filedname is tinyint(1).

At the top of the processing script, put

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>

this will dump the data that is being given to your script from your form. Make sure the value is being sent.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.