Jump to content

Why is my Query not working?


AdamCCFC

Recommended Posts

When I run my query I get this error, anybody know why??

 

Error

 

SQL query:

$query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST['comment']}')";

 

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST[' at line 1

Link to comment
https://forums.phpfreaks.com/topic/189561-why-is-my-query-not-working/
Share on other sites

When I run my query I get this error, anybody know why??

 

Error

 

SQL query:

$query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST['comment']}')";

 

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST[' at line 1

 

try

 

$query = "INSERT INTO dreams_comments VALUES('','$id','$_POST[name]','$_POST[comment]')";

 

That should work

When I run my query I get this error, anybody know why??

 

Error

 

SQL query:

$query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST['comment']}')";

 

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST[' at line 1

 

try

 

$query = "INSERT INTO dreams_comments VALUES('','$id','$_POST[name]','$_POST[comment]')";

 

That should work

 

Actually it shouldn't.

 

 

$query = "INSERT INTO dreams_comments VALUES('','" . $id . "','" . $_POST['name'] . "','" . $_POST['comment'] . "')";

 

Not the safest way to insert in to a database though, especially as it's accepting form inputs.  Go look up mysql_real_escape_string()

hmmm tried the suggestions but they dont seem to work! Maybe my form is wrong?? Here's the code:

 

    /* add a form where users can enter new comments */
echo "Leave a comment!</br></br>";
    echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>";
    echo "Name: <input type'text' width'30' name='name'></br>";
    echo "Comment: <textarea cols='30' rows='10' name='comment'></textarea></br>";
    echo "<input type='submit' name='submit' value='Add Comment'";
    echo "</form>";
     
}

function addComment($id) {
    global $db;
    /* insert the comment */
    $query = "INSERT INTO dreams_comments VALUES('','" . $id . "','" . $_POST['name'] . "','" . $_POST['comment'] . "')";
    mysql_query($query);
    
    echo "Comment entered. Thanks!</br>";
    echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>";
}

tried the suggestions but they dont seem to work!

 

That's because all three are just different methods of forming a string in php and they are all equivalent.

 

What is the code and where is it that is displaying the mysql_error(), because it is highly likely that it is a different piece of code that is producing the error.

echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>";

 

That line's wrong.

 

Try:

 

echo "<form id='addcomment' action=\"addcomment&id=$id\" method='POST'>";

 

I take it your form wasn't submitting to the correct page.

 

@Tazerenix

 

When wrapping text in ' ' it won't parse variables, so if you did this:

 

$myvariable = '$newvariable';

 

$myvariable would actually contain the text $newvariable rather than the value of $newvariable.  Hope that makes sense!  Give it a shot.

Here's all my code if this helps?

 

<?php
/* user config variables */
$max_items = 5; /* max number of news items to show */

/* make database connection */
$db = mysql_connect ('localhost','root','');
mysql_select_db ('mydreamz',$db);

function displayDreams($all = 0) {
    /* bring in two variables
     * $db is our database connection
     * $max_items is the maximum number
     * of news items we want to display */
    global $db, $max_items;
    
    /* query for news items */
    if ($all == 0) {
        /* this query is for up to $max_items */
        $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC LIMIT $max_items";
    } else {
        /* this query will get all news */
        $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC";
    }
    $result = mysql_query ($query);
    while ($row = mysql_fetch_assoc ($result)) {
        /* display news in a simple table */
        echo "<TABLE border'1' width='300'";

        /* place table row data in 
         * easier to use variables.
         * Here we also make sure no
         * HTML tags, other than the
         * ones we want are displayed */
        $date = $row['date_entered'];        
        $name = htmlentities ($row['name']);
        $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
	$tags = $row['tags'];
        
        /* display the data */
        echo "<TR><TD>$name</TD></TR>";
        echo "<TR><TD>$dream</TD></TR>";
	echo "<TR><TD>$tags</TD></TR>";
        
        /* get number of comments */
        $comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}";
        $comment_result = mysql_query ($comment_query);
        $comment_row = mysql_fetch_row($comment_result);
        
        /* display number of comments with link */
	echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}?action=show&id={$row['dream_id']}\">Comments ($comment_row[0])</a></TD></TR>";/* finish up table*/
        echo "</TABLE>";
        echo "</br>";
    }
    
    /* if we aren't displaying all dream, 
     * then give a link to do so */
    if ($all == 0) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}\"?action=all'\">'View all dreams'</a>";
    }
}

