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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.