Jump to content

Recommended Posts

Hi All

 

I'm trying to create a form that allows a user to input information into a table in my database. However, the insert keeps failing and I get the message 'Error Insert Failed.'. Can anyone help me understand what's gone wrong. Also I'm not sure if this is a php problem or a MYSQL problem.

 

Thanks for your help.

 

This is my html form....

 

<html>
<head>
</head>

<body>

<form method="post" action="upload.php">
Place Name:
<input type="text" name="name"/>
<input type="submit"/>
</form>

</body>
</html>

 

and this is my php code...

 

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 


//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");


$selected = mysql_select_db("MyWebsite",$dbhandle) 
  or die("Could not select examples");
  

$result = mysql_query("SELECT * FROM place ORDER BY ID desc limit 1") or die(mysql_error());

$row = mysql_fetch_array($result);
$newID = $row['ID'] + 1;

mysql_query("INSERT INTO place (ID, Name) VALUES ($newID, $_POST[name]") or die("Error, Insert Failed.");
?>

Link to comment
https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/
Share on other sites

You can setup your database to automatically increment your id.  Set that up, and then also make sure you always clean your variables before adding to db.  Your code should look something like this:

 

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 

if(!isset($_POST['name'])){
die("Name not posted.");
}

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");  

$name = mysql_real_escape_string($_POST['name']);

mysql_query("INSERT INTO `place` (`ID`, `Name`) VALUES ('', '$name'") or die("Error, Insert Failed.");
?>

Edit: nevermind again, It was a simple syntax error, just not the one I thought

try:

mysql_query("INSERT INTO place (ID, Name) VALUES ($newID, $_POST[name])") or die("Error, Insert Failed: ".mysql_error());

 

you are missing a closing ")" character in the query.

 

also, as some people said, you can set up your table to auto increment your id field (IDK if you set up your tables manually, or use something like phpmyadmin, but either way you can pretty easily)

 

in the future, when dealing with sql, it helps to output the mysql error ie

 

query or die("Mysql Error: " . mysql_error());

nevermind, I read your code wrong

 

also, as some people said, you can set up your table to auto increment your id field (IDK if you set up your tables manually, or use something like phpmyadmin, but either way you can pretty easily)

 

in the future, when dealing with sql, it helps to output the mysql error ie

 

query or die("Mysql Error: " . mysql_error());

 

Displaying the mysql error is definitely necessary for development, just make you remember to turn it off when the site is live or public.

Thanks everyone for your input.  And the tip about automatically incrementing my ID, I didn't know I could do that with MYSQL.

mikesta707 you were right, I was just missing a bracket...arghhh!  It's always the easiest thing.

 

Thanks again :)

 

 

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.