0

Hi friends,

I am trying to user nested foreach to insert data into table with three different column fields. Please look at that because i am getting error [syntax error, unexpected ',', expecting ')'].

$cLists = $_POST['nd'];

$Votes = $_POST['text'];

$Votes_p = $_POST['text1'];

$id = $_POST['invst_id'];

//foreach($cLists as $nomineeid) 
foreach (array_combine($cLists, $Votes, $Votes_p) as $nomineeid => $voted, $p_vote){

//foreach ($Votes_p as $voted_p => $p_vote) {

$query = "INSERT INTO election (invst_id_ar , nomine_id, vote_gain, vote_gain_p, casted_d_t) VALUES ('$id', '$nomineeid', '$voted', '$p_vote', NOW())";
}
}

--
Kind Regards
Have a G(::)D DaY!

3
Contributors
33
Replies
145
Views
4 Years
Discussion Span
Last Post by broj1
0

on which line? better yet include the code for the file providinf values for the $_POST variables

0

on which line?

Error comming when using of 3 variables ($cLists, $Votes, $Votes_p) at line 10, except in that if i remove any one of them its working fine.

include the code for the file providinf

for $Clists i have multiple input fields with same name nd[]
$Votes input name = text[] (for all input fields)
$Votes_p input name = text1[] (for all input fields)

--
Thanks you so much...!!!

1

I think the $nomineeid => $voted, $p_vote part in line 10 is not valid syntax.

foreach (array_combine($cLists, $Votes, $Votes_p) as $nomineeid => $voted, $p_vote){

The foreach construct is defined as

foreach (array_expression as $key => $value)

so in your case PHP expects a ) instead of a , after $voted. What do you actually want to achieve?

Edited by broj1

0

Thanks broj1,

can you please tell me than how i'll run insert query to insert all of $(varibales)such as $nomineeid, $voted, $p_vote

--
Kind Regards

0

You are actually not using the array_combine function correctly. This function's parameters are expected to be arrays but you are feeding it strings. What is the purpose of it?

0

if i am not i also worried about array_combine(its used only for to combine two arrays only), may i know what should be the right function for it.

--
Kind Regards

0

What is supposed to be in these variables (I guess the output of the form)?

$cLists = $_POST['nd'];
$Votes = $_POST['text'];
$Votes_p = $_POST['text1'];
$id = $_POST['invst_id'];

Edited by broj1

0
if(isset($_POST["Save"]))
{
$nd = $_POST['nd'];
$vote = $_POST['text'];

if(isset($_POST['nd']) && $_POST['text']) {
//$cListS = array();
$cLists = $_POST['nd'];

$Votes = $_POST['text'];

$Votes_p = $_POST['text1'];

$id = $_POST['invst_id'];

//foreach($cLists as $nomineeid) 
foreach (array_combine($cLists, $Votes, $Votes_p) as $nomineeid , $voted => $p_vote){

//foreach ($Votes_p as $voted_p => $p_vote) {

$query = "INSERT INTO election (invst_id_ar , nomine_id, vote_gain, vote_gain_p, casted_d_t) VALUES ('$id', '$nomineeid', '$voted', '$p_vote', NOW())";



//$result=mysql_query($query) or die (mysql_error(). "<br />not updated");

if (!$res = mysql_query($query)){
    echo mysql_error(); die("ERRRRORORORORORORO");
   }
//  }
    }
    }
}   
0

OK the processing code looks right but what is supposed to be the contents of the POST variables (what data are you asking a user to fill in)?

