adambedford Posted February 9, 2010 Share Posted February 9, 2010 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"); Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2010 Share Posted February 9, 2010 INSERT queries don't have WHERE clauses. They create a new record. Are you trying to create a record or UPDATE an existing one? Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/#findComment-1009578 Share on other sites More sharing options...
d_barszczak Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/#findComment-1009683 Share on other sites More sharing options...
Mchl Posted February 9, 2010 Share Posted February 9, 2010 I would also put `URL` as it is best practise. Not really. See discussion we've had the day before yesterday: http://www.phpfreaks.com/forums/index.php/topic,286982.0.html Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/#findComment-1009694 Share on other sites More sharing options...
adambedford Posted February 9, 2010 Author Share Posted February 9, 2010 Thank you for the replies! Yeah I was trying to update a query. All working now! Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/#findComment-1009706 Share on other sites More sharing options...
d_barszczak Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/#findComment-1009754 Share on other sites More sharing options...
Mchl Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191514-can-someone-tell-me-whats-wrong-with-this-mysql-insert-into-code/#findComment-1009758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.