Jump to content

Can someone tell me whats wrong with this MySQL INSERT INTO code


adambedford

Recommended Posts

I've written this simple, very basic code to insert a record and it's not working for some reason.

 

session_start();

$name = $_SESSION["name"];
$url = $_SESSION["url"];
$email = $_SESSION["email"];
$description = $_SESSION["description"];
$category = $_SESSION["category"];
$highlighted = $_SESSION["highlighted"];
$featured = $_SESSION["featured"];
$homepage = $_SESSION["homepage"];

$datecreated = date("Y-m-d");

$nextmonth = mktime(0,0,0,date("m")+1,date("d"),date("Y"));
$renewaldate = date("Y-m-d", $nextmonth);


$insert = ("INSERT INTO links (Name, Description, Category_ID, Email_address, Highlight, Featured, Homepage, Date_created, Renewal_date) VALUES ('$name','$description','$category','$email','$highlighted','$featured','$homepage','$datecreated','$renewaldate') WHERE '$url' = URL");

mysql_query($insert) or die("Error adding additional data to database. URL stored, all else unavailable");

If you are trying to update an existing query you would need something like the fillowing but obviously with all the data you need.

 

$query = "UPDATE `links` SET `name`= '$name', `description`='$description' WHERE `url`='$url';

 

If you are inserting an new row then your query would work without the WHERE '$url' = URL. Just a pointer but it looks as though the WHERE '$url' = URL bit is wrong too. It would be WHERE URL = '$url' but I would also put `URL` as it is best practise.

Hi Mchl,

 

Maybe it is, maybe its not and we don't really need to re-create that thread here :) I personally would always reommend it because the first time you create a column named date it will all go pear shaped.

 

Although that said, renaming the column to date_created would also fix the same problem but when you want your code to be consistant you would use ``.

 

Anyway thanks for the input.

Hi Mchl,

 

Maybe it is, maybe its not and we don't really need to re-create that thread here :) I personally would always reommend it because the first time you create a column named date it will all go pear shaped.

 

Although that said, renaming the column to date_created would also fix the same problem but when you want your code to be consistant you would use ``.

 

Anyway thanks for the input.

 

Actually `date` is a reserved word, but it can be used as colmn name without backticks due to popular demand ;) But I guess we can agree that it's important to be consistent and also know what using/omitting backticks means for your application.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.