0

出于某种原因,我的 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

谢谢您的帮助。

4

2 回答 2

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

我不支持 mysql_*

于 2012-05-29T16:35:43.083 回答
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).

于 2012-05-29T16:46:31.190 回答