problem with Array

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
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

problem with Array

Post by nite4000 »

A while back i had this code done for me and now its giving me trouble. nothing has been changed and nothing added. but its notgetting the array anymore. I hope someone can help me out

Code: Select all

$values = array();

for($i = 0; $i < $qty; $i++) {

$values[] = "(null,'$postion','$qty','$postion','$join','Inline')";  // create one line for INSERT

}

if(count($values) > 0) {

	$values = implode(',', $values);  // make full line for INSERT

}

$query=mysql_query("INSERT into line(id,pid,qty,spent_amt,date,status)VALUES".$values)or die(mysql_error());

unset($values);  // some cleanup

the INSERT into line is the problem however its reverting back to VALUESARRAY as the error so the array isnt being read or something.


This is the error I get
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUESArray' at line 1

Any ideas would help out.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: problem with Array

Post by x_mutatis_mutandis_x »

your count($values) might be returning a 0 in which case $values wont get 'imploded' to a string. Do something like this:

Code: Select all


if(count($values) > 0) {
 
        $values = implode(',', $values);  // make full line for INSERT
        $query=mysql_query("INSERT into line(id,pid,qty,spent_amt,date,status)VALUES".$values)or die(mysql_error());
        //execute query here
 
} else {
        echo 'No values to insert'; //or better log the error
}

unset($values); //some cleanup
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: problem with Array

Post by nite4000 »

I tried that and it come back saying no values to insert and i chked the table and nothing was saved.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: problem with Array

Post by x_mutatis_mutandis_x »

nite4000 wrote:I tried that and it come back saying no values to insert and i chked the table and nothing was saved.
Check the value of "$qty" variable, it might be zero, in which case your $values array will be empty. But what I don't understand is why are you looping through $qty times, and inserting rows with same values?
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: problem with Array

Post by nite4000 »

this is a form where they can purchase more then 1 item and if they enter 2 then it does the inserting 2 times
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: problem with Array

Post by x_mutatis_mutandis_x »

nite4000 wrote:this is a form where they can purchase more then 1 item and if they enter 2 then it does the inserting 2 times
Have you checked the value of $qty? If your $qty is 0, then nothing gets inserted as your $values will be empty as well (since your looping $qty (0) times).
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: problem with Array

Post by nite4000 »

I am chking it now so far its zero so then i am gonna debug a few other things to be sure
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: problem with Array

Post by nite4000 »

i got it working .. the way i have the form setup i had 2 boxes with name of qty and it was reading that box as well so in turn wasnt inserting i have renamed it and all is working. Thanks
Post Reply