Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    quick code check for php novice

    Assuming a valid array is passed to the function and the database connection is successful does everything look ok?

    code for functions.php:
    ---------------------

    PHP Code:
    <?php

    require_once('dbconnect.php');

    function 
    createset($setinfo)
    {

      
    $query "INSERT INTO sets ('setid', 'setname', 'setdesc') VALUES ('', $setinfo['setname'], $setinfo['setdesc'])";
        
    $result mysql_query($query) or die('Query failed: ' mysql_error());
        echo 
    "Set: $setinfo['setname'] created";

    }

    mysql_close($link);

    ?>

  • #2
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post
    Without really testing it, I think there are some issues with the quotes you have been using and perhaps the following would work better:

    PHP Code:
    <?php

    require_once('dbconnect.php');

    function 
    createset($setinfo)
    {

      
    $query "INSERT INTO sets (`setname`, `setdesc`) VALUES ('{$setinfo["setname"]}', '{$setinfo["setdesc"]}')";
        
    $result mysql_query($query) or die('Query failed: ' mysql_error());
        echo 
    "Set: {$setinfo["setname"]} created";

    }

    mysql_close($link);

    ?>
    Last edited by twomt; 12-31-2007 at 08:57 AM.

  • Users who have thanked twomt for this post:

    pphill2 (12-31-2007)

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Make sure you use braces around your array variables(or concatenate them) inside of a string:
    PHP Code:
    echo "Set: {$setinfo['setname']} created";
    // or
    echo 'Set: ' $setinfo['setname'] . ' created'
    Also, to optionally take debugging a little further you can use mysql_affected_rows() on MySQL UPDATE/INSERT/DELETEs to make sure the row was updated/inserted/deleted:
    PHP Code:
    if(mysql_affected_rows() == 1// 1 for this case; could be more with other queries
    {
        
    // echo success?
    }
    else
    {
        
    // error

    And if you need the ID from the auto_increment column you can get it with mysql_insert_id().

  • Users who have thanked Inigoesdr for this post:

    pphill2 (12-31-2007)

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    assuming setname and setdesc are strings, you'll want to put them through mysql_real_escape_string() before going anywhere near the database
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • Users who have thanked GJay for this post:

    pphill2 (12-31-2007)

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you, very helpful


  •  

    Posting Permissions

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