PHP代码没有将数据写入MySQL

时间:2012-05-29 16:34:02

标签: php mysql

由于某些原因,我的PHP代码不会将我的变量写入我的mysql数据库。

一切正常,直到评论“在这里死去”

<?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.";

?>

我的数据库全部设置完毕,我不知道它为什么不将数据发送到数据库。这是mySQL的图片 mysql

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

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

我不支持mysql _ *

答案 1 :(得分:0)

你应该使用这个

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

它更干净,可能更有效,因为is_int()返回true或false语句,if(is_int)基本上意味着if(is_int($ foo == true)。