Re: php can't insert data mysql table
| From: | Stuart Dallas | Date: | Mon, 01 Oct 2012 13:36:26 +0000 |
| Subject: | Re: php can't insert data mysql table | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319313@lists.php.net to get a copy of this message | ||
On 1 Oct 2012, at 05:32, Tim Dunphy <bluethundr@gmail.com> 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', 'admin', 'secretsauce',
> '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"><< 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>';
> }
> }
> ?>
>
> <hr />
> <form enctype="multipart/form-data" method="post" action="<?php
> echo
> $_SERVER['PHP_SELF']; ?>">
> <input type="hidden" name="MAX_FILE_SIZE" value="32768"
> />
> <label for="name">Name:</label>
> <input type="text" id="name" name="name"
> value="<?php if
> (!empty($name)) echo $name; ?>" /><br />
> <label for="score">Score:</label>
> <input type="text" id="score" name="score"
> value="<?php if
> (!empty($score)) echo $score; ?>" />
> <br />
> <label for="screenshot">Screenshot:</label>
> <input type="file" id="screenshot" name="screenshot" />
> <hr />
> <input type="submit" value="Add" name="submit" />
> </form>
> </body>
> </html>
>
> Results in this output:
>
> Guitar Wars - Add Your High Score Cannot insert query
Assuming the first field is an autonumber you don't want to set it. Otherwise every row you try
to insert will have an ID of 0, which will be a unique index clash after the first one.
But take note of what everyone else is saying. You should be getting the error message when this
happens which will tell you exactly what the problem is, above and beyond "Cannot insert
query" (which, btw, makes no sense at all :)).
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/