如果第一个查询插入了数据,我想插入第二个查询的数据

时间:2016-02-20 10:04:14

标签: php mysqli

我正在尝试插入数据。但我得到一个错误,我无法解决任何帮助将非常感激

  

错误:警告:mysqli_num_rows()期望参数1为   mysqli_result,boolean given

$insert_qryy = mysqli_query($con,"Insert into user_register(name,email,phone,cphone,address,city,country,dob)
values('".$name."','".$email."','".$phone."','".$cphone."','".$address."','".$cityr."','".$country."','".$dob."')")or die(mysqli_error());

if ($insert_qryy->num_rows==0)
{   
    echo "Error";
}
else
{      
    $handler = mysqli_query($con,"INSERT INTO `addproperty`(`purpose`, `property_type`, `city`, `title`, `description`,         
`property_price`, `land_area`, `expires_after`, `property_img`) VALUES('".$purpose."','".$type."','".$city."','".$title."','".$desc."','".$price."',
'".$landarea."','".$expiry."','".$im."')") or die (mysqli_error());
}

它正在输入$ insert_qryy数据但是第二个语句不起作用如果语句变为false我希望我能在这里得到我的解决方案

3 个答案:

答案 0 :(得分:2)

正如你所说 - No i want to insert the data of second query if first query have inserted its data 。检查一次: -

$insert_qryy = mysqli_query($con,"Insert into user_register(name,email,phone,cphone,address,city,country,dob) values('".$name."','".$email."','".$phone."','".$cphone."','".$address."','".$cityr."','".$country."','".$dob."')")or die(mysqli_error($con));

if ($insert_qryy)
{   
  $handler = mysqli_query($con,"INSERT INTO `addproperty`( `purpose`, `property_type`, `city`, `title`, `description`,`property_price`, `land_area`, `expires_after`, `property_img`) VALUES  ('".$purpose."','".$type."','".$city."','".$title."','".$desc."','".$price."','".$landarea."','".$expiry."','".$im."')") or die (mysqli_error($con));
} else{
   echo "First Insert not executed properly";
}

注意: - 问题是Insert查询根据执行的查询返回boolen值true或false。因此,您无法在mysqli_num_rows()中直接使用它,因为它要求result-set object作为参数而不是boolean值。

答案 1 :(得分:1)

要检查INSERT查询状态,您可以使用mysqli_affected_rowsmysqli_insert_id()

$lastid = mysqli_insert_id($link);

修改示例:

$insert_qryy = mysqli_query($con,"Insert into user_register(name,email,phone,cphone,address,city,country,dob) 
values ('".$name."','".$email."','".$phone."','".$cphone."','".$address."','".$cityr. "','".$country."','".$dob."')") 
or die(mysqli_error()); 

$lastid = mysqli_insert_id($con);

if (intval($lastid) <= 0) 
{ 
    echo "Error"; 
}
else{
    // your second query or success.
}

答案 2 :(得分:0)

使用 mysqli_affected_rows()函数检查数据是否已成功插入表格

$insert_qryy = mysqli_query($con,"Insert into user_register(name,email,phone,cphone,address,city,country,dob) values('".$name."','".$email."','".$phone."','".$cphone."','".$address."','".$cityr."','".$country."','".$dob."')")or die(mysqli_error($con));

if (mysqli_affected_rows() > 0) //use this to check if data is inserted successfully into table
{   
    $handler = mysqli_query($con,"INSERT INTO `addproperty`( `purpose`, `property_type`, `city`, `title`, `description`,`property_price`, `land_area`, `expires_after`, `property_img`) VALUES  ('".$purpose."','".$type."','".$city."','".$title."','".$desc."','".$price."','".$landarea."','".$expiry."','".$im."')") or die (mysqli_error($con));
} 
else
{
    echo "something went wrong!!! at first query insertion";
}