Jump to content

[SOLVED] PHP5 / MySQL5 // PHP SQL insert script fails to insert


ThumpernetRyan

Recommended Posts

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

 

/* 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

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

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

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.

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.

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.