我正在尝试将数据插入table_1,然后如果新的插入ID在第二个表上不可用,则插入第二个表,然后更新它。贝娄是我的代码请告诉我我做错了什么。
<?php
$name='Name';
$pass='Passsword';
$rid='FR200000';
$sql = "INSERT INTO table_1 (id,name,pass) VALUES('".$rid."','".$name."','".$pass."')";
$res = mysql_query($sql);
if(!$res){
echo'Failed to insert';
}else{
$sql = "SELECT id FROM site_settings WHERE id = '".$rid."'";
$res = mysql_query($sql);
$get_id = mysql_fetch_assoc($res);
if (!$get_id==$rid){
$site_url = 'www.example.com';
$site_email ='example@mysite.com';
$sql = "INSERT INTO site_settings (id,site_url,site_email) VALUES('".$rid."','".$site_url."','".$site_email."')";
$res = mysql_query($sql);
if(!$res) return 1;
return 99;
}
if ($get_id==$rid){
$sql = "UPDATE site_settings SET site_url = '" . $site_url . "', site_email = '" . $site_email . "' WHERE ID = '".$rid."'";
$res = mysql_query($sql);
if(!$res) return 1;
return 99;
}
?>
答案 0 :(得分:2)
<强> mysql_query() 强>
对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他语句返回 结果集
$sql = "SELECT id FROM site_settings WHERE id = '".$rid."'";
$get_id = mysql_query($sql);
您不会直接将结果集与$rid
if (!$get_id==$rid){
您需要先获取数据
$row = mysql_fetch_assoc($res);
$get_id=$row['id'];// fetch data
然后比较
if (!$get_id==$rid){
// YOUR code
注意: - 不推荐使用mysql,而是使用mysqli或PDO