RTS Posted October 13, 2006 Share Posted October 13, 2006 I am working on a PM system for my site, and having trouble with a line of code for a checkbox. I want it so that if the ncheckbox is checked, it inserts the word "yes" in to the column "sig" in my mysql database. hers the code I have so far. It gives me [code]Parse error: parse error in /Library/WebServer/Documents/users/send.php on line 8[/code]send.php:[code]<?phpsession_start();$con = mysql_connect("localhost","ZackBabtkis","");$subject = mysql_real_escape_string($_POST[subject]);$message = mysql_real_escape_string($_POST[message]);$user = $_COOKIE["user"];$sender = $_SESSION['username'];$date = date("F j, Y, g:i a");$sig = if (isset($_POST["sig"])) echo "yes"; if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("test", $con);$sql = "INSERT INTO messages (Username, Sender, Subject, Message, Date, sig) VALUES ('$user','$sender','$subject','$message', '$date', '$sig')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "Your message has been sent to $user";mysql_close($con)?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/ Share on other sites More sharing options...
.josh Posted October 13, 2006 Share Posted October 13, 2006 $sig = (isset($_POST["sig"])) ? "yes" : NULL; Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108468 Share on other sites More sharing options...
RTS Posted October 13, 2006 Author Share Posted October 13, 2006 okay, but how do I make it so that when the box isnt checked it insterts "no" in to the database? Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108472 Share on other sites More sharing options...
.josh Posted October 13, 2006 Share Posted October 13, 2006 change the NULL to "no"$sig = (isset($_POST["sig"])) ? "yes" : "no";or else in your database you can set the default to "no". Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108476 Share on other sites More sharing options...
Daniel0 Posted October 13, 2006 Share Posted October 13, 2006 Small modification (looks nicer and is smaller):[code]<?phpsession_start();$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());mysql_select_db("test", $con);$subject = mysql_real_escape_string($_POST['subject']);$message = mysql_real_escape_string($_POST['message']);$date = date("F j, Y, g:i a");$sig = isset($_POST['sig']) ? "yes" : "no";mysql_query("INSERT INTO messages (Username, Sender, Subject, Message, Date, sig) VALUES ('{$_COOKIE['user']}','{$_SESSION['username']}','{$subject}','{$message}', '{$date}', '{$sig}')",$con)) or die('Error: ' . mysql_error());echo "Your message has been sent to {$user}";mysql_close($con)?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108480 Share on other sites More sharing options...
RTS Posted October 13, 2006 Author Share Posted October 13, 2006 when I have [code]$sig = (isset($_POST["sig"])) ? "yes" : "no";[/code] it only echos no, when I change it to [code]$sig = (isset($_POST["sig"])) ? "no" : "yes";[/code] it always inserts yes. it seems to be ignoring the first quotes Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108491 Share on other sites More sharing options...
Daniel0 Posted October 13, 2006 Share Posted October 13, 2006 Make sure that your checkbox has a non-empty value attribute. Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108492 Share on other sites More sharing options...
.josh Posted October 13, 2006 Share Posted October 13, 2006 then the problem is with your post variable itself$sig = (isset($_POST["sig"])) ? "yes" : "no";basically what this does is if $_POST['sig'] is set (it exists) then $sig = "yes"; if it does not exist, then $sig = "no";the fact that it keeps setting $sig to the one on the right of the : means taht $_POST['sig'] is not set. check to make sure you actually named your checkbox 'sig' in your form, and that it is spelled right, your form method = 'post' etc.. Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108493 Share on other sites More sharing options...
RTS Posted October 13, 2006 Author Share Posted October 13, 2006 thanks, i figured it out. one more question though. I am trying to recieve the message ont eh other end, and if the column 'sig' says yes, I want the message to echo test on the bottom. this is what I have so far, but it is giving me a parse error.[code]<?php$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());mysql_select_db("test", $con);$result = mysql_query("SELECT * FROM messages WHERE ID=" . $_GET['id']);while($row = mysql_fetch_array($result)){$sigtest = $row['sig'];$sig = if ($sigtest = "no") echo "test"; echo $sig?>[/code]can someone tell me what might be wrong? I appreciate all the quick helpful replies by the way :) Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108508 Share on other sites More sharing options...
Psycho Posted October 13, 2006 Share Posted October 13, 2006 You need a semilcolon at the end of that last echo. Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108622 Share on other sites More sharing options...
.josh Posted October 14, 2006 Share Posted October 14, 2006 a) once again you have not properly used the ternary operator. The ternary operator format looks like this:$variable = (condition) ? condition_is_true : condition_is_false;b) you are also using an = instead of an == sign in your condition. Those are 2 different operators. = is the assignment operator. == is the equality operator. c) you should NEVER insert a $_GET variable directly into a query, otherwise you are begging for sql injection hacks. you should always sanitize your variables before using them in queries. you should at the very very VERY least do something like what I have done below. d) as mentioned above, you're missing a ; on your echo $sig line. [code]<?php$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());mysql_select_db("test", $con);$id = mysql_real_escape_string($_GET['id']);$result = mysql_query("SELECT * FROM messages WHERE ID=" . $id);while($row = mysql_fetch_array($result)) { $sigtest = $row['sig']; $sig = ($sigtest == "no") "test" : NULL; // will set $sig to "test" if $sigtest equals "no" echo $sig;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23875-php-checkbox/#findComment-108715 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.