$cLists = $_POST['nd'];
$Votes = $_POST['text'];
$Votes_p = $_POST['text1'];
$id = $_POST['invst_id'];
0
<table>
<tr>
<td id="a1" class="box190">&raquo;&nbsp;H.E<input type="hidden" name="nd[]" value="1" /></td>
          <td align="right"><input name="text1[]" type="text" class="box25" id="r1" value="0" onkeyup="budCalc();" />
            %</td>
          <td align="center"><input name="text[]" type="text" class="box70c" id="c1" readonly="readonly" /></td>
        </tr>
        <tr>
          <td class="box190" id="a2">&raquo;&nbsp;Hamad<input type="hidden" name="nd[]" value="2" /></td>
          <td align="right"><input name="text1[]" type="text" class="box25" id="r2" value="0" onkeyup="budCalc();" />
            %</td>
          <td align="center"><input name="text[]" type="text" class="box70c" id="c2" readonly="readonly" /></td>
        </tr>
        <tr>
          <td class="box190" id="a3">&raquo;&nbsp;Obeid<input type="hidden" name="nd[]" value="3" /></td>
          <td align="right"><input name="text1[]" type="text" class="box25" id="r3" value="0" onkeyup="budCalc();"/>
            %</td>
          <td align="center"><input name="text[]" type="text" class="box70c" id="c3" readonly="readonly" onkeyup="budCalc();"/></td>
          </tr>
          </table>
0

OK, so you have repeating four inputs (one hidden) and they actually create arrays in $_POST elements, right? in $_POST array you get three elements which are also arrays and those arrays should have same number of elements. Now, to construct a query you should use common iterator for all four arrays, something like:

// count the elements in one of the arrays (ideally the count should be equal for all three arrays)
$count = elementCount($_POST['text1'])

// construct the first part of the query
$query = "INSERT INTO election (invst_id_ar , nomine_id, vote_gain, vote_gain_p, casted_d_t) VALUES ";

// now loop through all the arrays and add values to the query
for($i = ; $i < $count; $i++) {

    // first escape the values (you can do other checks here)
    $nd = mysqli_real_escape_string($_POST['nd'][$i]);
    $Votes = mysqli_real_escape_string($_POST['text'][$i]);
    $Votes_p = mysqli_real_escape_string($_POST['text1'][$i]);
    $id = mysqli_real_escape_string($_POST['invst_id'][$i]);

    // add to the query
    $query .= "('$id', '$nomineeid', '$voted', '$p_vote', NOW())";

    // if it is not the last element add comma at the end of value set
    if($i < $count - 1) {

        $query .= ',';
    }
}

The code is not tested but I hope you get the principle.

Edited by broj1

0

67f82f76a51f1835b1503aa29de52b8d 100% right principle :)

but need one change if let say invst_id '1' as shown in attached that 1 should be repeat for all.

Edited by ismael ahm@d: attached

0

actually i have total (invst_id(1 to 150)) persons which will vote to each 10 nominees (nomine_id (1 to 10)),
in a simple words (150 * 10 = 1500) records i wil save in db.

--
Kind Regards

0

i have already created a master table for it, in which 1 to 150 range defined.
and in future i will match record between master and child table

Edited by ismael ahm@d: +

0

I so thinkless ...:( I got it the above point and now it working as per my request.

Dear broj1, I have one Qs more, how to get back these value in their fields agian if i update inserted data.

0

In this case you better construct the html code for table rows programatically with php. The current entered values are stored in $_POST. So you can set each field's value attribute to corresponding $_POST element if exists (you have to check if exists using isset()).

<?php
echo '<table>';

// I do not know how many rows do you have, I'll do it by your example of three rows
for($i = 0; $i < 3; $i++) {

    // row number
    $rowNo = $i + 1;

    echo '<tr>';
    echo "<td id=\"a$rowNo\"":
    echo 'class="box190">&raquo;&nbsp;<input type="hidden" name="nd[]" value="';
    // check if value exists in $_POST and if yes asign it to the attribute
    // otherwise assign row number (judging from your code)
    echo isset($_POST['nd'][$i]) ? $_POST['nd'][$i] : $rowNo;
    echo '" /></td>';

    ...
}

echo '<table>';
?>

Again this is just an example of principle. I am not sure how the logic in your code works so please adapt it.

Edited by broj1

0

Thanks for getting back,

may i know please how to be write update query for this?

0

The query does not change. The code above is just for constructing the table and getting posted values into input boxes. Or have I understood your question incorrectly?

0

yes i know table fields will get/post the values, but how i will write a code to update in db table

update table_name Set ..... ?

0

Dear broj1,

would you like to tell me how i get the inserted data in form if i have need to update it...please clarify it.

0

I am not sure if I understand the question. Which particular data do you want to update and with what data? You probably know how do do the update query:

$query = "UPDATE election SET nomine_id='$nomineeid', vote_gain='$voted', vote_gain_p='$p_vote', casted_d_t=NOW() WHERE invst_id_ar='$nd'";

This is again just an example since I do not know what you want to do. It would help if you explain what is on the screen and what is used supposed to do.

Edited by broj1

0

Dear broj1,

with thre refernce of last Insert Query post

$count = count($_POST['text1']);
// now loop through all the arrays and add values to the query

for($i=0;$i<$count;$i++){
// first escape the values (you can do other checks here)
    $nd = mysql_real_escape_string($_POST['nd'][$i]);
    $Votes = mysql_real_escape_string($_POST['text'][$i]);
    $Votes_p = mysql_real_escape_string($_POST['text1'][$i]);
    $id = $_POST['invst_id'];
    // add to the query
    $query .= "('$id', '$nd', '$Votes', '$Votes_p', NOW())";
    // if it is not the last element add comma at the end of value set
    if($i < $count - 1) {
        $query .= ',';
    }
    }

when i am going for to write update code like this...

$count = count($_POST['text1']);
$used_count = count($_POST['text']);
$iid = $_POST['invst_id'];
// construct the first part of the query

// now loop through all the arrays and add values to the query
for($i = 1; $i < $count; $i++) {
    // first escape the values (you can do other checks here)
        $nnd = mysql_real_escape_string($_POST['nd'][$i]);
    $Votes = mysql_real_escape_string($_POST['text'][$i]);
    $Votes_p = mysql_real_escape_string($_POST['text1'][$i]);

// echo "B | nomine_id = +" . $nd . "+ | Invster_id = +".$id. "+ | votes_in_percent = " . $Votes_p. "<br />";
    // add to the query


  $u_query = "UPDATE election SET
                vote_gain = '$Votes',
                vote_gain_p = '$Votes_p',
                casted_d_t = NOW()
                WHERE invst_id_ar = '".trim($iid)."' AND nomine_id = '$nnd'";
echo "|" .$i."|".$u_query ."<br />";

the above code now update last row in the table, remaining above (9) rows not updated......

Please check if i did any mistake.

Cheers :)

Edited by ismael ahm@d: +

Attachments updated_last_row_only.JPG 86.1 KB
0

The queries look OK. There could be two reasons for the error (as far as I can see):

  1. The values for the invst_id_ar are not changing, they are 8 all the time. But this could be OK if that is the structure of your data.
  2. Maybe the query execution happens outside of the for loop. You should submit the query immediately after the query is being assembled (after line 21 in the above ocde).
0

Thanks for getting back,

  1. Yes, it is OK
  2. i forgot to put } at end, actually update query running inside for loop, but after all it updates and shows the last record only. is it posible to handle or control the loop, i mean to display only a one record from loop.

--
Kind Regards

Edited by ismael ahm@d: +

0

is it posible to handle or control the loop, i mean to display only a one record from loop

Yes, if there is any value that you can compare it to the counter $i.

for($i = 1; $i < $count; $i++) {

    ...

    // compare the counter to something and break out of loop if true
    if($i == somecondition) {
        break;
    }


}
0

okay... after adding condition like this...

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

if ($i == 5){
echo "the number is " . $count. "<br />";
//echo "the number is " . $i. "<br />";
break;
}

i am getting the result when i used echo $i = 5 else on echo $count = 10. the attached file is for your reference.

Edited by ismael ahm@d: +

Attachments condition.JPG 82.58 KB
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.