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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.