954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mysql php row insertion

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);
	
	
	mysql_close($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.

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 

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

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

cereal,
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.

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 

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

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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 :)

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You