dstd2129 2015-03-29 12:50
浏览 132
已采纳

mysqli_stmt :: bind_param():类型定义字符串中的元素数与绑定变量警告数不匹配

My codes for the prevention of SQL injection isn't working. Can anyone help me?

I'm receiving this warning: Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables.

Thanks.

$mysqli = new mysqli('localhost', 'root', '', 'Muproj');    
$query="INSERT INTO tblmember VALUES (':id', ':uname' , ':passwrd' , ':name' , ':surname' ,':0' )";
$stmt = $mysqli->prepare($query);
$stmt->bind_param(':id', $newid);
$stmt->bind_param(':uname', $C_uname);
$stmt->bind_param(':passwrd', $C_passwrd);
$stmt->bind_param(':name', $C_name);
$stmt->bind_param( ':surname', $C_surname);
$stmt->bind_param(':0', '0');
$stmt->execute();
$result=mysql_query($stmt);
  • 写回答

1条回答 默认 最新

  • duan0414 2015-03-29 13:36
    关注

    you appear to be mixing PDO syntax with MySQLi syntax.

    Please read up on http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

    I do not use PDO myself much, but I do use MySQLi, so your references as :something are PDO declarations, but the SQL functions you use are MySQLi.

    With a MySQLi bind_param function you need to add all the data into an array-like row (it may actually be an array), but preceeded by a declaration of types, as in String, integer, Double and Blob.

    I have rewritten your code in the MySQLi form:

    $mysqli = new mysqli('localhost', 'root', '', 'Muproj');    
    $query="INSERT INTO tblmember VALUES (?, ? , ? , ? , ? ,? )";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("issssi", $newid, $C_uname, $C_passwrd, $C_name, $C_surname, $zero)
    $stmt->execute();
    //$result=mysqli_query($stmt);
    

    You need to do some serious research as to the differences of approach and formatting and functionality between MySQLi and PDO. Also be careful to maintain ALL your MySQL as MySQLi , as for example your $result was using the deprectated MySQL query statement.

    PS: I would also suggest for clarity and forward compatibility that your INSERT statement in the SQL reads as:

     INSERT INTO table_name (column_names1, column_name2, column_names3, ...) VALUES (?,?,?, ...)
    

    So you and the SQL can clearly see which values are plugged into which columns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?