Jump to content

parse error: syntax error, unexpected T_AS


abs0lut

Recommended Posts

Do $niqid and $_POST['cost'] have the same number of elements? And are they numerically indexed consecutively from 0?

 

<?php
for ($i=0; $i<count($niqid); $i++) {
    $query = "INSERT INTO ctable (name, cost, date, post)
              VALUES ('".$niqid[$i]."', '".$_POST['cost'][$i]."', '$date', '$post')";
    $sql = mysql_query($query) or die (mysql_error());
}
?>

Do $niqid and $_POST['cost'] have the same number of elements? And are they numerically indexed consecutively from 0?

 

<?php
for ($i=0; $i<count($niqid); $i++) {
    $query = "INSERT INTO ctable (name, cost, date, post)
              VALUES ('".$niqid[$i]."', '".$_POST['cost'][$i]."', '$date', '$post')";
    $sql = mysql_query($query) or die (mysql_error());
}
?>

how will I know if  $_POST['cost'] and $niqid have the same number of elements?

Is $_POST['cost'] actually an array?

 

You could try it like this

 

<?php
foreach($niqid as $ids) {

foreach($_POST['cost'] as $pids) {
        $sql = mysql_query("INSERT INTO ctable (name, cost, date, post)
            VALUES ('$ids', '$pids', '$date', '$post')") or die (mysql_error());
	}
}
?>

 

Sam

how will I know if  $_POST['cost'] and $niqid have the same number of elements?

 

You, as the developer, should know where your variables are coming from, what they "should" contain, and their format. So, why are you asking me? If they do not have the same number of elements then I don't have a clue what you are tring to accomplish in the code you posted - which is syntactically incorrect. It appeared to me you were trying to iterrate through both arrays simultaneously an run those queries. For example if the first array contains 1, 2, & 3 and the second array contains A, B, & C you would want queries using the combinations 1A, 2B, & 3C. However, if you want to do a matrix between the two arrays (1A, 1B, 1C, 2A, 2B, 2C, 3A, 3B, & 3C) then that is something different (which I think helraizer's code addresses). Since you didn't state what you are trying to do, I made a guess.

 

I put this in my sig for a reason:

The quality of the responses received is directly porportional to the quality of the question asked.

I'm still not sure/convinced that $_POST['cost'] can be an array though. I could be wrong, but it's a single posted value, surely? Therefore foreach will have no effect on it. Although you aren't recieving an error for it, so I could well be wrong.

 

Is it?

 

Sam

I'm still not sure/convinced that $_POST['cost'] can be an array though. I could be wrong, but it's a single posted value, surely? Therefore foreach will have no effect on it. Although you aren't recieving an error for it, so I could well be wrong.

 

Is it?

 

Sam

 

Yes you can have POST values that are arrays - just give multiple fields the same array name. Here's an example of a form where an array would be returned:

 

<input type="text" name="cost[]">
<input type="text" name="cost[]">
<input type="text" name="cost[]">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.