Re: php can't insert data mysql table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 12:32 AM 10/1/2012, Tim Dunphy wrote:
Hello list,

 I'm trying to learn and get comfortable with HTML forms in PHP. In one
example I am trying to include a file upload in one of the forms. I can
connect to the database but for some reason the MySQL query string is bad.
I've been over this for a while now and I think the problem is with the
form. I was hoping to get the esteemed opinion of the list in where I may
be going wrong.

This code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Guitar Wars - Add Your High Score</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Guitar Wars - Add Your High Score</h2>

<?php
  if (isset($_POST['submit'])) {
    // Grab the score data from the POST
    $name = $_POST['name'];
    $score = $_POST['score'];
    $screenshot = $_FILES['screenshot']['name'];

    if (!empty($name) && !empty($score)) {
      // Connect to the database
      $dbc = mysqli_connect('localhost', 'xxx', 'xxx', 'gwdb')
          or die('Cannot connect to database');

      // Write the data to the database
      $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score',
'$screenshot')";
      mysqli_query($dbc, $query)
         or die('Cannot insert query');

      // Confirm success with the user
      echo '<p>Thanks for adding your new high score!</p>';
      echo '<p><strong>Name:</strong> ' . $name . '<br />';
      echo '<strong>Score:</strong> ' . $score . '</p>';
      echo '<p><a href="index.php">&lt;&lt; Back to high scores</a></p>';

      // Clear the score data to clear the form
      $name = "";
      $score = "";

      mysqli_close($dbc);
    }
    else {
      echo '<p class="error">Please enter all of the information to add
your high score.</p>';
    }
  }
?>

[snip]

First -- NEVER post code with your database username/password. Since you did, change your db password immediately.

Second, when debugging, if you use the die() function, put out something meaningful like
... or die("Problem with the insert query: $query<br>" . mysqli($dbc));

Once you see the database error, you will probably be able to figure out the problem.

Ken

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux