EasyUI Forum
May 09, 2024, 07:16:04 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Saving into multiple table  (Read 4256 times)
karogel
Newbie
*
Posts: 28


View Profile
« on: November 01, 2015, 06:02:54 PM »

Please help...how to create save.php file with multiple table, say save to personalinfo.php and address.php

I am following or using this simple code for saving to single table:

<?php

$firstname = htmlspecialchars($_REQUEST['firstname']);
$lastname = htmlspecialchars($_REQUEST['lastname']);
$phone = htmlspecialchars($_REQUEST['phone']);
$email = htmlspecialchars($_REQUEST['email']);

include '../conn.php';

$sql = "insert into users(firstname,lastname,phone,email) values('$firstname','$lastname','$phone','$email')";
$result = @mysql_query($sql);
if ($result){
   echo json_encode(array(
      'id' => mysql_insert_id(),
      'firstname' => $firstname,
      'lastname' => $lastname,
      'phone' => $phone,
      'email' => $email
   ));
} else {
   echo json_encode(array('errorMsg'=>'Some errors occured.'));
}
?>
Logged
dies_felices
Newbie
*
Posts: 24


View Profile
« Reply #1 on: November 03, 2015, 09:24:54 AM »

Hi,

The simplest answer is a mysqli multi-line query:

Code:

$conn = new mysqli('host','user','pword','DB');

$sql = <<<EOT
insert into personalinfo(firstname,lastname) values('$firstname','$lastname');
insert into address(phone,email) values('$phone','$email');
EOT;

$result = $conn->multi_query($sql)


You would be much better off using stored procedures to do this as that is much safer.
« Last Edit: November 03, 2015, 09:30:36 AM by dies_felices » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!