SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding data to multiple tables with php

    Hello

    I'm having problems adding data from a php form to more than one mysql table at a time. Below is my php page, insert.php. I have no problems adding data to tblPerson, but when I simultaneously add to tblAsset I get a blank screen.

    Help would be most appreciated. seanj

    <HTML>
    <HEAD>
    <TITLE>Data entry</TITLE>
    </HEAD>
    <BODY>
    <P><?php $con = mysql_connect('localhost', 'username', 'password');
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db('newpolitics_com_pol', $con);

    $sql="INSERT INTO tblPerson (RelationshipID, FirstName, RestofName, StateID)
    VALUES
    ('$_POST[RelationshipID]','$_POST[FirstName]','$_POST[RestofName]','$_POST[StateID]')";
    if (!mysql_query($sql,$con))

    $sql="INSERT INTO tblAsset (AssetTypeID, Organisation, Location)
    VALUES
    ('$_POST[AssetTypeID]','$_POST[Organisation]','$_POST[Location]')";
    if (!mysql_query($sql,$con))

    {
    die('Error: ' . mysql_error());
    }
    echo "Data added";

    mysql_close($con);
    ?></P>
    <p><a href="/data">Enter more data</a></p>
    </BODY>
    </HTML>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (!mysql_query($sql,$con)) 
    This is the beginning of a conditional, but you've omitted the body, the stuff that should happen if it's true. You do it right the second time, but not the first.

    PHP Code:
    if (!mysql_query($sql,$con)) { 
        die(
    'Error: ' mysql_error());

    Once you've fixed that, you should change your queries to escape the user input. They will break if any of those fields contain single quotes, and you're also susceptible to people writing SQL into those fields to make your queries do something else entirely.

    http://us.php.net/mysql_real_escape_string

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan. Am learning.


Bookmarks

Posting Permissions

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