SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    INSERT INTO two tables

    Hi All,
    2nd day on this - it's driving me nuts!

    Trying to INSERT INTO two tables. id and linkid are numerical. Here's my latest attempt:

    Code:
     <?PHP
    $id = ($_POST['id']);
    $copyOne = ($_POST['copyOne']);
    $linkid = ($_POST['linkid']);
    $links = ($_POST['links']);
    $linkName = ($_POST['linkName']);
    include 'opendb.php';
    $mysql= "INSERT INTO about (id, copyOne) VALUES ($id, '$copyOne')";
    $results = mysql_query($mysql);
    $mysql2= "INSERT INTO links (linkid, links, linkName) VALUES ($linkid, '$links', '$linkName')";
    $results = mysql_query($mysql2);
    mysql_close();
    print "<html><body><center>";
    print "<p>Success! Here is the information you entered.<p>";
    print "Title : $linkName<br>";
    print "Link : $links<br>";
    print "Article : $copyOne<br>";
    print "</body></html>";
    ?>
    Just incase I made some stupid error on the form:
    Code:
    <form action="save_data.php" method="post">
    <input type="hidden" name="id" value="null">
    <input type="hidden" name="linkid" value="null">
    <table>
    <tr><td align="left">Page Title</td>
    <td><input type="text" name="linkName"></td>
    </tr>
    <tr><td align="left">link</td>
    <td><input type="text" name="links"></td>
    </tr>
    </table>
    <?php
    $oFCKeditor = new FCKeditor('copyOne') ;
    $oFCKeditor->BasePath = '/FCKeditor/';
    $oFCKeditor->Value = 'Default text in editor';
    $oFCKeditor->Create() ;
    ?>
    Connection is good, I can post to each table seperately. The above code gets no errors but does not post to the links table.

    TIA for any help!

    Steve

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    try changing

    PHP Code:
    $results mysql_query($mysql2); 
    to

    PHP Code:
    $results2 mysql_query($mysql2); 

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First of all to insert a record into a table you don't need to pass null for id, you can just skip it. Secondly, the id fields should be set to autoincrement, otherwise you have to specify them in the insert query.

    The code seems OK, so you should check if the id fields are set to autoincrement. If it's not the case then run the queries like this:
    PHP Code:
    mysql_query($mysql) or die(mysql_error()); 
    It'll output any query errors.
    Saul

  4. #4
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THANK YOU!
    For two days I have been reworking the code and it was a db problem. It was autoincrement. Boy do I feel stupid.

    Also thanks for the error checking tip should I always include it as a general practice or just when problems occur?

    Also been spending a lot of time looking around the site - AWSOME Site

    Thanks to all,

    Steve

  5. #5
    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)
    Quote Originally Posted by stevethenoob
    Also thanks for the error checking tip should I always include it as a general practice or just when problems occur?
    From The Pragmatic Programmer:

    Quote Originally Posted by Tip 32: Crash Early
    Crash, Don't Trash

    One of the benefits of detecting problems as soon as you can is that you can crash earlier. And many times, crashing your program is the best thing you can do. The alternative may be to continue, writing corrupted data to some vital database or commanding the washing machine into its twentieth consecutive spin cycle.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're welcome.

    I'd say you should always include this error checking. Though there are some points to consider as the visitors will see the nasty messages in case of an error. Personally, I use a logger class to write these messages to a log file instead of die() function.

    I agree with you, the site is really awesome.
    Saul


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
  •