再次先谢谢各位大佬!!
接下来直接看代码:
<?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')),则可以正确插入数据;
下面是我的表结构:
慕尼黑8549860
慕森王
明月笑刀无情