dousi4472 2013-10-24 15:27
浏览 33
已采纳

使用mysqli准备绑定和执行将信息插入数据库的问题

Hi there i'm having a lot of problems trying to get past such a basic problem I am sure using mysqli in the object oriented style. I have followed numerous guides and checked the php manual. But the only database operation I seem to be able to carry out with mysqli is retrieving data from tables. I am trying to create a registration page and am just putting post data into a database. I have done this many times with the older style msql_connect but the object oriented style is troubling me. Here is my code.

$db = new mysqli(info is all corrected as i tested database connectivity);

 if ($db->connect_errno > 0){
die('unable to connect to database [' . $db->connect_error . ']');
}
else {
    echo('conenction successful');
}

    $fname = $_POST['fname'];  
    $lname = $_POST['lname']; 
    $uname = $_POST['uname']; 
    $email = $_POST['email']; 
    $email1 = $_POST['emai1']; 
    $ad1 = $_POST['ad1']; 
    $ad2 = $_POST['ad2']; 
    $town = $_POST['town']; 
    $city = $_POST['city']; 
    $postcode = $_POST['postcode'];
    $mphone =  $_POST['mphone'];
    $hphone = $_POST['hphone'];
    $password ="hh";
    if (!($stmt->$db->prepare("INSERT INTO `users` (`username`, `fname`, `lname`, 
                            `password`, `email`, `address1`, `address2`, `town`, 
                            `city`, `postcode`, `mphone`, `hphone`)
                            VALUES
                            (?,?,?,?,?,?,?,?,?,?,?,?)")))
    {
        echo "Prepare failed: (" . $db->errno . ")" . $db->error;
        }

    if (!$stmt->bind_param("ssssssssssii",  $uname, $fname, $lname, $password, $email, $ad1, $ad2, $town, $city, $postcode, $mphone, $hphone))
    {
        echo "binding parameters failed: (" . $stmt->errno . ")" . $stmt->error;
    }               
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

The Database schema for the users table is this.

CREATE TABLE `users` (

id int(11) unsigned NOT NULL AUTO_INCREMENT, username varchar(10) NOT NULL DEFAULT '', fname varchar(11) NOT NULL, lname varchar(11) NOT NULL, password varchar(50) NOT NULL DEFAULT '', email varchar(40) NOT NULL DEFAULT '', access int(8) NOT NULL DEFAULT '1', address1 varchar(50) DEFAULT NULL, address2 varchar(50) DEFAULT NULL, town varchar(25) DEFAULT NULL, city varchar(20) DEFAULT NULL, postcode varchar(8) DEFAULT NULL, mphone int(11) DEFAULT NULL, hphone int(11) DEFAULT NULL, salt varchar(50) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

Can someone please point me in the right direction with where I am going wrong.

  • 写回答

1条回答 默认 最新

  • douchen1924 2013-10-25 05:27
    关注

    Try this (adjust to your needs):

    <?php
    
    if (!$stmt = $db->prepare(/* I'm not gonna write all this down :P */)) {
        // Error output (or better log them somewhere, users don't have to see this stuff)
        return;
    }
    $stmt->bind_param("ssssssssssii", $uname, $fname, $lname, $password, $email, $ad1, $ad2, $town, $city, $postcode, $mphone, $hphone);
    if (!$stmt->execute()) {
        // Error output (again, better to log them somewhere)
        return;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了