Adding Data from an Array into a MySQL DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Adding Data from an Array into a MySQL DB

Post by GlennCurtis2009 »

Hi guys,

As you can see from my code iam attemping to add data from an array that goes up to 20 possiable fileds, some maybe empty, and in the tests iam runing i only use 3 fields. But its not working, even when i select $SQL2 which only has the three fileds seleted, it still will not add the test data to the DB.

All i get back from my if statement is 'Error'.......

I have not worked with MySQL in some years and I think there is something iam doing wrong - there must be or it would be working :D - but i can not see what it is, please help?

Code: Select all

 
 
$SQL1 = "INSERT INTO Billing (Item01, Item02, Item03, Item04, Item05, Item06, Item07, Item08, Item09, Item10, Item11, Item12, Item13, Item14, Item15, Item16, Item17, Item18, Item19, Item20) VALUES ($Form_In[1], $Form_In[2], $Form_In[3], $Form_In[4], $Form_In[5], $Form_In[6], $Form_In[7], $Form_In[8], $Form_In[9], $Form_In[10], $Form_In[11], $Form_In[12], $Form_In[13], $Form_In[14], $Form_In[15], $Form_In[16], $Form_In[17], $Form_In[18], $Form_In[19], $Form_In[20] )";
 
$SQL2 = "INSERT INTO Billing (Item01, Item02, Item03) VALUES ('test1', 'test2', 'test3')";
 
$SQLRun = mysql_query($DB_Link, $SQL2);
 
if (mysql_affected_rows($DB_Link) == 1) {
        print ("your db data has been added");
} else {
        print ("Error");
}
 
 


This is how iam connecting to my database, this is held within another file and i use require_once to access it - this works even when i want to access the database to print information from it, which i can do, just not add data to it!

Thanks Glenn

Code: Select all

 
 
<?php
$Host = "localhost";
$User = "USER";
$Pass = "PASSWORD";
$DB_Name = "DB_NAME";
 
$DB_Link = mysql_pconnect($Host, $User, $Pass);
mysql_select_db($DB_Name, $DB_Link);
?>
 
 
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: Adding Data from an Array into a MySQL DB

Post by Skara »

what is the output of

Code: Select all

if (mysql_affected_rows($DB_Link) == 1) {
        print ("your db data has been added");
} else {
        print (mysql_error()); // <<<
}
?
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Re: Adding Data from an Array into a MySQL DB

Post by GlennCurtis2009 »

Hi,

Nothing at all, the top line does not print but the mysql_error commmand does not print / display any errors or reports about whats going wrong.

Glenn.
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Re: Adding Data from an Array into a MySQL DB

Post by GlennCurtis2009 »

Its all right now i seem, and yes i use that word correctly, seems to be working.

Code: Select all

 
 
3.$SQL1 = "INSERT INTO Billing (Item01, Item02, Item03, Item04, Item05, Item06, Item07, Item08, Item09, Item10, Item11, Item12, Item13, Item14, Item15, Item16, Item17, Item18, Item19, Item20) VALUES ($Form_In[1], $Form_In[2], $Form_In[3], $Form_In[4], $Form_In[5], $Form_In[6], $Form_In[7], $Form_In[8], $Form_In[9], $Form_In[10], $Form_In[11], $Form_In[12], $Form_In[13], $Form_In[14], $Form_In[15], $Form_In[16], $Form_In[17], $Form_In[18], $Form_In[19], $Form_In[20] )";
 
 
The current method is to place the $Form_In[X] into '$Form_In[X]' with the single marks.



Thanks Glenn.
Post Reply