Sorry, Me again (PHP)

Soldato
Joined
28 Sep 2008
Posts
14,207
Location
Britain
Ok, apart from my other issues, here's another one which I seem incapable of solving.

My form has a checkbox (now, nicely formatted thanks to Skyfall, etc).

When the form is submitted and the data passed to the DB, I want the DB to show a zero if its left unchecked, or a 1 if its checked.

This easy enough to do? Or is there a better method?

Thanks
 
Hmm, ok, how does that fit in with this which is the rest of my form?

PHP:
<?php
} else {
$field1 = stripslashes(mysql_real_escape_string($_POST['field1']));
$field2 = stripslashes(mysql_real_escape_string($_POST['field2']));
$field3 = stripslashes(mysql_real_escape_string($_POST['field3']));

mysql_query("INSERT INTO table (field1, field2, field3) VALUES 

('$field1','$field2','$field3')");
echo "Thank you. You have successfully added <font color=#FF0000>$field1</font> to the database";
}
mysql_close();
?>

Thanks for the phpfreaks link too
 
If you have the column in the mysql database ready for the 0 or 1. Then just output the ($_POST['$CHECKBOXNAMEHERE'] on the end of your current query, example:

<?php
} else {
$field1 = stripslashes(mysql_real_escape_string($_POST['field1']));
$field2 = stripslashes(mysql_real_escape_string($_POST['field2']));
$field3 = stripslashes(mysql_real_escape_string($_POST['field3']));
$checkbox = $_POST['checkbox'];

mysql_query("INSERT INTO table (field1, field2, field3, checkbox) VALUES

('$field1','$field2','$field3','$checkbox')");
echo "Thank you. You have successfully added <font color=#FF0000>$field1</font> to the database";
}
mysql_close();
?>

And just make sure you name your checkbox ID "checkbox". It's easy if you just read up for 20 seconds about checkpost POSTing and basic variables. Then just add it on the end of your sql statement. Don't be lazy :)
 
Hmm, ok, how does that fit in with this which is the rest of my form?

PHP:
<?php
} else {
$field1 = stripslashes(mysql_real_escape_string($_POST['field1']));
$field2 = stripslashes(mysql_real_escape_string($_POST['field2']));
$field3 = stripslashes(mysql_real_escape_string($_POST['field3']));

mysql_query("INSERT INTO table (field1, field2, field3) VALUES 

('$field1','$field2','$field3')");
echo "Thank you. You have successfully added <font color=#FF0000>$field1</font> to the database";
}
mysql_close();
?>

Thanks for the phpfreaks link too
you're stripslashing after escaping?!
 
PHP:
stripslashes(mysql_real_escape_string($_POST['field1']))
You're adding slashes with mysql_real_escape_string(), then taking them away again with stripslashes().
 
In your HTML, do you have a value set for the checkbox? If you don't, I believe it will pass "on" to your form rather than "1". You need to either set the value to 1, or just modify the PHP to check if anything was submitted for the checkbox at all (as a checkbox which isn't checked doesn't submit anything):

Code:
if (isset($_POST['checkbox'])) {
mysql_query("UPDATE table SET checkbox = '1'");
}
You also need to record a 0 in the database if the box isn't checked. You could do that by extending the PHP above using an 'else' statement, but it's probably easier to make sure the default value of the checkbox field in the database is 0, so you don't have to worry about it.
 
Quick test to do for yourself is echo out the $_POST['checkbox'].

If it contains the value you need then you know it's an issue with the SQL, else it's the form not passing it on.

My guess is the form. Post it up.
 
Back
Top Bottom