PHP使用prepare(),insert数据时要注意的一点!!!
今天看了PHP防SQL注入,使用预处理prepare,但是我insert数据时,总是插不进去,但是select却可以,弄了很久终于知道原来问题在这里,先上代码
<?php header('content-type:text/html;charset=utf8');
//接收表单数据
//$username = $_POST['username'];
$conn = new mysqli('localhost','root','akagami-666','water');
if($conn -> connect_errno){ echo "连接失败".$conn -> connect_error; }
$sql = "insert into w_safe(name) values(?)";
// $sql = "select * from w_safe where name=?";
$stmt = $conn -> prepare($sql); $stmt -> bind_param("s",$username);
$username = $_POST['username'];
$stmt -> execute();
echo $stmt -> affected_rows;
$stmt -> close();
$conn -> close();
出问题的原因就是,我一开始是先接收post值,定义$username变量,后执行bind_param(),而这就导致无法插入数据,
必须要先写bind_param(),在定义里面的变量才行。
希望能帮助遇到同样问题的小伙伴!!!
PHP使用prepare(),insert数据时要注意的一点!!!的更多相关文章
- hive: insert数据时Error during job, obtaining debugging information 以及beyond physical memory limits
insert overwrite table canal_amt1...... 2014-10-09 10:40:27,368 Stage-1 map = 100%, reduce = 32%, Cu ...
- mysql中在表中insert数据时,有重复主键id时,变成update
MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDATE语法 例如: id name sex age 1 kathy male 23 2 Javer f ...
- peewee insert 数据时报错:'buffer' object has no attribute 'translate'
错误信息: "'buffer' object has no attribute 'translate'" 场景:使用peewee insert 数据时,BlobField 字段存储 ...
- mysql insert插入时实现如果数据表中主键重复则更新,没有重复则插入的四种方法
[CSDN下载] Powerdesigner 设计主键code不能重复等问题 [CSDN博客] Oracle中用一个序列给两个表创建主键自增功能的后果 [CSDN博客] MySQL自增主键删除后重复问 ...
- mysql插入数据时 insert IGNORE、ON DUPLICATE KEY UPDATE、replace into
转: mysql insert时几个操作DELAYED .IGNORE.ON DUPLICATE KEY UPDATE的区别 博客分类: mysql基础应用 mysql insert时几个操作DE ...
- MySql LAST_INSERT_ID 【插入多条数据时】
LAST_INSERT_ID 自动返回最后一个 INSERT 或 UPDATE 操作为 AUTO_INCREMENT 列设置的第一个发生的值. 参考这里 The ID that was generat ...
- SQLSERVER:大容量导入数据时保留标识值 (SQL Server)
从MSDN上看到实现大容量导入数据时保留标识值得方法包含三种: MSDN链接地址为:https://msdn.microsoft.com/zh-cn/library/ms178129.aspx 感觉M ...
- 转 SSIS处理导入数据时, 存在的更新, 不存在的插入
SSIS处理导入数据时, 存在的更新, 不存在的插入 分类: DTS/SSIS2006-09-10 12:43 18185人阅读 评论(22) 收藏 举报 ssissql servermicrosof ...
- 【JDBC】向数据表插入数据时,自动获取生成的主键
数据表设计时,一般都会有一个主键(Key)(自己指定),有时也可以使用联合主键: 有许多数据库提供了隐藏列为表中的每行记录分配一个唯一键值(如:rowid): 当我们没有指定哪一列作为主键key时,数 ...
随机推荐
- Linux探索之路1---CentOS入坑笔记整理
前言 上次跟运维去行方安装行内环境,发现linux命令还是不是很熟练.特别是用户权限分配以及vi下的快捷操作.于是决定在本地安装一个CentOS虚拟机,后面有时间就每天学习一点Linux常用命令. 作 ...
- JavaScript中Array数组的方法
查找: indexOf.lastIndexOf 迭代:every.filter.forEach.map.somereduce.reduceRight 用法: /* 1 查找方法: * arr.inde ...
- Python之socketserver模块和验证客户端链接的合法性
验证客户端链接的合法性 分布式系统中实现一个简单的客户端链接认证功能 #_*_coding:utf-8_*_ from socket import * import hmac,os secret_ke ...
- html标记语言 --框架
html标记语言 --框架 六.框架 1.什么是框架 框架将浏览器划分成不同的部分,每一部分加载不同的网页 实现同一浏览器窗口中加载多个页面的效果. 语法格式<frameset>..... ...
- [项目推荐] Corcel 让你在 WordPress 中使用 Laravel
你想过可以在 WordPress 中使用 Laravel 或者任意一种 PHP 框架吗? Corcel 可以帮你实现! 开发网站应用就应该是快捷并有趣的.当然了,每个应用都会有它自己的需求和生命周期. ...
- [LeetCode] Bold Words in String 字符串中的加粗单词
Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any le ...
- [LeetCode] Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- [LeetCode] Shopping Offers 购物优惠
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- 机器学习技法:08 Adaptive Boosting
Roadmap Motivation of Boosting Diversity by Re-weighting Adaptive Boosting Algorithm Adaptive Boosti ...
- Joomla!3.7.0 Core SQL注入漏洞动态调试草稿
参考joolma的mvc框架讲解:http://www.360doc.com/content/11/1219/18/1372409_173441270.shtml 从这个页面开始下断点:Joomla_ ...