ThumpernetRyan Posted July 26, 2007 Share Posted July 26, 2007 I wrote a PHP script that takes data from an html form and then "should" insert the data into a SQL database. It can be seen in action here: http://www.thumpernet.net/testdb/form.html Here's the HTML code: <html> <form action="write.php" method="post"> <p> Name:<br /> <input type="text" name="name" size="30" maxlength="30" value="" /> </p> <p> From:<br /> <input type="text" name="from" size="30" maxlength="30" value="" /> </p> <p> Comment:<br /> <input type="text" name="comment" size="50" maxlength="50" value="" /> </p> <p> <input type="submit" name="submit" value="Submit!" /> </p> </form> </html> and here's the PHP code: <?php $server = 'localhost'; $user = (removed); $pass = '(removed)'; $db = 'thump4_testdb'; /* Connect to MySQL server */ $link = @mysql_connect($server, $user, $pass) or die("Could not connect to SQL server because: " . mysql_error()); /* Connect to MySQL database */ mysql_select_db($db) or die("Server Connected OK, but could not connect to database because: " . mysql_error()); /* Retrieve the posted information. */ $name = $_POST['name']; $from = $_POST['from']; $comment = $_POST['comment']; echo "$name<br>"; echo "$from<br>"; echo "$comment<br>"; /* Insert the information into the table */ $query = "INSERT INTO 'table1' ('name', 'from', 'comment') VALUES ('$name', '$from', '$comment')"; $result = mysql_query($query, $link); /* Display a result*/ if ($result) echo "<p>Insert Successful!</p>"; else echo "<p>There was a problem with the insert!</p>"; mysql_close(); ?> Midway through this script the values that are brought over from the HTML form are displayed -- that works fine. Even if I change the insert statement to actual values instead of the passed over values, it still fails ("There was a problem with the insert) (see end of script). The db user has full rights to the database, so I don't think it's a permissions issue. There is a auto-incrementing numeric field that is the db key, but I don't call it in this script. Is it necessary to? Please tell me if I'm just messing up the script in some way, or if I may have a setup problem with the MySQL Db. Thanks! -R Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 /* Insert the information into the table */ $query = "INSERT INTO table1 ('name', 'from', 'comment') VALUES ('".$name."', '".$from."', '".$comment."')"; $result = mysql_query($query) or (die mysql_error()); /* Display a result*/ if ($result) echo "<p>Insert Successful!</p>"; else echo "<p>There was a problem with the insert!</p>"; mysql_close(); try and tell me the error Quote Link to comment Share on other sites More sharing options...
ThumpernetRyan Posted July 26, 2007 Author Share Posted July 26, 2007 Error: Parse error: syntax error, unexpected T_STRING in /home/thump4/public_html/testdb/write.php on line 37 <?php $server = 'localhost'; $user = ; $pass = ; $db = 'thump4_testdb'; /* Connect to MySQL server */ $link = @mysql_connect($server, $user, $pass) or die("Could not connect to SQL server because: " . mysql_error()); /* Connect to MySQL database */ mysql_select_db($db) or die("Server Connected OK, but could not connect to database because: " . mysql_error()); /* Retrieve the posted information. */ $name = $_POST['name']; $from = $_POST['from']; $comment = $_POST['comment']; echo "$name<br>"; echo "$from<br>"; echo "$comment<br>"; /* Insert the information into the table */ $query = "INSERT INTO table1 ('name', 'from', 'comment') VALUES ('".$name."', '".$from."', '".$comment."')"; $result = mysql_query($query) or (die mysql_error()); /* Display a result*/ if ($result) echo "<p>Insert Successful!</p>"; else echo "<p>There was a problem with the insert!</p>"; mysql_close(); Thanks, -R Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 ooops sorry $result = mysql_query($query) or (die mysql_error()); change to $result = mysql_query($query) or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
ThumpernetRyan Posted July 26, 2007 Author Share Posted July 26, 2007 namehere fromhere commenthere 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 ''name', 'from', 'comment') VALUES ('namehere', 'fromhere', 'co' at line 1 Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 try $query = "INSERT INTO table1 (name, from, comment) VALUES ('".$name."', '".$from."', '".$comment."')"; echo $query; Quote Link to comment Share on other sites More sharing options...
ThumpernetRyan Posted July 26, 2007 Author Share Posted July 26, 2007 namehere fromhere commenthere INSERT INTO table1 (name, from, comment) VALUES ('namehere', 'fromhere', 'commenthere') Is there a way to "log" the PHP<>SQL session? I was thinking about asking my hoster to give me the SQL logs. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 i guess the error is fixed but what is your question ???? Quote Link to comment Share on other sites More sharing options...
ThumpernetRyan Posted July 26, 2007 Author Share Posted July 26, 2007 The data is not making it's way into the database ... does the SQL code look correct so that now I have to go look at the database? Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 yes . heres how to check your new prob i have echo the query remember copy that and query it to you db to see if that will work in your DB Quote Link to comment Share on other sites More sharing options...
ThumpernetRyan Posted July 26, 2007 Author Share Posted July 26, 2007 I figured it out after posting the SQL query directly into MySQL ... it turns out that I was using a field name of "from" which is of course a SQL query command so I renamed the field to "user_name" and it's working! Thanks for the help. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 yes i dont see that but you can still run that if you use the thick (````) that Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.