检查已注册的数据,然后插入另一个表

时间:2013-12-17 09:12:44

标签: php html mysql sql post

我想请你帮我一些PHP脚本。

我有两张表 Registered_Bus 表和 Bus_Route表

所以我想通过检查Bus是否已在Registered_bus表中注册来插入 Bus_Route表中的数据,否则提示用户该总线未注册。

下面是插入Bus_route信息的PHP代码

// Insert data into mysql

$sql = "INSERT INTO $tbl_name VALUES('$BID', '$Route', '$Driver_Name',
             '$Driver_phone_no', '$Time_in', '$Time_out', '$Date' , '$Comment')";
$result = mysql_query($sql) or die(mysql_error());

1 个答案:

答案 0 :(得分:1)

$check = "select count('X') as bus_status from Registered_Bus where id_Registered_Bus = '".$BID."'";
$result_check = mysql_query($check);
$row_check = mysql_fetch_assoc($result_check);
if($row_check['bus_status'] == 1){
    $sql = "INSERT INTO $tbl_name VALUES('$BID', '$Route', '$Driver_Name',
                 '$Driver_phone_no', '$Time_in', '$Time_out', '$Date' , '$Comment')";
    $result = mysql_query($sql) or die(mysql_error());
} else {
    echo "not registered";
}