0

嗨,我在试图解决这样一个基本问题时遇到了很多问题,我确信在面向对象的风格中使用 mysqli。我遵循了许多指南并检查了 php 手册。但我似乎能够用 mysqli 执行的唯一数据库操作是从表中检索数据。我正在尝试创建一个注册页面,并且只是将发布数据放入数据库中。我已经用旧风格的 msql_connect 做过很多次了,但是面向对象的风格让我很困扰。这是我的代码。

$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;
}

用户表的数据库模式是这样的。

CREATE TABLE `users` (

idint(11) unsigned NOT NULL AUTO_INCREMENT, usernamevarchar(10) NOT NULL DEFAULT '', fnamevarchar(11) NOT NULL, lnamevarchar(11) NOT NULL, passwordvarchar(50) NOT NULL DEFAULT '', emailvarchar(40) NOT NULL DEFAULT '', accessint(8) NOT NULL DEFAULT '1', address1varchar(50) DEFAULT NULL, address2varchar(50) DEFAULT NULL, townvarchar(25) DEFAULT NULL, cityvarchar(20) DEFAULT NULL, postcodevarchar(8) DEFAULT NULL, mphoneint (11) 默认空值, hphoneint(11) 默认空值, saltvarchar(50) 默认空值,主键 ( id)) 引擎=InnoDB AUTO_INCREMENT=4 默认字符集=latin1;

有人可以指出我出错的正确方向吗?

4

1 回答 1

-1

试试这个(根据您的需要调整):

<?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;
}
于 2013-10-25T05:27:47.943 回答