Jump to content

PHP checkbox


RTS

Recommended Posts

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]<?php
session_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]
Link to comment
Share on other sites

Small modification (looks nicer and is smaller):

[code]<?php
session_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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.