PHP Gurus, assemble!

Discussion in 'OT Technology' started by o2, May 26, 2006.

  1. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    I require help, I decided to learn php and Im stuck with something.

    Im building a test script that adds, deletes, and submits pieces of text along with author's name and email.
    Text is stored in 1 table, and author's info in another.
    I can get it to insert info into both tables, but I cant interconnect it, so it takes the assigned author id, and puts it into the "aid" field of the text table.
    So lets say "John Doe" is id=3, I need to put that 3 into the "aid" field of the text table.

    Here is what my insertion code looks like:
    Code:
    if ("SUBMIT" == "$submitjoke") {
    $sql1 = "insert into jokes set " . 
            "joketext = '$joketext', jokedate = curdate()";
            
    $sql2 = "insert into authors set " .     
            "name = '$name', email = '$email'";
            
            if (mysql_query($sql1) && mysql_query($sql2)) {
            echo ("<b>Your joke has been added!</b><br><i>" . $sql1 . " - " . $sql2 . "</i><br><br>"); }
            
            else { 
            echo("there was an error: " . mysql_error() . "<br>");
            }
            }
    Merging the 2 sql queries would probably be a good idea too, but I cant figure out how to do that either.

    Can anyone help?

    PS. I know this code is "insecure" but I havent got to the sql injection prevention.
     
  2. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    You'll need to utilize two queries, not one. You'll need to utilize the MySQL function mysql_last_id() which returns the last ID of the query last sent.

    And you should code with security in mind.
     
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,206
    Likes Received:
    516
    PHP gurus don't assemble. They interpret.
     

Share This Page