嗨,我在试图解决这样一个基本问题时遇到了很多问题,我确信在面向对象的风格中使用 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` (
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) 默认空值,
hphone
int(11) 默认空值,
salt
varchar(50) 默认空值,主键 ( id
)) 引擎=InnoDB AUTO_INCREMENT=4 默认字符集=latin1;
有人可以指出我出错的正确方向吗?