Results 1 to 14 of 14
  1. #1
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    Below is a short script that is not working. It gives me the "couldn't execute query" error. I have triple checked everything 3 or 4 times over. I cannot figure out why it won't work. the table name is correct, user and password are correct. I think it is the $sql variable. This script is just like another I am running with changes made to the field names. No other differences. Any help would be appreciated. Username and password are "starred" out.

    <?
    if ((!$title) || (!$link) || (!$desc)) {
    header( "location: addalink.htm");
    exit;
    }
    $db_name = "hehkqwdo_classic";
    $table_name = "links";

    $connection = @mysql_connect("localhost", "******", "******") or
    die("couldn't connect.");

    $db = @mysql_select_db($db_name, $connection) or die("couldn't select database.");

    $sql = "INSERT INTO $table_name(title, link, desc)
    VALUES
    (\"$title\", \"$link\", \"$desc\")
    ";

    $result = @mysql_query($sql,$connection) or die("Couldn't execute query.");
    ?>

    "Bueller...? Bueller...?"

  2. #2
    Just Lurking
    Join Date
    January 18th, 2005
    Posts
    1,263
    try single quotes ' :

    $sql = "INSERT INTO $table_name (title, link, desc)
    VALUES
    ('$title', '$link', '$desc')
    ";

    ------------------------------
    "A man is but the product of his thoughts. What he thinks, he becomes." -- Mahatma Gandhi

  3. #3
    Just Lurking
    Join Date
    January 18th, 2005
    Posts
    1,263
    Bet it's the link, did you escape and/or is magic-quotes on?

    ------------------------------
    "A man is but the product of his thoughts. What he thinks, he becomes." -- Mahatma Gandhi

  4. #4
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    single quotes did not work... the link is good. I checked it AGAIN and as far as magic quotes... huh? Not sure. This script is running on the same domain as the other one I mentioned. All is the same. I put them side by side and the only difference is the values and the link back to the form page. I checked the values over and over and all is fine there. It is making a connection so I know that $connection is good. It has to be $sql that is not right.

    "Bueller...? Bueller...?"

  5. #5
    Full Member
    Join Date
    January 18th, 2005
    Posts
    322
    You might try disabling error suppression and seeing what error message PHP is generating. Or one thing that I do when I’m having trouble with a query is echo it to the screen. That way you can see exactly what you’re trying to execute. Or, and this is just the way that I code: try … (’”.$title.”’,”’.$link.”’,’”.$desc.”’)”;

    edited to add:
    Ok, you can’t really see what it is I’m doing because of the way it’s displayed, but basically I’ve got the open parentheses a single quote then I close the double quotes opened put a period then your variable then another period then open quotes then a second single quote then your comma and start all over again with a single quote. You shouldn’t have to go through all this if you have magic quotes enabled. I’d still bet money that the problem is somewhere in the values of the variables you’re using in your query. Maybe some unescaped quotes or semicolon or something.

    Jason
    “I get on my knees and pray, we don’t get fooled again!” – Who said that? I said that!

  6. #6
    Just Lurking
    Join Date
    January 18th, 2005
    Posts
    1,263
    What I'm thinking is that the values are not escaped, this means there could be a stray ' " \ any of these could screw up the query.

    Example: $title = '"Big" Title';

    now you put that in your double quoted query you get;

    ""Big" Title"

    See the problem Big isn't even in quotes.

    Anyway try this:

    <?
    if ((!$title) || (!$link) || (!$desc)) {
    header( "location: addalink.htm");
    exit;
    }
    $title = addslashes($title);
    $link = addslashes($link);
    $desc = addslashes($desc);
    .... rest of script

    ------------------------------
    "A man is but the product of his thoughts. What he thinks, he becomes." -- Mahatma Gandhi

  7. #7
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    I'm kinda new so don't understand everything. I did delete the error suppression "@" in front of the mysql_query but it still gives me the "Couldn't execute query." error which it shouldn't do. I manually entered data into the table, and my web page (links page) displays the data so I know I am able to query the database and table. I am stumped.

    "Bueller...? Bueller...?"

  8. #8
    Full Member
    Join Date
    January 18th, 2005
    Posts
    322
    I would go ahead and check the contents of the $sql variable. Comment out the $result… line and add
    echo $sql;
    Then look for anything in the output that might throw a wrench into the script. Any double quotes should be escaped, etc.

    edited to add:
    or, uhmm, add a space between $table_name and (

    Jason
    “I get on my knees and pray, we don’t get fooled again!” – Who said that? I said that!

  9. #9
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    Thanks newcastle... I wrote (copied from another book) a whole new script. It is below. It is giving me the exact problem.It includes both yours and Buddhas suggestions.

    <html>
    <head>
    <title> Adding a link to my website</title>
    </head>
    <body>
    <?
    if (!$title || !$link || !$desc)
    {
    header( "location: addalink.htm");
    exit;
    }
    $title = addslashes($title);
    $link = addslashes($link);
    $desc = addslashes($desc);

    @ $db = mysql_pconnect("localhost", "******", "******") or
    die("couldn't connect.");

    if (!$db)
    {
    echo "error: could not connect";
    exit;
    }

    mysql_select_db("hehkqwdo_classic");
    $table_name = "links";
    $query = "insert into $table_name values
    (' ".$title." ',' ".$link." ',' ".$desc." ',)";
    $result = mysql_query($query) or die("couldn't execute query.");
    if ($result)
    echo mysql_affected_rows()." Link inserted into database.";
    ?>

    </body>
    </html>

    This comes from PHP and MYSQL web development by welling and thomson, which is highly recommended by Amazonians.

    I find it hard to believe that 2 different scripts are giving me the same problem unless it the table itself is the problem.

    I do appreciate everyones help BTW

    Thanks
    Steve

    "Bueller...? Bueller...?"

  10. #10
    Just Lurking
    Join Date
    January 18th, 2005
    Posts
    1,263
    you've go an extra " , " in there:

    $query = "insert into $table_name values
    ('".$title."','".$link."','".$desc."')";


    Edit: Might want to change mysql_pconnect to mysql_connect if your on a shared host. If your on a dedicated server then you can kill the stray processes.

    ------------------------------
    "A man is but the product of his thoughts. What he thinks, he becomes." -- Mahatma Gandhi

  11. #11
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    I got up this morning and thought I would take care of this problem easily with a little sleep. That was 6 hours ago.

    I have copied it verbatem. I see no extra "," as far as the book goes.

    I add this: ...Couldn't execute query because: ' . mysql_error());

    It says parse error on line 29 (oh boy)

    ...mysql_error());
    ?>


    <HTML>
    <HEAD>
    <TITLE>Add a link</TITLE>
    </HEAD>
    ***this is line 29*** <BODY>
    <H1>Adding a record to <? echo "$table_name"; ?><H1>

    <TABLE CELLSPACING=3 CELLPADDING=3>
    <TR>

    I'm giving up. I will hard code the damn links in.

    Thanks again.
    Steve

    "Bueller...? Bueller...?"

  12. #12
    Just Lurking
    Join Date
    January 18th, 2005
    Posts
    1,263
    that bold " , "

    $query = "insert into $table_name values
    (' ".$title." ',' ".$link." ',' ".$desc." ',)";

    ------------------------------
    "A man is but the product of his thoughts. What he thinks, he becomes." -- Mahatma Gandhi

  13. #13
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    I see it now. I didn't see what you were talking about.

    "Bueller...? Bueller...?"

  14. #14
    ABW Ambassador Steveinid's Avatar
    Join Date
    January 18th, 2005
    Posts
    827
    Nope... not that either.

    I think it is possessed by demons as an earlier person suggested thier computer had also been possessed.

    "Bueller...? Bueller...?"

  15. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Dealing with Frustration
    By andyparks16 in forum Midnight Cafe'
    Replies: 2
    Last Post: December 22nd, 2009, 09:52 AM
  2. Tech Frustration
    By Vinny O'Hare in forum Virtual Family and Off-Topic
    Replies: 26
    Last Post: September 3rd, 2006, 05:46 PM
  3. Frustration
    By John Kruger in forum Midnight Cafe'
    Replies: 13
    Last Post: November 4th, 2003, 12:38 PM

Posting Permissions

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