vendredi 3 novembre 2017

How to insert data in multiple tables using a prepared statement in PHP?

I have two tables which are billing_info, shipping_info. billing_id is Primary key and billing_id is a foreign key in shipping_info.

I have billing_info address form. The user will fill the billing info, After filling info there is a checkbox which is called as shipping address is different. If any user checked that checkbox then it will ask for the shipping information. After filling the shipping information according to the data billing info will store in the billing_info table and shipping_info will store in the shipping info table.

If the user is not checked the checkbox than only billing info will store in the billing info table.

<input type="checkbox" name="shipping_status" value="1">

I am able to insert the data in the database only for billing_info if the user does not check the checkbox but not able to insert the data after checked the checkbox. There might be some issue with the query. I tried query is

1)  START TRANSACTION;
    INSERT INTO table1 (column name)VALUES (values);
    INSERT INTO table2 (column name) VALUES (values);
    COMMIT;

 2) BEGIN;
    INSERT INTO table1 (column name)VALUES (values);
    INSERT INTO table2 (column name) VALUES (values);
    COMMIT; 

but still not able to insert the data. Would you help in out in this?

is this right way to store int user information?

if (isset($_POST['submit'])){
  $b_firstname=$conn->real_escape_string(trim($_POST['b_firstname']));
  $b_lastname=$conn->real_escape_string(trim($_POST['b_lastname']));
  $b_address =$conn->real_escape_string(trim($_POST['b_address']));
  $b_country=$conn->real_escape_string(trim($_POST['b_country']));
  $b_state=$conn->real_escape_string(trim($_POST['b_state']));
  $b_city=$conn->real_escape_string(trim($_POST['b_city']));
  $b_pincode=$conn->real_escape_string(trim($_POST['b_pincode']));
  $b_mobileno=$conn->real_escape_string(trim($_POST['b_mobileno']));
  //$login_user_id=$conn->real_escape_string(trim($_POST['login_user_id']));
  //echo $shipping_status=$conn->real_escape_string(trim($_POST['shipping_status']));

  /*Shipping address*/
  $s_firstname=$conn->real_escape_string(trim($_POST['s_firstname']));
  $s_lastname=$conn->real_escape_string(trim($_POST['s_lastname']));
  $s_address =$conn->real_escape_string(trim($_POST['s_address']));
  $s_country=$conn->real_escape_string(trim($_POST['s_country']));
  $s_state=$conn->real_escape_string(trim($_POST['s_state']));
  $s_city=$conn->real_escape_string(trim($_POST['s_city']));
  $s_pincode=$conn->real_escape_string(trim($_POST['s_pincode']));
  $s_mobileno=$conn->real_escape_string(trim($_POST['s_mobileno']));

if (empty($conn->real_escape_string(trim($_POST['shipping_status'])))) {
  $shipping_status=0;
}
else{
$shipping_status=1;
}

if ($shipping_status==0) {

echo $sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
//  print_r($sql);
  $stmt = $conn->prepare($sql);
  $stmt->bind_param("ssssssiiiis", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added);

}
else{
  // prepare and bind
  echo $sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);INSERT INTO shipping_info (s_firstname, s_lastname, s_address, s_country, s_state, s_city, s_pincode, s_mobileno,b_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";

  $stmt = $conn->prepare($sql);
  $stmt->bind_param("ssssssiiiisssssssiii", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added,$s_firstname, $s_lastname, $s_address, $s_country, $s_state, $s_city, $s_pincode, $s_mobileno,LAST_INSERT_ID());

}


  $stmt->execute();
  $stmt->close();

  }

//redirect code
//header('Location: contactus.php');
  $conn->close();
}




Aucun commentaire:

Enregistrer un commentaire