Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you enter lengthy mySQL commands with PHP? 2

Status
Not open for further replies.

Eloff

Programmer
Aug 29, 2001
78
CA
I have a mysql insert command that is rather lengthy since it inserts 52 columns of data. Since it won't all fit on one line i have something like the following.

$query = "insert into table_name (col1, col2, col3,".
etc) values ('$col1', '$col2', '$col3',".
etc)";

MYSQL_QUERY($query);

trouble is it inserts nothing into the database. Does everything look okay to you? is "." a proper way to continue quotes (concatenate I think its called) onto the next line? Age: 17
School: Alberta Distance Learning Center
Location: British Columbia, Canada
If at first you dont't succeed, try, try again. - programmer's motto.
 
check if there's not an error occuring while running the query (mysql_error())

and i would create the query string this way
$query = "insert into table_name (col1, col2, col3) values ('".$col1."', '".$col2."', '".$col3."')";

the use of quotes depends on the column type, i think for numbers can be omited

 
Piti you are right, only strings need quotes. But if there are so many variables to insert i would write out the whole query and do checks:

$query = "insert into table_name (";

foreach($column as $col) {
$query .= "$col, ";
}
$query .= ") values (";

foreach ($value as $val) {
$query .= "'".addslashes($val)."', ";
}

This is much easier for 52 values. However if it goes wrong you dont know where it does. The other option is to write all 52 out. And i guess that you dont want to do that. mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
well i have to disagree with you, as long as you are the programmer, you always know where it goes wrong ;-)
 
Thanks guys it turns out the error was simply a spelling mistake on my part. Thanks to Piti for the mysql_error() function which told me where the mistake was. Age: 17
School: Alberta Distance Learning Center
Location: British Columbia, Canada
If at first you dont't succeed, try, try again. - programmer's motto.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top