我正在尝试在插入之前检查记录是否存在。我的检查代码是:
$query = "SELECT * FROM firm WHERE ( name = '" . mysql_real_escape_string( $name ) . "' AND area = '" . mysql_real_escape_string( $areaname ) . "' )";
$rs = mysql_query( $query );
if ( !$rs ) {
echo "The firm for this area already exist.";
trigger_error( mysql_error() );
} else {
echo "Saved.";
}
我的插入代码是:
$result = array();
mysqli_autocommit( $dbc, FALSE );
$sql1 ="INSERT INTO `firm`(name, area, VAT, active) VALUES ('$name', '$areaname', '$VAT', '$active')";
//echo $sql1;
$result[] = mysqli_query( $dbc, $sql1 ) ;
$sql2 = "INSERT INTO `area`(name, hub_name, fk_hub_id) VALUES ('$areaname'
,(SELECT `name` from hub WHERE name = '$hub_name')
,(SELECT `id` from hub WHERE name = '$hub_name'))
";
//echo $sql2;
$result[] = mysqli_query($dbc,$sql2);
$success = true;
if ( is_array( $results ) ) {
foreach( $results as $result ) {
if ( !$result ) {
$success = false;
}
}
if ( !$success ) {
mysqli_rollback( $dbc );
} else {
mysqli_commit( $dbc );
}
}
我的问题是我的错误消息不起作用。当我提交我的表格并且记录存在时,即使它没有插入,它也会说已保存。谁能告诉我哪里出错了?