News area!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

News area!!!

Post 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 8)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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());
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

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')";
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
Last edited by JAM on Wed Mar 17, 2004 8:20 pm, edited 1 time in total.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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 8)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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...
Post Reply