#生手来袭# 一段简单的代码为什么提示出错
程序员文章站
2022-05-04 13:38:26
...
#新手来袭# 一段简单的代码为什么提示出错
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'";
------解决方案--------------------
字符类型的量要用引号括起
正因为太简单了,所以容易忽视
------解决方案--------------------
楼上正解,少了单引号,因为这是字符串
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'";
------解决方案--------------------
字符类型的量要用引号括起
正因为太简单了,所以容易忽视
------解决方案--------------------
楼上正解,少了单引号,因为这是字符串
相关文章
相关视频