Lukas Lukas - 2 months ago 19
PHP Question

PDO update row value in foreach loop

I created a table, inside a foreach loop displaying in the table all the records. Then I included a SQL update with the select and update one array value. When I select different value, the loop updates all the records in the table and NOT only one selected to be updated. I was all day struggling with it, please help me.

If foreach is a wrong way, I would highly appreciate your suggestions of how to display a table that would display records and permit updates for each array.

<?php
foreach ($stmt as $key => $row)
{

$newcustomerid = htmlentities($row['customer_id']);

echo '<tr><td>' . htmlentities($row['customer_company']) . '</td>';
echo '<td> id:' . $newcustomerid . 'key: ' . $key .'</td>';
echo '<td>' . htmlentities($row['customer_email']) . '</td>';
echo '<td>' . htmlentities($row['customer_phone']) . '</td>';
echo '<td>' . htmlentities($row['customer_country']) . '</td>';
echo '<td>' . htmlentities($row['customer_city']) . '</td>';
echo '<td>' . htmlentities($row['customer_segment']) . '</td>';
echo '<td>' . htmlentities($row['customer_updated']) . '</td>';
$customer_status = htmlentities($row['customer_status']);

if(isset($_POST['Submitbb'])){ //check if form was submitted
$input = $_POST['Submitbb']; //get input text

$stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
$stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $row['customer_id']));
}

echo '<td>
<form action="" method="post">
<select name="Submitbb" onchange="this.form.submit();">
<option> - ' . $customer_status . ' - </option>
<option>Susisiekti</option>
<option>Priminimas 1</option>
<option>Priminimas 2</option>
<option>Paskambinti</option>
<option>Netinkamas klientas</option>
</select>
</form>
</td>';


echo '<td>' . 'Išsaugoti' . '</td></tr>';

}



?>


Thank you.

Answer

You could use a hidden input to capture the row in the database you wish to update

 echo '<td>
                <form action="" method="post">
                    <input name="customer_id" type="hidden" value="'.$newcustomerid.'"/>  <!-- hidden -->
                    <select name="Submitbb" onchange="this.form.submit();">
                        <option> - ' . $customer_status . ' - </option>
                        <option>Susisiekti</option>
                        <option>Priminimas 1</option>
                        <option>Priminimas 2</option>
                        <option>Paskambinti</option>
                        <option>Netinkamas klientas</option>
                        </select>
                </form>
            </td>';

Then compare it when the form is posted

            if(isset($_POST['Submitbb'])){ //check if form was submitted
            $input = $_POST['Submitbb']; //get input text
            $customer_id =  $_POST['customer_id']; // get the posted customer ID
            if($row['customer_id'] == $customer_id){

                    $stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
                    $stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $customer_id)); 
                }
            }
Comments