I need help with inserting data into mysql w/ PHP
This is the loop :
Code:
for($i=0;$i<$end;$i++) $form_array[$i] = the text input $infield = the field name $db_name = database name $tb_name = table name Assume that all variables work correctly (they do). What happens is the print statements in the bottom of the loop verify that : 1. $infield = the field name 2. $form_array[$i] = the field value (which is text input) However, this line : $sql2="INSERT INTO $tb_name ($infield) $form_array[$i]"; is not causing $form_array[$i] to be inserted into $infield . Please help. JC |
ok, well here is how i usually do this. first you connect to the database:
Code:
$database="localhost"; Code:
$sql="select * from <table>"; now that way you can test to see if its been inputted or not. $result will be given a true if it was succesful or a false if not. so you can just use a simpel if then statement to output to the screen if it was all good or not. so, assuming that you are connecting to the database correctly, the reason you aren't getting things entered in is because your sql syntax is wrong. the sql syntax for entering something into a table is Code:
insert into table_name (column list) values (item list) Code:
$sql2="INSERT INTO $tb_name ($infield) VALUES ($form_array[$i]) so long post short, you just need to add the word values. |
Thanks. I got it ironed out.
"you may need to put single quotes around $form_array[$i]" When I first tried to use 'VALUES' in the query, I did not have single quotes. And it of course did not work. I tried double quotes, and I got errors. Also, one other mistake : $sql2="INSERT INTO $tb_name ($infield) VALUES('$form_array[$i]')"; Here is the good one : $sql2="INSERT INTO $tb_name ($infield) VALUES('$form_array[$i]');"; Notice the semicolon after ---> ('$form_array[$i]); <----right here Once again, thanks JC |
actdually you don't need to semicolon (and in fact, SHOULDNT have the semicolon) in a php mysql call. only in the mysql prompt do you need the semicolon. :-P
|
All times are GMT -5. The time now is 05:44 AM. |