在单个PHP中在多个表(父/子)中插入数据时出现问题

时间:2018-02-16 06:18:12

标签: php sql android-studio wampserver multi-table

父表 - login_main_db

子表 - rp_main_db

PHP文件 - owner_register.php

使用wampserver

尝试做什么 - 我正在尝试同时向父表(login_main_db)和子表(rp_main_db)插入数据。

问题 - 当我执行文件时,数据被插入父表(login_main_db)但是无法将数据添加到子表(rp_mian_db)

请在几天内帮助陷入困境..

请在下面找到.php文件和.html

注意: -

我们从html传递给php的变量 只有 $ latitude $ longitude double $ PId int ...如果有帮助..其余变量是文本..

<?php

require "init.php";


$username = $_POST["username"];
$password = $_POST["password"];
$businessname = $_POST["businessname"];
$businesstype = $_POST["businesstype"];
$address = $_POST["address"];
$area = $_POST["area"];
$city = $_POST["city"];
$state = $_POST["state"];
$country = $_POST["country"];
$pin = $_POST["pin"];
$email = $_POST["email"];
$phone1 = $_POST["phone1"];
$phone2 = $_POST["phone2"];
$businessdetail = $_POST["businessdetail"];
$website = $_POST["website"];
$homedelivery = $_POST["homedelivery"];
$workinghours = $_POST["workinghours"];
$latitude = $_POST["ownerlatitude"];
$longitude = $_POST["longitude"];




$sql = "select * from login_main_db where Username like '".$username."';";

$result = mysqli_query($con,$sql);
$response = array();

if(mysqli_num_rows($result)>0)
{
    $code = "reg_failed";
    $message = "User already exist.....";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode($response);
}
else
{

    if($businesstype == "Resturant/Pub")
    {


    $sql_login = "INSERT INTO login_main_db (Username, Password, 
    BusinessName) VALUES 
    ('$username','$password','$businessname')";
    $result = mysqli_query($con,$sql_login);


    $last_id = $con->insert_id;

    $sql_main = "INSERT INTO rp_main_db         
    (PId, BusinessName,BusinessType, Address, Area, City, State, Country, 
    Pin, Email, Phone1, Phone2, BusinessDetail, Website,
    HomeDelivery, WorkingHours, OwnerLatitude, OwnerLongitude) VALUES 
   ('$last_id','$businessname','$businesstype','$address','$area','$city'
   ,'$state','$country','$pin','$email','$phone1','$phone2',    
  '$businessdetail','$website','$homedelivery','$workinghours',
  '$latitude','$longitude')";

    $result1 = mysqli_query($con,$sql_main);

    $code = "reg_success";
    $message = "Thank you for register with us. Now you can login.... " ;
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode($response);
    }



}


mysqli_close($con);

?>                                      

以下是HTML文件

<html>
<body>
<form action="owner_register.php" method="post">
<table>                                                                                                                                                                                                                                                                                                                             

<tr>                                                                                                                                                
<td> Business name :</td><td><input type="text" name="businessname"/></td>                                                                                                                                                      
</tr>

<tr>
<td> Username :</td><td><input type="text" name="username"/></td>
</tr>

<tr>
<td> Password :</td><td><input type="text" name="password"/></td>
</tr>
<tr>
<td> Business type :</td><td><input type="text" name="businesstype"/></td>
</tr>
<tr>
<td> Address :</td><td><input type="text" name="address"/></td>
</tr>

<tr>
<td> Area :</td><td><input type="text" name="area"/></td>
</tr>

<tr>
<td> City :</td><td><input type="text" name="city"/></td>
</tr>

<tr>
<td> State :</td><td><input type="text" name="state"/></td>
</tr>

<tr>
<td> Country :</td><td><input type="text" name="country"/></td>
</tr>

<tr>
<td> Pin :</td><td><input type="text" name="pin"/></td>
</tr>

<tr>
<td> Email :</td><td><input type="text" name="email"/></td>
</tr>

<tr>
<td> Phone1 :</td><td><input type="text" name="phone1"/></td>
</tr>

<tr>
<td> Phone2 :</td><td><input type="text" name="phone2"/></td>
</tr>

<tr>
<td> Business Detail :</td><td><input type="text" name="businessdetail"/>
</td>
</tr>

<tr>
<td> Website :</td><td><input type="text" name="website"/></td>
</tr>

<tr>
<td> HomeDelivery :</td><td><input type="text" name="homedelivery"/></td>
</tr>

<tr>
<td> Working hours :</td><td><input type="text" name="workinghours"/></td>
</tr>

<tr>
<td> Latitude :</td><td><input type="double" name="latitude"/></td> 
</tr>

<tr>
<td> Longitude :</td><td><input type="double" name="longitude"/></td>
</tr>


<tr>
<td> <input type="submit" Value="Register"/></td>
</tr>


</table>
</form>
</body>


</html>

0 个答案:

没有答案