Results 1 to 12 of 12

Thread: MySQL Help

  1. #1
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    307

    MySQL Help

    I was reading a php book and I attepted to create a database. I took the first beginning example and I was able to create it. Then I made a form. Then I had to make a "HandleForm.php" but I couldn't get it to work. Could anybody helpme on what is wrong please. I get the (The query could not be executed) message when I pass information trough the form to be stored in the database. Below is the script. Ofcourse I was using the right username and password. Also where it says "mysql" instead of "localhost" I am required to use that by my ISP. Thanks.





    <HTML>
    <HEAD>
    <TITLE>Inserting Data into a Database</TITLE>
    </HEAD>
    <BODY>
    <?php
    /* This page receives and handles the data generated by "form.html". */
    // Trim the incoming data.
    $Array["FirstName"] = trim ($Array["FirstName"]);
    $Array["LastName"] = trim ($Array["LastName"]);
    $Array["Email"] = trim ($Array["Email"]);
    $Array["Comments"] = trim ($Array["Comments"]);

    // Set the variables for the database access:
    $Host = "mysql";
    $User = "username";
    $Password = "password";
    $DBName = "NewDatabase";
    $TableName = "Feedback";

    $Link = mysql_connect ($Host, $User, $Password);
    $Query = "INSERT into $TableName values ('0', '$Array[FirstName]', '$Array[LastName]', '$Array[Email]', '$Array[Comments]')";
    print ("The query is:<BR>$Query<P>\n");
    if (mysql_db_query ($DBName, $Query, $Link)) {
    print ("The query was successfully executed!<BR>\n");
    } else {
    print ("The query could not be executed!<BR>\n");
    }
    mysql_close ($Link);
    ?>
    </BODY>
    </HTML>

  2. #2
    Join Date
    Sep 2002
    Location
    Santiago de Queretaro, Queretaro de Arteaga, Mexico
    Posts
    21
    Maybe if you use the next syntax at your query statement it could work:

    $Query = "INSERT into ".$TableName." values ('0', '".$Array["FirstName"]."', '".$Array["LastName"]."', '".$Array["Email"]."', '".$Array["Comments"]."')";

  3. #3
    Join Date
    Jul 2003
    Location
    East TN
    Posts
    568
    Try using <?php and ?> tags....

    Take care,
    Lori

  4. #4
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    A few tips.

    First ... always use error checking, never assume that things went okay, especially when its something external (like a database server). For mysql, there is mysql_error() that returns the error from MySQL.

    Second ... use syntax highlighting ... it helps avoid common syntax errors (from typos) :

    PHP Code:
    <HTML>
    <HEAD>
    <TITLE>Inserting Data into a Database</TITLE>
    </HEAD>
    <BODY>
    <?php
    /* This page receives and handles the data generated by "form.html". */
    // Trim the incoming data.
    $Array["FirstName"] = trim ($Array["FirstName"]);
    $Array["LastName"] = trim ($Array["LastName"]);
    $Array["Email"] = trim ($Array["Email"]);
    $Array["Comments"] = trim ($Array["Comments"]);

    // Set the variables for the database access:
    $Host "mysql";
    $User "username";
    $Password "password";
    $DBName "NewDatabase";
    $TableName "Feedback";

    $Link mysql_connect ($Host$User$Password);
    $Query "INSERT into $TableName values ('0', '$Array[FirstName]', '$Array[LastName]', '$Array[Email]', '$Array[Comments]')";
    print (
    "The query is:<BR>$Query<P>\n");
    if (
    mysql_db_query ($DBName$Query$Link)) {
    print (
    "The query was successfully executed!<BR>\n");
    } else {
    print (
    "The query could not be executed!<BR>\n");
    }
    mysql_close ($Link);
    ?>
    </BODY>
    </HTML>
    There could be many reasons your processing script isn't working. First of all, $Host = "mysql"; should be $Host = "localhost"; if your mysql server is the same computer as your web server.

    Secondly, you never select the database ... although you assign it to a variable ($DBName).

    Thirdly, $Array does not hold incoming data from a form. The data will either be in $_GET or $_POST.

    Try and get these things sorted out and then it might work

    <plug type="shameless">I have a tutorial that's slightly more detailed that shows you how to do the same thing. Check it out at www.meidomus.com </plug>

    Would personally like to know what book this example is from

    Good luck

  5. #5
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    307
    Hello

    I tried pretty much everything above. Finally I used one of those mysql error() that fyrestrtr pointed me to and I got the following error result.

    The query is:
    INSERT into Feedback values ('0', 'raul', 'gonzalez', 'raulgonzalez@smartlaredo.com', 'Just my email')
    The query could not be executed!
    A fatal MySQL error occured.
    Query:
    Error: (2002) Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46)

    Could anybody tell me what this means.

    thanks

  6. #6
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    It means one of two things :

    1. Your mysql server isn't running

    2. You didn't specify the correct mysql server in your mysql_connect() function.


    I think its number 2 since $Host = "mysql"; doesn't look right. Try changing $Host to "localhost" or "127.0.0.1"

  7. #7
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    307

    localhost

    the thing is that I am not using a server on my computer. I am using yahoo webhost. Will that work anyway?

    The reason I am using "mysql" instead of "localhost" is because I visited one of yahoo's help pages and I read the following:

    First, make sure that you are using the host name "mysql" and not "localhost" in any of your PHP configuration files that require a MySQL host name.

  8. #8
    Join Date
    Oct 2003
    Location
    atlanta georgia usa
    Posts
    6

    which book?

    fyrestrtr sez:
    Would personally like to know what book this example is from

    Ullman, Larry, _PHP for the World Wide Web_ chapter 11, Script 11.4, page 194.

    I have been trying to use this book and gotten stuck in ax example in the regular expressions chapter that doesn't work and cant seem to find out why. The book is out of date. I wish I had another newer edition.

    Bob

  9. #9
    Join Date
    Jan 2003
    Posts
    1,715
    Try using mysql error on the mysql_connect. Perhaps the login and/or password is wrong, but I think you're feeding db_query an invalid link, so it's making a desperate attempt to connect... and failing.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

  10. #10
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    307

    The book

    Yes that would be the exact same book I am using.

  11. #11
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    307

    Hello

    Hi. I finally made it work.

    That was pretty much what the book covered.

    1. create database
    2. create a table
    3. create an HTML form
    4. create a HandleForm
    and
    5. Display database

    does anybody know where I could go and learn how to "errase" from the database hehe.

    thanks

  12. #12
    Join Date
    Dec 2002
    Location
    Toronto, Canada
    Posts
    522
    Go to www.mysql.com and look through their documentation. It's not for the true beginners, but they explain everything really well.

    Inserting, deleting and updating a database is done through queries. Instead of doing the INSERT query that you did before, you would pass a DELETE query. Sometime like:

    PHP Code:
    $Query "DELETE FROM `".$TableName."` WHERE <some condition>"
    You can always not put the WHERE, but that will delete everything from that table. So you should replace <some condition> with a condition, like FirstName = "John"

    Also, note the ` around the name of the table. Although this is optional, it's a good idea to always add these around table and field names to make sure MySQL doesn't confuse it with some reserved word (most common example being a table named ORDER being confused with ORDER BY).

    Hope this helps. Feel free to PM me if you need help.

    Max
    Nokhrin - http://www.nokhrin.com/
    ~ e-commerce application development

Posting Permissions

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