0

For some reason my php code isnt writing my variables into my mysql database.

Everything works up until the comment "dies here"

<?php
$name = ucwords($_POST['name']);
$sex = ucwords($_POST['sex']);
$age = intval($_POST['age']);
$email = $_POST['email'];

//DB ACCESS
$db = mysql_connect("localhost", "root", "root");
mysql_select_db("namedb", $db);
//DB ACCESS

if ($sex != 'M' && $sex != 'F') {

echo "Please go back and enter either M or F for 'Sex' <br />";
echo "<a href='index.html'>Back</a>";
die;
}

if (is_int($age) != yes) {
echo "Please enter a number for your age. <br />";
echo "<a href='index.html'>Back</a>";
die;
}

$query = "INSERT INTO people (age, name, email, sex) VALUES($age, $name, $email, $sex)";
mysql_query($query) or die ("Error Updating DB"); //DIES HERE
echo "Thanks $name, we've added you to our database.";

?>

My database is all set up, I have no idea why it isn't sending the data to the database. Here's a picture of the mySQL mysql

Thanks for the help.

4
  • 3
    you have to quote VALUES Commented May 29, 2012 at 16:34
  • 2
    What does mysql_error() say? Commented May 29, 2012 at 16:35
  • Don't use mysql_ functions. Use PDO or mysqli with parameterized queries. That would also fix the serious SQL injection problems you currently have. Commented May 29, 2012 at 16:44
  • Could you give me a tutorial on how tO use PDO. Im not fimiar with it Commented May 29, 2012 at 17:26

2 Answers 2

3
$query = "INSERT INTO people (age, name, email, sex) VALUES('$age', '$name', '$email', '$sex')";

I don't support mysql_*

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. Please consider using mysqli_* or PDO, mysql_* is in deprecation process, just so you know.
0

You should use this

  if (!is_int($age)){
       echo "Please enter a number for your age. <br />";
       echo "<a href='index.html'>Back</a>";
       die;
}

It's cleaner and probably more effective since is_int() returns a true or false statement and the if (is_int) basically means if (is_int($foo == true).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.