LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I need help with inserting data into mysql w/ PHP (https://www.linuxquestions.org/questions/programming-9/i-need-help-with-inserting-data-into-mysql-w-php-85231/)

lostboy 08-24-2003 03:32 PM

I need help with inserting data into mysql w/ PHP
 
This is the loop :

Code:

for($i=0;$i<$end;$i++)
{
  $form_array[$i]=$_GET[$i];       
  $infield = mysql_field_name($fname, $i);
  $sql2="INSERT INTO $tb_name ($infield) $form_array[$i]";
  mysql_db_query($db_name,$sql2);
 
  print $infield;
  print $form_array[$i];
 
}

$end = number of iterations
$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

sk8guitar 08-24-2003 04:50 PM

ok, well here is how i usually do this. first you connect to the database:

Code:

$database="localhost";
$datauser="username";
$datapass="password";
$conn=mysql_connect("$database", "$datauser","$datapass");

and i leave that at the top of my page. now do make a sql query you can just do this:

Code:

$sql="select * from <table>";
$result=mysql_query($sql);

;

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)
so you need to change your syntax to say

Code:

$sql2="INSERT INTO $tb_name ($infield) VALUES ($form_array[$i])
you may need to put single quotes around $form_array[$i] if its a phrase (or the table is expecting a phrase i.e. its a column of type varchar, or text, or something along those lines).

so long post short, you just need to add the word values.

lostboy 08-25-2003 09:13 AM

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

sk8guitar 08-25-2003 12:08 PM

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.