日期:2014-05-17  浏览次数:20453 次

数据无法写入数据库
<?php 
session_start();
$coon=@mysql_connect("localhost", "root", "ch12345") or die("数据库链接错误");
mysql_select_db("literature", $coon);
mysql_query("set names 'UTF-8'");
@$username=$_POST['username'];
@$password=$_POST['password'];
@$email=$_POST['email'];
@$sex=$_POST['sex'];
@$constallation=$_POST['constallation'];
$time=time();
$str="select * from user where username={$username}";
  $result1=$coon->query($str); //这行有错误,应该怎么改?
  $row=$result1->fetch_row();
  if($row)

$temp="已有人注册此名,请重新选择名字!"; 
echo $temp;
echo"<a href=register.php>返回</a>";

else {
  $sql="INSERT INTO user(username,sex,time,constallation,password,email)".
  " VALUES('$username','$sex',now(),'$constallation','$password','$email')";
  //$sql="INSERT INTO user VALUES($username,$sex,$time,$constallation,$password,$email)";
  $result=$coon->query($sql);
  if($result==true)
  {
  $_SESSION['mail']="注册成功,请登陆";
  echo "<script>window.location.href='login.php'</script>";
  }
  else {echo "注册失败".mysql_error();}
}
?>

------解决方案--------------------
探讨
$result1=$coon->query($str); //这行有错误,应该怎么改?
$row=$result1->fetch_row();

------解决方案--------------------
$coon=@mysql_connect("localhost", "root", "ch12345") or die("数据库链接错误");
$coon->query($str);
还没见过这样写的!仔细看看手册先http://www.php.net/manual/zh/book.mysql.php
------解决方案--------------------
探讨

$coon=@mysql_connect("localhost", "root", "ch12345") or die("数据库链接错误");
$coon->query($str);
还没见过这样写的!仔细看看手册先http://www.php.net/manual/zh/book.mysql.php