LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2003, 03:32 PM   #1
lostboy
Member
 
Registered: Mar 2003
Location: Florida
Distribution: Slackware 9.1,10.1
Posts: 268

Rep: Reputation: 30
Talking 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
 
Old 08-24-2003, 04:50 PM   #2
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
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.
 
Old 08-25-2003, 09:13 AM   #3
lostboy
Member
 
Registered: Mar 2003
Location: Florida
Distribution: Slackware 9.1,10.1
Posts: 268

Original Poster
Rep: Reputation: 30
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
 
Old 08-25-2003, 12:08 PM   #4
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem inserting data into a mysql table using PHP Rockgod2099 Programming 13 08-03-2005 12:27 AM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
inserting data in mysql zuessh Linux - Software 4 05-19-2003 01:00 PM
Problem Inserting MySQL data from PHP! rhuser Linux - Software 9 03-03-2003 07:56 PM
MySQL - Inserting Data Not Happening! Rhapsodic Programming 4 10-26-2002 10:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration