SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Adding Articles to a Database

    Hi all,

    I'm writing a content mangaement system, and am having some trouble with the following script. I keep getting an error saying there is a parse error on line 63. I have indicated which line this is as this script is huge.
    PHP Code:
    <?php

    /*

    addauthor.php - Used oto add a new author to the database.

    */

    include("docs/config.php");
    include(
    "/home/corby/phplib/pass.inc");
    $pagetitle "Add New Author";

    ?>
    <html>
    <head>
    <title><?php echo($pagetitle); ?></title>
    <?php

    include("$header");
    mysql_select_db(justforwebmasters_com$cnx)or die(mysql_error());

    if (
    $submit) {

    $id mysql_insert_id();

    $sql "INSERT INTO Authors2 SET " .
           
    "ID=$id, " .
           
    "FirstName='$fname', " .
           
    "LastName='$lname', " .
           
    "Email='$email', " .
           
    "Byline='$byline', " .
           
    "PictureFilename='$picfile', " .
           
    "HomepageURL='$hpurl', " .
           
    "HomepageTitle='$hptitle', " .
           
    "HomepageDesc='$hpdesc'";

    $newid mysql_last_insert_id();

          
    $ok mysql_query($sql);
          if (
    $ok) {
            
    Msg("<p>Author successfully added.</p>\n" .
                
    "<p>Add a new article for this author by clicking <a href=\"addarticle.php?aid=$id\">here</a>.</p>" .
                
    "<p>Add another author by clicking <a href=\"$PHP_SELF\">here</a>.</p>" .
                
    "<p>Return to the front page by clicking <a href=\"index.php\">here</a>.</p>");
          } else {
            
    errorMsg("<p>Error adding author to database:" .
                     
    mysql_error() . "</p>");
          }
    } else {

    ?>
    <form action="<?php echo($PHP_SELF); ?>" method="post">...
    Any help is greatly appreciated.
    Last edited by JustForWebmasters; Jan 6, 2002 at 20:35.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is the if() that goes with the

    PHP Code:
    else: // Allow the user to enter a new article 
    Also else: is so 1998, you should really be upgrading to else { }

    I see you are still writing your queries with a bunch of ". concatenation operators.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freddy,

    I deleted the original code and then re-inputted it because I got a whole bunch of errors, and forgot to the put the if ($submit): in.

    What do you have against else: and "concatenation operators?" I find that it keeps my query a little more organized and easier to edit.

    Thanks for your help.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Corbb O'Connor
    What do you have against else: and "concatenation operators?" I find that it keeps my query a little more organized and easier to edit.
    the alternative "if (blah): ... else: ... endif;" syntax is frowned upon. so, therefore you shouldn't use it. it makes things harder to keep track of. stick to curly braces.

    how does concatenating make your queries more organized??? it's not much different than just doing this (and it saves a few bytes )

    PHP Code:
    $sql "INSERT INTO table SET
        foo='bar'
        bar='foo'"

    and you don't have to worry about putting the dots. let me guess, do you use the dots b/c that's what you say Kevin doing in his tutorial?
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and you don't have to worry about putting the dots. let me guess, do you use the dots b/c that's what you say Kevin doing in his tutorial?
    I love it!
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you have against else: and "concatenation operators?" I find that it keeps my query a little more organized and easier to edit.
    Read the coding standards at http://pear.php.net I having nothing against them except for the fact that they were made fro php2 or php/fi, and ever since then the standard has been if() {} else {}
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I understand.

    Is it still okay to use them? Or will they soon be "phased out?" I just feel much more comfortable with using if ($var): else:.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  8. #8
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_insert_id()

    last_insertIn the PHP manual it says that mysql_insert_id(); can be used to increase the number of the field by one (as long as it is defined as an auto-increment).

    In my script (above--I'll edit when I'm done replying) I want to return the new id that MySQL generated. How do I do this? I tried using the last_insert_id(); function as explained in the manual, but can't get it to work.

    Any help is very much appreciated.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well first you use set ID = $id, so how can you be using an auto-incrementing field if you set the id explicitly.

    Omit the ID = $id part, MySQL will automatically set the ID if it auto-incrementing.

    the $id = mysql_insert_id() returns the last id used during an insert so you can't use it before you inser the record. You code should look more like.

    PHP Code:
    $sql "INSERT INTO Authors2 SET 
           FirstName='
    $fname', 
           LastName='
    $lname', 
           Email='
    $email', 
           Byline='
    $byline', 
           PictureFilename='
    $picfile', 
           HomepageURL='
    $hpurl', 
           HomepageTitle='
    $hptitle', 
           HomepageDesc='
    $hpdesc'";

    $newid mysql_insert_id(); 

    $newid will only hold a value after the record was inserted.


    AS for the if: else: I don't really understand your logic, especially since you do it properly here.

    PHP Code:

          
    if ($ok) {
            
    Msg("<p>Author successfully added.</p>\n" .
                
    "<p>Add a new article for this author by clicking <a href=\"addarticle.php?aid=$id\">here</a>.</p>" .
                
    "<p>Add another author by clicking <a href=\"$PHP_SELF\">here</a>.</p>" .
                
    "<p>Return to the front page by clicking <a href=\"index.php\">here</a>.</p>");
          } else {
            
    errorMsg("<p>Error adding author to database:" .
                     
    mysql_error() . "</p>");
          } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Louie,

    Not sure if you noticed, but I did change the if else statement back to the if (condition) {} format.

    Thanks for your help.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!


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
  •