inserting values into database with PHP&HTML checkbox
Posted: Fri Apr 23, 2010 9:01 am
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
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