INSERT INTO won't INSERT INTO

I don't understand for the life of me why this won't insert into the db.  The table structure is as follows:

Id = Autoincrement
title = varchar
date = varchar
url = varchar
$link=mysql_connect(SERVER,USER,PASSWORD) or die("Database Connection Error.");
mysql_select_db("DB", $link);
$sql="INSERT INTO `news` (title, url, date) VALUES (\"$title\", \"$file\", \"$date\")";
if (!$link)
	{
		die('Could not connect: ' . mysql_error());
	}
echo "The following info was successfully added:<br><br><b>Title:</b> $_POST[title]<br><b>Date:</b> $_POST[date]<br><b>Filename:</b> $file";
mysql_close($link)
?>
 
I have also tried:
 
$link=mysql_connect(SERVER,USER,PASSWORD) or die("Database Connection Error.");
mysql_select_db("DB", $link);
$sql="INSERT INTO `news` (title, url, date) VALUES ('$title', '$file', '$date')";
if (!$link)
	{
		die('Could not connect: ' . mysql_error());
	}
echo "The following info was successfully added:<br><br><b>Title:</b> $_POST[title]<br><b>Date:</b> $_POST[date]<br><b>Filename:</b> $file";
mysql_close($link)

Open in new window

LVL 9
pmagonyAsked:
Who is Participating?
 
mostartConnect With a Mentor Commented:
you are not doing any query in your code you need to add a mysql_query($sql) to your code.

http://de2.php.net/mysql_query
0
 
Ray PaseurCommented:
As with most incomplete posts, it is hard to know all the data-related issues, or where the fields are coming from, so I made some assumptions.  You might want to use something more like this..

HTH, ~Ray
// CONNECT AND SELECT 
$link = mysql_connect(SERVER,USER,PASSWORD) or die("Database Connection Error.");
if (!mysql_select_db("DB", $link)) die("Database Selection Error.");
 
 
// ESCAPE THE DATA TO AVOID DATABASE CATASTROPHE
$title = mysql_real_escape_string($_POST["title"], $link);
$date  = mysql_real_escape_string($_POST["date"],  $link);
$file  = mysql_real_escape_string($_POST["file"],  $link);
 
// CONSTRUCT THE QUERY FROM THE ESCAPED DATA
$sql="INSERT INTO `news` (title, url, date) VALUES ('$title', '$file', '$date')";
 
// EXECUTE THE QUERY AND TRAP ANY ERRORS
if (!$s = mysql_query($sql))
{
   echo "<br/>QUERY FAIL: $sql \n";
   $ermgs = mysql_errno() . mysql_error();
   echo "<br/>$ermgs \n";
   die('Foo');
}
 
// REPORT THE RESULTS
echo "The following info was successfully added:<br/><br/><b>Title:</b> $_POST[\"title\"]<br/><b>Date:</b> $_POST[\"date\"]<br/><b>Filename:</b> $_POST[\"file\"]";;
mysql_close($link);

Open in new window

0
 
pmagonyAuthor Commented:
pmagony <-- TOTAL NEWB.

Thanks Mostart.

End result:

$link=mysql_connect(SERVER,USER,PASSWORD) or die("Database Connection Error.");
mysql_select_db("DB", $link);
$sql=mysql_query("INSERT INTO `news` (title, url, date) VALUES ('$title', '$file', '$date')");
printf("Records updated: %d\n", mysql_affected_rows());
if (!$link)
      {
            die('Could not connect: ' . mysql_error());
      }
echo "The following info was successfully added:

Title: $_POST[title]
Date: $_POST[date]
Filename: $file";
mysql_close($link)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
@pmagony: Going forward, you may find you want to leave your questions open a little bit longer.  You may find, as in the example I posted above, that there are other things you need to know besides just the question you asked.  For example, you should probably escape the query strings, and you should be aware that a construct like this...

   $_POST[title]

... is a time-bomb because that tells PHP to look for a defined constant named 'title' -- if you or anyone else who maintains your code ever defines 'title' your program will stop working!

A good book on PHP and MySQL is available here: http://www.sitepoint.com/books/phpmysql1/ -- if you get that and work through the examples, it will teach you a great deal about the subjects and will surely save you a lot of time and effort.

Best regards, ~Ray
0
 
pmagonyAuthor Commented:
Thanks Ray for the feedback, I was actually closing out the question during your response.  I will certainly let it stew longer next time.  
0
 
Ray PaseurCommented:
10-4.  Even when I get an answer in two minutes, I like to leave the Q open for a day.  A lot of people can only look at EE in the evenings, etc.  A little extra time often gets a good collection of comments and suggestions.

Cheers, ~Ray
0
All Courses

From novice to tech pro — start learning today.