Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How could I declared checkbox, if checkbox checked = 1 else = 0.
After that insert or update to database,in the database will only 1 and 0 represent checked and unchecked.

<input type="checkbox" id="homepage" name="homepage" value="1" />

This my checkbox code,it haven't declared.

P/S: I'm using PHP code writing. And I'm newbie for php.

Updated 29-Dec-13 20:45pm
Peter Leow 30-Dec-13 4:09am    
I have updated solution 1 to make it clearer and corrected some typo.

1 solution

I will show you an example that you can adapt to your need.
In the html form:
<form action="nameofphpscript.php" method="post">
    Want to go home?
    <input type="checkbox" id="homepage" name="homepage" value="1" />
    <input type="submit" name="formSubmit" value="Submit" />

in the 'nameofphpscript.php' file:

$status = '0';

if(isset($_POST['homepage']) && $_POST['homepage'] == '1')
	$status = $_POST['homepage'];

$mysqli = new mysqli('localhost', 'username', 'password', 'databasename');
// check connection
if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
// assuming there is a checkstatus field in the table
$stmt = $mysqli->prepare("UPDATE tablename SET checkstatus =  ? WHERE userid = 'bill123'");

$stmt->bind_param('s', $status);
// execute prepared statement
// close statement
// close connection


Hope it is helpful.
Share this answer
Adrian4263 31-Dec-13 3:28am    
Thanks for your answered.I solved it already.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900