在表单字段中添加电话&将值发布到数据库

时间:2016-10-07 10:08:27

标签: php mysql

原始代码如下

signup.php

if(isset($_POST['btn-signup']))
{
    $uname = trim($_POST['txtuname']);
    $email = trim($_POST['txtemail']);
    $upass = trim($_POST['txtpass']);
    $cpass = trim($_POST['txtcpass']);
    $code = md5(uniqid(rand()));

    $stmt = $reg_user->runQuery("SELECT * FROM tbl_users WHERE userEmail=:email_id");
    $stmt->execute(array(":email_id"=>$email));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    /* some code in middle */
        if($reg_user->register($uname,$email,$upass, $code))
        {           
            $id = $reg_user->lasdID();      
            $key = base64_encode($id);
            $id = $key;

class.usr

public function register($uname,$email,$upass, $code)
    {
        try
        {                           
            $password = md5($upass);
            $stmt = $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass, tokenCode) 
                                                         VALUES(:user_name, :user_mail, :user_pass, :active_code)");
            $stmt->bindparam(":user_name",$uname);
            $stmt->bindparam(":user_mail",$email);
            $stmt->bindparam(":user_pass",$password);
            $stmt->bindparam(":active_code",$code);
            $stmt->execute();   
            return $stmt;
        }

html表单

<form>
<input id="pass2" type="password" class="input-block-level" placeholder="confirm Password" name="txtcpass" required />
</form>

我正在尝试为表单添加新的textfield [phone]并将值发布到数据库中。 我创建了列名为“phone”,我更改了一些代码如下:

$phone = trim($_POST['phone']);
if($reg_user->register($uname,$email,$upass, $code, $phone))

public function register($uname,$email,$upass, $code, $phone)
        $stmt = $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass, tokenCode, phone) 
                                                         VALUES(:user_name, :user_mail, :user_pass, :active_code, phone)");
        $stmt->bindparam("phone",$phone);

但它对我没用,我是php&amp;的新手MySQL的。

错误

SQLSTATE [HY093]:参数号无效:绑定变量数与tokenssorry数不匹配,Query无法执行...

1 个答案:

答案 0 :(得分:2)

 VALUES(:user_name, :user_mail, :user_pass, :active_code, phone)");

应该是

 VALUES(:user_name, :user_mail, :user_pass, :active_code, :phone)");

您忘记了:

前面的phone