Page 1 of 1
News area!!!
Posted: Wed Mar 17, 2004 8:05 pm
by Joe
I am trying to create a news script for my site but i have bumped into a small problem. Every time i submit the form i get an error on this line:
mysql_query = ("INSERT INTO news VALUES('1','$head','$user','$newsarea') WHERE ID = '1'") or die("Could not connect...");
I cant think why. I have also included a small section of the script below if it helps. Please help?
$link1 = mysql_connect("???", "???", "???");
mysql_select_db("???") or die("couldnt connect" . mysql_error());
mysql_query = ("INSERT INTO news VALUES('1','$head','$user','$newsarea') WHERE ID = '1'") or die("Could not connect...");
mysql_close($link1);
Regards
Joe

Posted: Wed Mar 17, 2004 8:09 pm
by markl999
An INSERT doesn't need a WHERE clause, you'de only use WHERE on an UPDATE/DELETE etc ... but not an INSERT
And change ..
mysql_query = ("INSERT INTO news VALUES('1','$head','$user','$newsarea') WHERE ID = '1'") or die("Could not connect...");
to
mysql_query("INSERT INTO news VALUES('1','$head','$user','$newsarea')") or die(mysql_error());
Posted: Wed Mar 17, 2004 8:10 pm
by tim
Code: Select all
<?php
mysql_query("INSERT INTO news VALUES('1','$head','$user','$newsarea')") or die(mysql_error());
?>
also the WHERE clause is not needed for INSERT command...
you would however use it for UPDATE (to target a specific row for editing.)
/edit - ahh, late.
Posted: Wed Mar 17, 2004 8:12 pm
by Goowe
So, I have a question as responces arise. Sorry to hijack the thread Joe
Do you need to title the values before putting them in? Or as long as the values are in order with the columns in the database it will all work out?
Example...
$query = "INSERT INTO news (news_id, news_name, news_article) VALUES ('1', 'Hi', 'Just testing')";
instead of
$query = "INSERT INTO news VALUES ('1', 'Hi', 'Just testing')";
Posted: Wed Mar 17, 2004 8:13 pm
by JAM
markl999 wrote:An INSERT doesn't need a WHERE clause, you'de only use WHERE on an UPDATE/DELETE etc ... but not an INSERT ;)
And change ..
mysql_query = ("INSERT INTO news VALUES('1','$head','$user','$newsarea') WHERE ID = '1'") or die("Could not connect...");
to
mysql_query("INSERT INTO news VALUES('1','$head','$user','$newsarea')") or die(mysql_error());
Just continuing on this one... ;)
If the '1' in the last query statement is an autoincrement id of some sort, use
Code: Select all
mysql_query("INSERT INTO news VALUES('','$head','$user','$newsarea')") or die(mysql_error());
...without the actual number. Or else you are likely trying to insert a row that allready contains that key/id, and that will not work.
Posted: Wed Mar 17, 2004 8:15 pm
by tim
Goowe, you can do it either way, the second method is the more clear/better way. and as u said, the values going in must be in the order of the fields.
Posted: Wed Mar 17, 2004 8:16 pm
by JAM
Goowe wrote:So, I have a question as responces arise. Sorry to hijack the thread Joe :roll:
Do you need to title the values before putting them in? Or as long as the values are in order with the columns in the database it will all work out?
Example...
$query = "INSERT INTO news (news_id, news_name, news_article) VALUES ('1', 'Hi', 'Just testing')";
instead of
$query = "INSERT INTO news VALUES ('1', 'Hi', 'Just testing')";
No, but see my post of some issues that might happen when dealing with autoinc. values (as id's often are).
Edited:
Man, did everyone just felt an urge to post in this thread at once?
A long time since I've seen 4 different people posting in less than 10 sec intervals and then also delete their posts. Hillarious. ;)
Shouldn't delete them though, as the personal thoughts are more than welcome...
Posted: Wed Mar 17, 2004 8:20 pm
by Joe
Thanks for all the help. In the end up i used:
mysql_query("UPDATE news SET ID='1', username='$user', headline='$head', mainnews='$newsarea' WHERE ID='1'") or die(mysql_error());
Basically because i had to use a Where clause in order to be navigated to the correct ID.
Regards
Joe

Posted: Wed Mar 17, 2004 8:21 pm
by Illusionist
mysql_query("UPDATE CS_news SET ID='1', username='$user', headline='$head', mainnews='$newsarea' WHERE ID='1'") or die(mysql_error());
you really don't need that ID='1', , because it already is 1... but all the same it still works...