View Full Version : MySQL Error while trying to insert into database

03-21-2010, 07:30 AM
Hi There,

I am trying to get this code working and have sat here for hours trying to figure out whats wrong. Every time I try to add to a mysql database with this is just returns the error "Unable to insert record into database". I can't figure out whats wrong with it.

* Micro Upload
* Version: 0.1
* Date: 2006-10-27
* Usage:
* Set the uploadLocation variable to the directory
* where you want to store the uploaded files.
* Use the version which is relevenat to your server OS.

//Windows way
//$uploadLocation = "c:\\";
//Unix, Linux way
$uploadLocation = "/var/www/html/galleryadmin/upload/";


$hostUrl = 'l*****';
$userName = '*****';
$password = '******';
// connect to database
$connectID = mysql_connect($hostUrl, $userName, $password)
or die ("Sorry, can't connect to database");

//select the database to read from
mysql_select_db("majubagallery_-_data", $connectID)
or die ("Unable to select database");


if (($_POST['submitted']) && (!$_GET['modify_id'])) {
// the user has submitted a new listing
//write to database
mysql_query ("INSERT into art_glass (name, price, thumb_url, big_url) VALUES ('$name', '$price', '$thumb_url', '$big_url')", $connectID)
or die ("Unable to insert record into database");
if ($success) {
print "Record Successfully Added";
header ('Location: microUpload.php');
} else {
// The user has loaded the page to enter a new listing
// do nothing - just let the page load

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Edit links</title>
<style type="text/css">
body {font-family:verdana, arial, sans-serif; font-size:80%}
h2 {font-size:1.4em; }
h3 {padding:10px 0 0 0; margin:0;}
label {display:block; margin:8px 0 2px 0;}
a {display:block; color:#066; margin:3px 0 10px;}
a:hover {color:#000; text-decoration:none;}
input[type="submit"] {display:block; margin-top:8px;}


<h2><img src="http://www.majubagallery.co.nz/images/header.jpg" alt="" width="790" height="228" /></h2>
<h2>Add an Art Glass Piece</h2>
<a href="main.php">Back</a>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

<input name="name" type="text" size="30" id="name" />
<input name="price" />
<a href="microUpload.php" target="_blank">Upload Photos </a></p>

<p>Thumbnail Image Filename</p>
<input name="thumb_url" type="text" size="30" id="topic_url" />
<p>Large Image Filename </p>
<input name="big_url" type="text" size="30" id="pic_big" />
<input type="submit" value="Submit" name="submitted" />
<p>Administration and Content Management Script | &copy; Copyright George Bates 2009 | All Rights Reserved</p>
// close the connection

03-21-2010, 08:05 AM
remove the ( ) from

and rename $namen to price


03-21-2010, 08:12 AM
Thanks for that. Sorry but what do you mean "remove the ( ) from". Where from?

03-21-2010, 08:58 AM
Remove the parentheses around the post values. Also I suggest you do some research on mysql injection.

03-21-2010, 09:30 AM
i just did that, changed it to


yet it still comes up with same error!

03-21-2010, 04:00 PM
Change this

mysql_query ("INSERT into art_glass (name, price, thumb_url, big_url) VALUES ('$name', '$price', '$thumb_url', '$big_url')", $connectID)
or die ("Unable to insert record into database")
to this

$sql = "INSERT into art_glass (name, price, thumb_url, big_url) VALUES ('$name', '$price', '$thumb_url', '$big_url')";
$result = mysql_query ($sql, $connectID)
or die ("Mysql Error:".mysql_error()."<br>SQL:".$sql)
Copy and paste the results here. This should give you a better idea of why its failing.