SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok guys, this problem has been driving me nuts for hours and I was hoping someone would have a solution for me. I'm sure it's a stupid mistake on my part, but any help would be appreciated.

    Basically this script will take input from the form within it and add it to the database. This isn't the full script because I'm having problem accessing the database, thought that should be taken care of first.

    The pass.inc included file contains the database connection as the variable $cnx. So I run the script, input my information and press Submit and I receive the following message from my browser:
    Warning: 0 is not a MySQL link index in /home/corby/justforwebmasters-www/be/admin/addauthor.php3 on line 33
    Couldn't select database.

    Any ideas? And thanks in advance!

    PHP Code:
    <?php

    include("/home/corby/php/pass.inc");
    include(
    "/home/corby/www/be/admin/functions.php3");
    $db_name "justforwebmasters_com";
    $table_name "Authors";

    function 
    printForm()
    {        
    // Prints out HTML form to allow user to create new author account.

        
    $scriptaddress "http://" getenv("HTTP_HOST") . getenv("SCRIPT_NAME");
        
        print 
    "<form action=\"$scriptaddress\" method=\"post\">\n";
        print 
    "<table align=\"center\" border=0 cellpadding=5 bgcolor=\"black\">\n";
        print 
    "<tr><td valign=\"top\" bgcolor=\"white\">Author Name:</td>\n";
        print 
    "<td valign=\"top\" bgcolor=\"white\"><INPUT TYPE=\"text\" NAME=\"authorname\" size=\"40\" value=\"$authorname\"></td></tr>\n";
        print 
    "<tr><td valign=\"top\" bgcolor=\"white\">Author Email:</td>\n";
          print 
    "<td valign=\"top\" bgcolor=\"white\"><INPUT TYPE=\"text\" NAME=\"authoremail\" size=\"40\" value=\"$authoremail\"></td></tr>\n";
        print 
    "<tr><td valign=\"top\" bgcolor=\"white\">Author Byline:</td>\n";
        print 
    "<td valign=\"top\" bgcolor=\"white\"><TEXTAREA NAME=\"authorbyline\" ROWS=\"3\" COLS=\"35\" value=\"$authorbyline\"></TEXTAREA></td></tr>\n";
        print 
    "<tr><td valign=\"top\" bgcolor=\"white\" colspan=\"2\">\n";
        print 
    "<center><input type=\"hidden\" name=\"page1\" value=\"1\"><input type=\"reset\"> <input type=\"submit\"></center>\n";
        print 
    "</td></tr></table></FORM>\n";
    }

    function 
    errorCheck()
    {
        print 
    "Checking for any errors";
    }

    function 
    databaseEntry()
    {        
    // Inserts records into database
        
    $db mysql_select_db($db_name$cnx) or die("Couldn't select database.");
        
    $sql "INSERT INTO $table_name (Name, Email, Byline) VALUES ($authorname$authoremail$authorbyline)";
        
    $result mysql_query($sql,$cnx) or die("Could not execute query. The error is on the SQL or connection line.");

        print 
    "Successful!";
    }

    if(
    $page1)
    {
        
    errorCheck();
        
    databaseEntry();
    }
    else
    {
        
    printHeader("JustForWebmasters.com: Add New Author""Add New Author");
        
    printForm();
        
    printFooter();
    }

    ?>
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  2. #2
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to connect to the db using code like
    mysql_connect("localhost", "username", "password");
    befopre you can select the db and execute commands
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  3. #3
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I know. My connection statement is in my "pass.inc" file, which is accessed through the include() statement.

    I know someone will ask this so I'll cover it now. The functions printHeader() and printFooter() are in the "functions.php3" file which is also accessed by the include() statement.
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  4. #4
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok well in that case i geuss you have made $cnx contain the connection right?
    You need to make that variable global so the function can access it add the code...
    global $cnx;
    as the first line of the fucntion that accesses the db.
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    because your global values are not visible to your functions, you have to insert them into your functions as parameter or with GLOBAL keyword. Check www.php.net


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
  •