PHP/MySQL Problem

Q&A, advice, reviews, and news about the computers, phones, TVs, stereos, and pretty much anything else that can't be easily whittled out of a stick or chipped out of stone.
Locked
I_Like_Pie
Redshirt
Posts: 5
Joined: Sat Aug 28, 2004 4:25 am
Location: Ohio
Contact:

PHP/MySQL Problem

Post by I_Like_Pie » Sun Nov 06, 2005 11:49 pm

Well, let me start this off by saying that I'm fairly new to dealing with databases. Basically, I'm (for practice), designing a news site. I have a login page that accepts a username/password/poster name/news. Then it goes to my php script that does all the wonderful fun stuff.

Code: Select all

1 <?php
2 $link = mysql_connect('localhost', $_POST["username"], 
3 $_POST["password"]);
4 if (!$link) {
5    die('Could not connect: ' . mysql_error());
6 }
7 echo 'Connected successfully';
8 @mysql_select_db("mydomain_com_news") or die( "Unable to 
9 select database");
10 //Removed pointless variable here...just noticed it.
11 $poster = $_POST['poster'];
12 $news = $_POST['news'];
13 $query = "INSERT INTO Main VALUES (' ', $poster, $news)"
14 mysql_query($query);
15 $i++;
16 if (mysql_query($query))
17 {
18 echo("<p>News Added.</p>");
19 }
20 else
21 {
22 echo("<p>Error adding submission: " . mysql_error() . "</p>");
23 }
24 mysql_close();
25 ?> 
Nevermind. Solved the first problem.
New one.

When I submit the information, I get "Unknown column '(the value i entered for poster)' in 'field list'
Coming soon.

User avatar
edge
Redshirt
Posts: 3376
Joined: Mon Jun 02, 2003 9:43 pm
Gender: Male
Location: Pittsburgh, PA
Contact:

Post by edge » Mon Nov 07, 2005 2:26 pm

I'm not sure if you copy & pasted this or not, but for one thing, I see you're missing your ";" on line 13. Also, what's the i++; for on 15?

I would try for your query, "INSERT INTO Main VALUES (,$poster,$news)";
Not really sure about denoting an empty vaule like that. Also, check to be sure that "Main" is also capitalized in your db. If it's not hitting the right table name, you can get those kinds of errors (Since obviously it wont be able to find the columns).

Also, do you need to specify columns? I don't recall at the moment... INSERT INTO Main('Date','Poster','News') VALUES(',$poster,$news)

Something along those lines?

kyoryu
Redshirt
Posts: 153
Joined: Thu Apr 14, 2005 11:24 pm
Contact:

Post by kyoryu » Tue Nov 08, 2005 6:33 pm

I believe (but could be wrong, it's been a while) that MySQL is case insensitive on column/table names.

edge: It's not mandatory (last I checked!) but it's generally safer, as future modifications to the table then won't impact you. I could be wrong about it being mandatory, as the error seems to indicate that it thinks the value of $poster is a column name.

Also, I'd recommend using consistent formatting. It really does help readability, especially if you have to look at the code again in 6 months.

workmad3
Redshirt
Posts: 614
Joined: Mon Aug 22, 2005 6:51 pm

Post by workmad3 » Tue Nov 08, 2005 8:37 pm

Column headings are not mandatory, but if you dont use them you need to have something for every column in the order that it was created in whereas if you put them in you can omit columns from your insert, put them in an arbitrary order, and only insert the data you need (SQL standard, so MySQL should obey this) so for the insert query, use "INSERT INTO Main(poster, news) VALUES ($poster, $news);" assuming that poster and news are the names of the fields in the db. This stops the table having an arbitrary blank in a column, and instead shows a null for it.

Also, you seem to be running the command 'mysql_query($query);' twice, once in the code(line 14), and once in the if statement(line 16). Not sure on php(not really done much myself) but wouldnt that run the insert query twice? could that be causing your problem?
I'm still an atheist, thank god.
Christianity: The belief in an invisible santa
RLHLC
Image

Locked

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest