www.webdeveloper.com
Results 1 to 9 of 9

Thread: How do you insert Data into two tables Using one form?

  1. #1
    Join Date
    Feb 2009
    Posts
    21

    How do you insert Data into two tables Using one form?

    Hi, Does anybody know how to insert Data into two tables Using one html form?

  2. #2
    Join Date
    Nov 2006
    Location
    Springfield, MO
    Posts
    1,541
    Usually you can do it by just separating the insert statements with a semi-colon:

    INSERT INTO Table() VALUES(); INSERT INTO Table2() VALUES()
    Ryan Butler

    Ryan Butler.org

  3. #3
    Join Date
    Feb 2009
    Posts
    21
    Hi Ryan.

    $sql= "INSERT INTO flats (date_posted, type, location, rent, title, image, description, contactEmail, number)
    VALUES
    (NOW(),'$_POST[type]', '$_POST[location]','$_POST[rent]', '$_POST[title]', '$_POST[image]',
    '$_POST[description]','$_POST[contactEmail]','$_POST[number]')";

    "INSERT INTO users (email, password, conpass,) VALUES
    ('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

    I manage to insert into the flats table but not the users table.
    can you see anything wrong with this?

  4. #4
    Join Date
    Jun 2009
    Posts
    98
    Get rid of last comma after conpass: "INSERT INTO users (email, password, conpass,) VALUES
    ('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

    If this is an all or nothing proposition, I would recommend using Transactions assuming the database your working with supports it.
    Last edited by JavaServlet; 06-22-2009 at 05:46 PM.

  5. #5
    Join Date
    Feb 2009
    Posts
    21
    Hi, I removed the comma but still no luck. I only manage to insert into the flats table.

    $sql="INSERT INTO flats (date_posted, type, location, rent, title, image, description, contactEmail, number)
    VALUES
    (NOW(),'$_POST[type]', '$_POST[location]','$_POST[rent]', '$_POST[title]', '$_POST[image]', '$_POST[description]','$_POST[contactEmail]','$_POST[number]')";

    "INSERT INTO users (email, password, conpass) VALUES
    ('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

    any ide why?

  6. #6
    Join Date
    Jun 2009
    Posts
    98
    Quote Originally Posted by balamberas View Post
    Hi, I removed the comma but still no luck. I only manage to insert into the flats table.

    $sql="INSERT INTO flats (date_posted, type, location, rent, title, image, description, contactEmail, number)
    VALUES
    (NOW(),'$_POST[type]', '$_POST[location]','$_POST[rent]', '$_POST[title]', '$_POST[image]', '$_POST[description]','$_POST[contactEmail]','$_POST[number]')";

    "INSERT INTO users (email, password, conpass) VALUES
    ('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

    any ide why?
    I assume you have a reason for first statement in a variable and the second one is not. Do you have any error messages after you execute the PHP script? Next step is to test the SQL in the database with test values and see if it works and go from there. For example if using Oracle you can test the statements in SQL Plus.

  7. #7
    Join Date
    Feb 2009
    Posts
    21
    Hi, no error massages. if i would put the user table first then i manage to insert into that table but not the flats table. It will only insert into one table

    "I assume you have a reason for first statement in a variable and the second one is not".

    I dont know what you meen by this. and i inserted manuanly into the database and its inserting data fine.

    any other way to insert data into two tables using one html form??

  8. #8
    Join Date
    Apr 2008
    Posts
    190
    If your using php to execute the command then you have to separate the statements. I believe the mysql_query command in PHP can only execute one statement at a time. Just call mysql_query twice.

  9. #9
    Join Date
    Feb 2009
    Posts
    21
    yeah. i solved it today and was going to post the solution. i did this. $sql= INSERT INTO... and then $sql2=INSERT INTO...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles