Jump to content

parse error: syntax error, unexpected T_AS


abs0lut

Recommended Posts

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

please help me...

Link to comment
Share on other sites

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());
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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[]">

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.