georgehowell Posted April 23, 2010 Share Posted April 23, 2010 hi again, sorry that my first ever forum entry was so vaguely composed. my question is, if the target column in the MySQL DB is a "tiny init", will the following html checkbox give me a "1" if checked, or "0" unchecked? <input name="subscribe" type="checkbox" id="subscribe" value="true" property="boolean" > Here's my PHP: <?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(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199481-inserting-checkbox-values-into-my-db/ Share on other sites More sharing options...
cags Posted April 23, 2010 Share Posted April 23, 2010 I'm not sure where you got the property attribute from for that input tag, I've never heard of it. That will give the value of 'true' as a string if you use $_POST['subscribe']. Since it is a checkbox though the value will not be submitted if it is not checked. As such you can use a simple check to get a boolean representation... $subscribe = isset($_POST['subscribe']) ? 1 : 0; Quote Link to comment https://forums.phpfreaks.com/topic/199481-inserting-checkbox-values-into-my-db/#findComment-1046960 Share on other sites More sharing options...
georgehowell Posted April 23, 2010 Author Share Posted April 23, 2010 hi CodeCanyon I'm giving it ago in the following fashion: "User.php" <?php class User extends ConnectToDB { public function newsletter($subscribe = isset($_POST['subscribe']) ? 1 : 0; { $ps = $this->db->prepare("Insert into users (subscribe) values (?)"); $ps->execute(array($subscribe)); return ($ps->rowCount() == 1); } } ?> Anybody, please let me know if this looks totally wrong. Not sure what values to set the column within "users" table however. Thanx a mil CodeCanyon Quote Link to comment https://forums.phpfreaks.com/topic/199481-inserting-checkbox-values-into-my-db/#findComment-1046980 Share on other sites More sharing options...
cags Posted April 23, 2010 Share Posted April 23, 2010 a.) Who in the hell is CodeCanyon? b.) The isset code wouldn't normally go in the function declaration, you would normally place it wherever you are calling the function newsletter. Having said that you have a syntax error since you do not close the brackets for newsletters parameters (after 1 : 0). Quote Link to comment https://forums.phpfreaks.com/topic/199481-inserting-checkbox-values-into-my-db/#findComment-1046988 Share on other sites More sharing options...
georgehowell Posted April 23, 2010 Author Share Posted April 23, 2010 sorry Cags, got you mixed up with some advert at the bottom of the page. I kept getting errors after placing the code you gave me into "register.php" - something i'm doing wrong. The attached files give me a result at least, but there's no differentiation between whether the checkbox is checked or not... 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> p.s. What do i owe you for helping me on this one? thanx, George [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/199481-inserting-checkbox-values-into-my-db/#findComment-1047032 Share on other sites More sharing options...
cags Posted April 23, 2010 Share Posted April 23, 2010 I realise what you did with the name, it just amused me . In register.php, line 25 contains... $_POST['subscribe'],)) ...replace that with... isset($_POST['subscribe']) ? 1 : 0)) Technically you don't owe me anything, but my beer fund is always appreciative of donations Quote Link to comment https://forums.phpfreaks.com/topic/199481-inserting-checkbox-values-into-my-db/#findComment-1047038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.