function displayOneItem($id) {
    global $db;

    /* query for item */
    $query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'";
    $result = mysql_query ($query);
    
    /* if we get no results back, error out */
    if (mysql_num_rows ($result) == 0) {
        echo "Bad Dream ID\n";
        return;
    }
    $row = mysql_fetch_assoc($result);
    echo "<table border='1' width='300'>";

    /* easier to read variables and 
     * striping out tags */
    $name = htmlentities ($row['name']);
    $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
$tags = ($row['tags']);
    
    /* display the items */
    echo "<TR><TD>$name</TD></TR>";
    echo "<TR><TD>$dream</TD></TR>";
echo "<TR><TD>$tags</TD></TR>";
    
    echo "</TABLE>";
    echo "</br>";
    
    /* now show the comments */
    displayComments($id);
}

function displayComments($id) {
    /* bring db connection variable into scope */
    global $db;

    /* query for comments */
    $query = "SELECT * FROM dreams_comments WHERE dream_id=$id";
    $result = mysql_query ($query);
    echo "Comments:</br>";
    
    /* display the all the comments */
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TABLE border='1' width='300'";
        
        $name = htmlentities ($row['name']);
        echo "<TR><TD>$name</TD></TR>";
    
        $comment = strip_tags ($row['comment'], '<a><b><i><u>');
        $comment = nl2br ($comment);
        echo "<TR><TD>$comment</TD></TR>";
    
        echo "</TABLE>";
        echo "</br>";
    }
    
    /* add a form where users can enter new comments */
echo "Leave a comment!</br></br>";
    echo "<form id='addcomment' action=\"addcomment&id=$id\" method='POST'>";
    echo "Name: <input type'text' width'30' name='name'></br>";
    echo "Comment: <textarea cols='30' rows='10' name='comment'></textarea></br>";
    echo "<input type='submit' name='submit' value='Add Comment'";
    echo "</form>";
     
}

function addComment($id) {
    global $db;
    /* insert the comment */
    $query = "INSERT INTO dreams_comments VALUES('','$id','$_POST[name]','$_POST[comment]')";
    mysql_query($query);
    
    echo "Comment entered. Thanks!</br>";
    echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>";
}

/* this is where the script decides what do do */


switch($_GET['action']) {
    
    case 'show':
        displayOneItem($_GET['id']);
        break;
    case 'all':
        displayDreams(1);
        break;
    case 'addcomment':
        addComment($_GET['id']);
        break;
    default:
        displayDreams();
}
?>

I'm tired, so I completely mis-read your form tag.  The fact your query string is called 'action' threw me!

 

Try changing the form tag to this now:

 

echo "<form id='addcomment' action=\"" . $_SERVER['PHP_SELF'] . "?action=addcomment&id=$id\" method='POST'>";

Load up the page where you're submitting the comment and right-click -> View Source, what does it say inside the action tag of the form?

 

The form tag looks fine to me now but it sounds a bit odd you're getting a 404 error...

 

There's still an issue with your addComment() function but I'll come to that when you start getting MySQL errors rather than 404s.

Ok so here's the page source:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<title>Object not found!</title>

<link rev="made" href="mailto:postmaster@localhost" />

<style type="text/css"><!--/*--><![CDATA[/*><!--*/

    body { color: #000000; background-color: #FFFFFF; }

    a:link { color: #0000CC; }

    p, address {margin-left: 3em;}

    span {font-size: smaller;}

/*]]>*/--></style>

</head>

 

<body>

<h1>Object not found!</h1>

<p>

 

 

    The requested URL was not found on this server.

 

 

 

    The link on the

    <a href="http://localhost/mydreamz/comments.php%3faction=show&id=19">referring

    page</a> seems to be wrong or outdated. Please inform the author of

    <a href="http://localhost/mydreamz/comments.php%3faction=show&id=19">that page</a>

    about the error.

 

 

 

</p>

<p>

If you think this is a server error, please contact

the <a href="mailto:postmaster@localhost">webmaster</a>.

 

</p>

 

<h2>Error 404</h2>

 

<address>

  <a href="/">localhost</a><br />

 

  <span>23/01/2010 22:43:43<br />

  Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0</span>

</address>

</body>

</html>

 

 

My bad, I'm also tired  :D Here we go:

 

<table border='1' width='300'><TR><TD>Sarah</TD></TR><TR><TD>I was watching Heroes and Sylar started to chase me</TD></TR><TR><TD>sylar,heroes</TD></TR></TABLE></br>Comments:</br>Leave a comment!</br></br><form id='addcomment' action="/mydreamz/comments.php?action=addcomment&id=16" method='POST'>Name: <input type'text' width'30' name='name'></br>Comment: <textarea cols='30' rows='10' name='comment'></textarea></br><input type='submit' name='submit' value='Add Comment'</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.