0

I would like make INSERT INTO to two different MySQL tables. For example,

  1. insert customer details in tblcustomer
  2. insert customer address in tbladdress

Can I make it in

$result = mysql_query( "INSERT INTO tblcustomer( txtaccount_name,txtfirst_name,txtlast_name,txtemail ,intoffice_no,intfax,intmobile,intother_no,dtebirth_date) VALUES('$acc_name','$fname','$lname','$email', '$office','$fax_no','$mobile_no','$others_no','$date')"); $result1 = mysql_query( "INSERT INTO tbladdress( txtmailing_add,txtothers_add,txtmailing_street,txtothers_street, txtmailing_city,txtothers_city,txtmailing_state,txtothers_state, txtmailing_postcode,txtothers_postcode,txtmailing_country, txtothers_country) VALUES('$m_add','$o_add','$m_street','$o_street', '$m_city','$o_city','$m_state','$o_state', '$m_postcode','$o_postcode','$m_country', '$o_country')"); 
1
  • Never add php $values directly into MySQL statements, always use mysql_real_escape_string, or you'll suffer SQL injection attacks, see answers below. Commented Apr 17, 2011 at 0:02

2 Answers 2

1

Yes that would work, but you would have to execute two queries to the database, I suggest using PDO. It support transactions.

Sign up to request clarification or add additional context in comments.

1 Comment

Check here php.net/manual/en/book.pdo.php and follow the instructions, from start to finish, read carefully and you will be on your way in no time, make sure to use prepared statements as it ensures against SQL attacks, if you learn PDO well, it will greatly benefit your website in terms of almost everything.
1

You can insert with these queries but you should make connection between customer table and customer address table if you already did not done.

They should be connected by a foreign key or something like that if the engine is not innodb.

You should escape user input using mysql_real_escape_string

or better to use PDO.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.