Forum Moderators: coopster

Message Too Old, No Replies

Problem entering information into a database

         

TymArtist

4:35 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



This is most likely a newb error so bear with me, but I copied the code from our previous webmaster's form submit page and put it on a new form submit page that I created. It worked perfectly on the other but it will not submit the data to the table I have specified (I modified it somewhat, but it didn't work either way) Please help!

He's got the database connection on an external file, so it's simply calling to that and then entering the data into a table - i replaced the variables and file names with generics for sake of example and help...

require("database.php");
database_connect();
mysql_select_db("mydatabase");
$query = "insert into table ('$var1', '$var2', '$var3', '$var4', '$var5')";
$result = mysql_query($query);

what am i doing wrong here? obviously once i pick up a bit more about PHP i won't have many errors, but it's kind of an on-the-job learning process :)

also, if anyone has a better suggestion on how I should be connecting to the database, i'm up for hearing about it. like i said, i have an inherited system, so suggestions are something i'll definitely be willing to take. thanks all!

stargeek

4:47 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



do a mysql_error(); and see what it says.

TymArtist

5:09 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



i added "or die("message");" first, then varied with "or die(mysql_error());", but neither returned any errors in the database entry process. there is still no data in the table. is there anything specific i have to do with the database in order to get it to enter correctly? i have all the columns in the same order in the code and database, and didn't think there was anything else i could possibly check. so frustrating!

stargeek

5:10 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



did you try mysql_error to see what the error is?

jatar_k

5:20 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld TymArtist,

I don't think that query will work and should return a syntax error. Add a couple more or dies and see.

require("database.php");
database_connect();
mysql_select_db("mydatabase") or die ('<p>selectdb: ' . mysql_error());
$query = "insert into table ('$var1', '$var2', '$var3', '$var4', '$var5')";
$result = mysql_query($query) or die ('<p>insertquery: ' . mysql_error());

there is also the actual connection which I assume is in 'database.php' you should look to see if that has one as well or that it handles the case where the connection fails.

another thing is the query itself
$query = "insert into table ('$var1', '$var2', '$var3', '$var4', '$var5')";

what is your table name? the syntax is slightly off as well

$query = "insert into tablename values ('$var1', '$var2', '$var3', '$var4', '$var5')";

so your query should have been giving you an error of some kind.

TymArtist

8:08 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



ended up getting 'insertquery: Column count doesn't match value count at row 1'
trying to research the fix now, as it appears to be a rather common newb mistake. thanks for your help, jatar!

jatar_k

8:14 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you use this syntax

insert into tablename values ('value1', 'value2', 'value3', 'value4', 'value5')

the number of values must match the column count and be in the appropriate order

if you do it this way

insert into tablename (column1, column2, column3, column4, column5) values ('value1', 'value2', 'value3', 'value4', 'value5')

then you set the columns and what their values are, they don't have to match total number or order

dmmh

8:27 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



he forgot the 'VALUES' part alltogether :)

TymArtist

8:27 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



wow total newb error.
#1 - forgot the $ in front of one of the variables
#2 - had one too many fields in the database

for the record, values was originally there, but i took it out because i saw a number of web references that did not include it. no need to knock my skills :)

entry works successfully now! on to phase 3: form validation. that should be...interesting. anyone know any good tutorials?

dmmh

8:32 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



well in that case Ive learned something, thought it was needed ;)

coopster

8:42 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For clarification, the VALUES list is necessary, the optional column list is not. The difference being if you don't use the optional column list ... well, read jatar_k's post again ;)

dmmh

8:46 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



so I was right after all :)