php 向mysql插入数据出错

zhang_jianzhi 2010-07-25 12:46:19

CREATE DATABASE `user`
use user
CREATE TABLE `userinfo` (
`userid` int(11) NOT NULL auto_increment,
`username` varchar(10) default NULL,
`pwd` varchar(10) default NULL,
PRIMARY KEY (`userid`)
)
上面这是数据库代码
<?php


$link = mysqli_connect('localhost', 'root', '123', 'user');

if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql="insert into userinfo (username,pwd) values (?,?)";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, $name,$p);

$name="aa";
$p="aa";

mysqli_stmt_execute($stmt);

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

mysqli_stmt_close($stmt);

mysqli_query($link, "delete from userinfo where username='admin'");
printf("%d Row deleted.\n", mysqli_affected_rows($link));

mysqli_close($link);


?>
上面这是php代码

结果报错为:

Warning: mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]: Number of elements in type definition string doesn't match number of bind variables in D:\Program Files\php\AppServ\www\lianxi\dblogin.php on line 68
0 Row inserted. 0 Row deleted.

这是怎么回事?请帮忙看一下
...全文
132 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dewentan 2010-07-25
下载,好啊!
回复
black__art 2010-07-25
回复
zhang_jianzhi 2010-07-25
终于找到答案了

$q = "select firstname,lastname from users where uid = ?";
$stmt = mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'i',$id);
其中i是Integer就是$id的类型

字母 表示绑定的值类型
d Decimal
i Integer
b Blob (二进制类型)
s 所有其它类型

$q = "select uid,firstname from users where email=? AND pass=SHA1(?)";
$stmt = mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'ss',$e,$p);

其中ss,第一个s是$e的数据类型:其它类型(string) 第二个s是$p的数据类型:其它类型(string)
回复
zhang_jianzhi 2010-07-25

bool mysqli_stmt_bind_param ( mysqli_stmt stmt, string types, mixed &var1 [, mixed &...] )

第二个参数是第三个参数的类型吗?

那我这一个绑定参数是怎么写?

我有点晕,请指点,
回复
Dleno 2010-07-25
bool mysqli_stmt_bind_param ( mysqli_stmt stmt, string types, mixed &var1 [, mixed &...] )
注意参数
回复
Dleno 2010-07-25
手册:

<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* execute prepared statement */
$stmt->execute();

printf("%d Row inserted.\n", $stmt->affected_rows);

/* close statement and connection */
$stmt->close();

/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted.\n", $mysqli->affected_rows);

/* close connection */
$mysqli->close();
?>
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告