如果我理解你的问题,你想插入姓氏的值,即使它没有设置。
$db->exec('INSERT INTO `customer`(`name`,`surname`) VALUES('.$dbh->quote($name).','.$dbh->quote($surname).')');
即使代码为空,代码也会插入该值。
编辑:
,如果你想要得到的响应,使用此:
if($db->exec('INSERT INTO `customer`(`name`,`surname`) VALUES('.$dbh->quote($name).','.$dbh->quote($surname).')')) {
echo 'Success';
} else {
echo 'fail';
}
编辑2:
/* if name is not set, insert ONLY surname */
if(!isset($name)) {
if($db->exec('INSERT INTO `customer`(`surname`) VALUES('.$db->quote('surname').')')){
echo 'success';
} else {
echo 'fail';
}
/* if surname is not set, insert ONLY name */
} elseif(!isset($surname)) {
if($db->exec('INSERT INTO `customer`(`name`) VALUES('.$db->quote('name').')')){
echo 'success';
} else {
echo 'fail';
}
/* if both are set, insert both */
} else {
if($db->exec('INSERT INTO `customer`(`name`,`surname`) VALUES('.$db->quote('name').','.$db->quote('surname').')')){
echo 'success';
} else {
echo 'fail';
}
}
建立一个查询作为一个变量。 –
使用if条件并根据if条件评估使用其中一个查询。 –
请阅读'INSERT'语法手册http://dev.mysql.com/doc/refman/5.7/en/insert.html然后重新开始 – RiggsFolly