代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>无标题文档</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head><?php
//下面是接受到的数据
$netId=$_POST['netId'];
$surname=$_POST['surname'];
$forenames=$_POST['forenames'];
$cCode = $_POST['cCode'];$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];$cYear=$_POST['cYear'];
$dob=$_POST['dob'];
if ( $cYear > 4 || $cYear < 1 ) { 
    echo "<script language=javascript>alert('error cYear');history.back();</script>";
}
//处理数据:
$netId = strtoupper ( $netId );
$surname = strtoupper ( $surname );
$forenames = strtoupper ( $forenames );
$cCode = strtoupper ( $cCode );$cYear = (int) $cYear;
$month = (int) $month;
$day = (int) $day;
$year = (int) $year;$dob[0] = $month;
$dob[1] = "-";
$dob[2] = $day;
$dob[3] = "-";
$dob[4] = $year;
//链接数据库
$con = mysql_connect("localhost","root","199026");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
else {
echo "connect database successful";
echo "<br />";
}
$dbselect = mysql_select_db("andy", $con);//插入数据:
$sql = mysql_query("INSERT INTO Student (netId, surname, forenames,cCode,cYear,dob,photo) 
VALUES ('$netId', '$surname', '$forenames', '$cCode', '$cYear', '$dob', '')");
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
?>
<body>
</body>
</html>

解决方案 »

  1.   

    values中变量两边的单引号去掉
      

  2.   


    $sql = mysql_query("INSERT INTO Student (netId, surname, forenames,cCode,cYear,dob,photo) 
    VALUES ('$netId', '$surname', '$forenames', '$cCode', '$cYear', '$dob', '')"); //这里已经执行了 mysql_query 了,即 $sql = true OR false
    if (!mysql_query($sql,$con))  //这里就没有sql语句了,所以报错
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";$sql = "INSERT INTO Student (netId, surname, forenames,cCode,cYear,dob,photo) 
    VALUES ('$netId', '$surname', '$forenames', '$cCode', '$cYear', '$dob', '')";
    if (!mysql_query($sql,$con))
    {
        echo "插入失败";
    }
    echo "1 record added";