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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100