php的prepare准备语句写注册页面

CSDN问答 2021-12-30 12:00:29 阅读数:907

php 注册 语句 准备 prepare
无法实现数据的插入
也没有错误的提示
想问问是哪里错了
<?php session_start(); if(isset($_POST['ok'])) { $username = $_POST['username']; $passwords = $_POST['passwords']; $repasswords = md5($_POST['repasswords']); $telephone = $_POST['telephone']; $email = $_POST['email']; $address = $_POST['address']; $coded = $_POST['code']; $regdate = date('Y-m_d',time()); // $rs = findUserByUserName($username); //创建连接 $mysqli=new mysqli("localhost","root","","lg_shop"); //设置mysqli编码 mysqli_query($mysqli,"SET NAMES gb2312"); //检查连接是否被创建 if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } //创建准备语句 if($stmt=$mysqli->prepare("insert into lg_user (username,password,email,address,telephone,regdate)values(?,?,?,?,?)")){ $stmt->bind_param("sssss",$username,$password,$email,$address,$telephone,$regdate); $stmt->execute(); $stmt->bind_result($username,$username,$password,$telephone,$address,$email); if($stmt->fetch()){ echo "<script>alert('用户名已存在,请重新填写!')</script>"; echo "<script>location='regsiter.php'</script>"; } //写入数据库 else{ //检查验证码 if($_SESSION["identifying"] == $coded) { /** $isok = mysql_query($sql) or die(mysql_error());//这时候可以直观的显示是否是sql语句报错 */ $sql = addUser($username,$repasswords,$email,$address,$telephone,$regdate);//调用函数,新增用户 if($sql == 1){ echo "<script>alert('注册成功!')</script>"; echo "<script>location='index.php'</script>"; } else{ echo "<script>alert('注册失败,请重新填写!')</script>"; echo "<script>location='regsiter.php'</script>"; } } else{ echo "<script>alert('验证码错误!')</script>"; echo "<script>location='regsiter.php'</script>"; } } } } //判断用户是否存在?>

```




采纳答案:

6个字段,题主只有5个参数。。。
if($stmt=$mysqli->prepare("insert into lg_user (username,password,email,address,telephone,regdate)values(?,?,?,?,?)")){
$stmt->bind_param("sssss",$username,$password,$email,$address,$telephone,$regdate);



其他答案2:

insert into lg_user (username,password,email,address,telephone,regdate)values(?,?,?,?,?)
insert语句6个字段,只有5个?号,少了一个?号。


其他答案3:
<?php session_start(); if(isset($_POST['ok'])) { $username = $_POST['username']; $passwords = $_POST['passwords']; $repasswords = md5($_POST['repasswords']); $telephone = $_POST['telephone']; $email = $_POST['email']; $address = $_POST['address']; $coded = $_POST['code']; $regdate = date('Y-m_d',time()); // $rs = findUserByUserName($username); //创建连接 $mysqli=new mysqli("localhost","root","","lg_shop"); //设置mysqli编码 mysqli_query($mysqli,"SET NAMES gb2312"); //检查连接是否被创建 if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } //创建准备语句 if($stmt=$mysqli->prepare("insert into lg_user (username,password,email,address,telephone,regdate)values(?,?,?,?,?,?)")){ $stmt->bind_param("ssssss",$username,$password,$email,$address,$telephone,$regdate); $stmt->execute(); $stmt->bind_result($username,$password,$email,$address,$telephone,$regdate); if($stmt->fetch()){ echo "<script>alert('用户名已存在,请重新填写!')</script>"; echo "<script>location='regsiter.php'</script>"; } //写入数据库 else{ //检查验证码 if($_SESSION["identifying"] == $coded) { /** $isok = mysql_query($sql) or die(mysql_error());//这时候可以直观的显示是否是sql语句报错 */ $sql = addUser($username,$repasswords,$email,$address,$telephone,$regdate);//调用函数,新增用户 if($sql == 1){ echo "<script>alert('注册成功!')</script>"; echo "<script>location='index.php'</script>"; } else{ echo "<script>alert('注册失败,请重新填写!')</script>"; echo "<script>location='regsiter.php'</script>"; } } else{ echo "<script>alert('验证码错误!')</script>"; echo "<script>location='regsiter.php'</script>"; } } } } //判断用户是否存在 ?>

现在又提示如下错误了

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in D:\study\xampp\htdocs\lg_test\addRegsiter.php on line 27

Fatal error: Uncaught Error: Call to undefined function addUser() in D:\study\xampp\htdocs\lg_test\addRegsiter.php:40 Stack trace: #0 {main} thrown in D:\study\xampp\htdocs\lg_test\addRegsiter.php on line 40

版权声明:本文为[CSDN问答]所创,转载请带上原文链接,感谢。 https://ask.csdn.net/questions/7619945