I have following html:
<label for="live">Live</label>
<input type="checkbox" name="live" id="live" />
How to save text value of selected = '1'/ or text value of unchecked = '0' to database using SQL INSERT?
Any suggestion much appreciated.
PHP handling the html form (at the moment ‘live’ is an input):
<?php
//Start session
session_start();
//Include database connection details
require_once('../inc/config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
if($link) {
echo "DB SUCESS <br />";
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
if($db){ echo "TABLE SUCCESS<br />"; }
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$live = clean($_POST['live']);
$content = clean($_POST['content']);
//Create INSERT query
$qry = "INSERT INTO news(live, content) VALUES('$live','$content') ";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
//header("location: ../form/register-success.php");
echo "Succes";
exit();
}else {
die("Query failed");
}
?>
^Note the Value “1”. If someone checks the box, it will be a 1, if they don’t it will be NULL. (NOT 0!)
Do an isset on the next page, if it’s set, you’re good to go, if it’s not, just set it to zero.