Home  >  Article  >  Backend Development  >  #生手来袭# 一段简单的代码为什么提示出错

#生手来袭# 一段简单的代码为什么提示出错

WBOY
WBOYOriginal
2016-06-13 13:20:551232browse

#新手来袭# 一段简单的代码为什么提示出错
public function addemail(){
  $email_name=$_POST['name'];
  $email_email=$_POST['email'];

  $con=mysqli_connect("localhost","root","root","bottlelover")
  or die('connect error');

  mysqli_set_charset($con,"utf8");

  $sql="SELECT * FROM email_list WHERE email_email = $email_email";
  $result=mysqli_query($con,$sql)
  or die('query1 error');

  if($row=mysqli_fetch_array($result)){
  $this->assign("res",'邮箱已被使用,用户添加失败');
  mysqli_close($con);
  $this->display("Bottleloveradd");
  }
  else {
  $sql="INSERT INTO email_list(email_name,email_email) VALUES ('$email_name','$email_email')";
  $result=mysqli_query($con,$sql)
  or die('query2 error');
  $this->assign("res",'新用户添加成功');
  mysqli_close($con);
  $this->display("Bottleloveradd");
  }
  }

--------------------------------------------以上是代码--------------------

我的本意是添加新用户之前要检查邮箱是不是已经被使用,
$sql="SELECT * FROM email_list WHERE email_email = $email_email";
  $result=mysqli_query($con,$sql)
  or die('query1 error');
这段代码提示query1 error错误,其他运行正常;为什么,明显没有错啊,表名没错,列名没错,变量名都没错.....

------解决方案--------------------
$sql="SELECT * FROM email_list WHERE email_email = '$email_email'";
------解决方案--------------------
字符类型的量要用引号括起
正因为太简单了,所以容易忽视
------解决方案--------------------
楼上正解,少了单引号,因为这是字符串

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn