Hey there,

I am trying to build a small survey for a homework assignment.
So, I have the following code:

$con = mysql_connect("localhost","user", "mypassword");
	if (!$con)
		die('Could not connect: ' . mysql_error());
	mysql_select_db("mydatabase", $con);

	$slq = "INSERT INTO results VALUES('', '', '', '', '', '', '', '', '', '', '', '')";
	mysql_query($sql, $con);

What is even more weird is that I use this same sql statement through command line, as well as through phpMyAdmin, and it inserts a row in my results table in both cases, but it won't insert a row when I use it in a script.

Anyone knows what might I be doing wrong?

thank you.

Change line 12 with mysql_query($sql, $con) or die(mysql_error()); and see what it happens, bye.

I tried that but nothing happened. no error reporting, no row insertion. However, after rewriting my sql statement to include column names as:

INSERT INTO results (column name) VALUES ('val', 'val', 'val'....);

everything went fine. I guess I will mark this thread solved now.

thank you very much.
perhaps I have error reporting turned to off in my server or something.

Member Avatar

BTW you can use the SET syntax as well:

INSERT INTO table SET field1='val1',field2='val2',field3='val3'

However this will only allow you to insert a single record at a time, the VALUES syntax allows insertion of many records in one query. I find the SET syntax easier to keep an eye on. The VALUES syntax can be tricky, especially if you include the autoincrement field in the query. My 2p

yes ardav,
I use
UPDATE tableName SET columnName = 'someValue'
for questions number 2 and above, but I use an INSERT statement to insert a row with null values for all columns, except the first, auto incremental one.

thank you for your two cents :)