www.webdeveloper.com
Results 1 to 3 of 3

Thread: One insert.php for two tables

  1. #1
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    140

    One insert.php for two tables

    I have a database with two tables. There are four fields in table 1 that are duplicated in table 2. I would like to be able to enter that information only once and have it inserted into both tables. Is that possible?

    For example, of the 12 fields in table 1, four are also in table 2. There are 13 other fields in table 2 that are entered separately. Below is the code for table 1. Can it be modified to also enter the four common fields into table 2 as well? The four common fields are progname, refurl, affurl and country.

    [code]<?php
    include("dbinfo.inc.php");

    $progname=mysql_real_escape_string($_POST['progname']);
    $parent=mysql_real_escape_string($_POST['parent']);
    $uname=mysql_real_escape_string($_POST['uname']);
    $pass=mysql_real_escape_string($_POST['pass']);
    $refurl=mysql_real_escape_string($_POST['refurl']);
    $affurl=mysql_real_escape_string($_POST['affurl']);
    $parentname=mysql_real_escape_string($_POST['parentname']);
    $parenturl=mysql_real_escape_string($_POST['parenturl']);
    $netname1=mysql_real_escape_string($_POST['netname1']);
    $netnameurl1=mysql_real_escape_string($_POST['netnameurl1']);
    $netname2=mysql_real_escape_string($_POST['netname2']);
    $netnameurl2=mysql_real_escape_string($_POST['netnameurl2']);
    $netname3=mysql_real_escape_string($_POST['netname3']);
    $netnameurl3=mysql_real_escape_string($_POST['netnameurl3']);
    $hosting=mysql_real_escape_string($_POST['hosting']);
    $country=mysql_real_escape_string($_POST['country']);
    $notes=mysql_real_escape_string($_POST['notes']);

    $query = "INSERT INTO programs
    VALUES (
    '$progname',
    '$parent',
    '$uname',
    '$pass',
    '$refurl',
    '$affurl',
    '$parentname',
    '$parenturl',
    '$netname1',
    '$netnameurl1',
    '$netname2',
    '$netnameurl2',
    '$netname3',
    '$netnameurl3',
    '$hosting',
    '$country',
    '$notes'
    )";

    mysql_query($query)or die("Unable to insert data" . mysql_error());

    echo "<center><table><tr><td>";
    echo "<h2>Record Added</h2>";
    echo "</td></tr></table></center>";
    mysql_close();
    ?>[code]

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    The Insert acts only in one table at a time.
    My question is what is the relationship between the two tables. If there isn't then you just need to run another query for the second table.
    If however the two table are related via a unique key then I see no reason for duplicating data.

    Just a tip:

    include("dbinfo.inc.php");
    $Fields=array('progname', 'parent', ...., 'notes'); //put all fields name in this array

    $QStr="";
    foreach($Fields as $field){
    $val=isset($_POST[$field])?addslashes($_POST[$field]):"";
    $QStr.=($QStr!="")?",":"";
    $QStr.="'$val'";
    }

    $query = "INSERT INTO programs VALUES ($QStr)";

    mysql_query($query)or die("Unable to insert data" . mysql_error());

    echo "<center><table><tr><td>";
    echo "<h2>Record Added</h2>";
    echo "</td></tr></table></center>";
    mysql_close();

  3. #3
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    140
    The two tables are related by progname. One contains a list of affiliate programs, including hosting programs, and their related data. The other contains details about each hosting provider. I use output for the programs table on one website and the output from webhosts on another. Just need to include the referral url from programs with the data from webhosts.

    Hope that makes sense.

    In other words, match data by the common unique field progname and include the referral url from programs in output for webhosts.

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