再次先谢谢各位大佬!!接下来直接看代码:<?php
include 'mysql_ini.php';
$sql_select = "select * from word where word = ?";
$sql_inset = "insert into word(word,explain) values('ssss','sss')";
$word = 'passwor';
$explain = '密码';
try{
$stmt_select = $pdo -> prepare($sql_select);
$stmt_select -> setFetchMode(PDO::FETCH_ASSOC);
$stmt_select -> execute(array($word));
$rows_select = $stmt_select -> fetchAll();
if($rows_select){
$error = '该单词已存在!';
echo $error;
}else{
$stmt_insert = $pdo -> prepare($sql_inset);
$stmt_insert -> execute(array($word,$explain));
$rows_insert = $stmt_insert -> rowCount();
echo $rows_insert;
}
}catch(PDOException $e){
}
?>
如上代码所示:$sql_inset变量中的SQL语句如果像上面那样则无法插入数据,但如果去除指定列名(改为:insert into word values('ssss','sss')),则可以正确插入数据;下面是我的表结构:
5 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
explain是关键词,不适合当字段名,可能是这个导致错误。尝试在explain两边加上这个`,不是单引号,是键盘上方数字键一行最左边的那个按键。
- 5 回答
- 0 关注
- 558 浏览
添加回答
举报
0/150
提交
取消