Page 1 of 1

problem with Array

Posted: Mon Apr 30, 2012 8:24 am
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.

Re: problem with Array

Posted: Mon Apr 30, 2012 9:03 am
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

Re: problem with Array

Posted: Mon Apr 30, 2012 9:17 am
by nite4000
I tried that and it come back saying no values to insert and i chked the table and nothing was saved.

Re: problem with Array

Posted: Mon Apr 30, 2012 9:56 am
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?

Re: problem with Array

Posted: Mon Apr 30, 2012 9:58 am
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

Re: problem with Array

Posted: Mon Apr 30, 2012 10:13 am
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).

Re: problem with Array

Posted: Mon Apr 30, 2012 10:16 am
by nite4000
I am chking it now so far its zero so then i am gonna debug a few other things to be sure

Re: problem with Array

Posted: Mon Apr 30, 2012 10:22 am
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