inserting values into database with PHP&HTML checkbox

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
georgehowell
Forum Newbie
Posts: 9
Joined: Fri Apr 23, 2010 8:44 am

inserting values into database with PHP&HTML checkbox

Post by georgehowell »

good morning USA people (it's almost morning where i am)
I've got this form almost working for college, but there's one thing lacking... the checkbox doesn't give a "1" when checked (ie, "0" is inserted into the Database either way). Here's the html:

<form name="join" action="register.php" onsubmit="checkEmptyFields()" method="post">
<input name="subscribe" type="checkbox" id="subscribe">
<input name="submit" type="submit" id="submit" >
</form>

Here's the "register.php" file:

<?php
include("lib/ConnectToDb.php");
include("lib/User.php");

print_r($_POST);
if(isset($_POST['username']) && isset($_POST['password'])) {
$user = new User();
if($user->register(
$_POST['subscribe'])) {
include("Welcome.php");
exit();
}
}
?>

Here's the "User.php" file:

<?php
class User extends ConnectToDb {
public function __construct()
{ parent::__construct(); }
public function login($username,$password)
{
$ps = $this->db->prepare("Select username, password from users where username = ? and password = ?");
$ps->execute(array($username,$password));
return ($ps->rowCount() == 1); }
public function register($subscribe)
{
$ps = $this->db->prepare("Insert into users (subscribe) values (?)");
$ps->execute(array($subscribe));
return ($ps->rowCount() == 1); }
}
?>

Cheers,
George
georgehowell
Forum Newbie
Posts: 9
Joined: Fri Apr 23, 2010 8:44 am

Re: inserting values into database with PHP&HTML checkbox

Post by georgehowell »

SOLVED!!

if($user->register(
$_POST['subscribe']))

IS REPLACED WITH:

if($user->register(
isset($_POST['subscribe']) ? 1 : 0))



thanks to Cags from phpfreaks.com
Post Reply