Many questions have already been asked on this question. I reviewed some of the solutions given in stackoverflow, but none of them work. I have a form with a foreach loop, and I'm trying to submit a "selected form" with its unique value, and then run an SQL query with the value.
The problem is the attribute of the text area name. At first, the query will only update the last column in the loop. Then I tried a different approach. For example, changing the name attribute to "someattribute []".
Now all selected columns in the query are updated.
form.php
<?php
...................
foreach($stmt as $obj){
$id = $obj['id'];
$likes = $obj['like1'];
echo '<form action="" method="post" enctype="multipart/form-data">';
echo '<input type="hidden" name="lkcv[]" value="'.$id.'">';
echo '<input type="hidden" name="like" value="">';
echo '<input type="image" src="images/like.png" id="lksub" width="15"
value="som" height="15" style="float:right;position:relative;margin-
right:290px;"/><div class="ld">'.$likes.'</div>';
echo '</form>’;
}
?>
query.php
<?php
if (isset($_POST['like'])){
$com_id = $_POST['lkcv'];
for ($i=0; $i<sizeof($com_id);$i++){
$query = mysql_query("SELECT like1 FROM comment WHERE id = '$com_id[$i]'");
if ($data = mysql_fetch_array($query)) {
$like = ++$data['like1'];
$query2 = mysql_query("UPDATE comment SET like1 = '{$like}' WHERE id = '$com_id[$i]'");
}
}
}
?>
query.php [RESOLVED]
<?php
if( isset( $_POST['lkcv'] ) && is_array( $_POST['lkcv'] ) )
{
$idArray = array();
foreach( $_POST['lkcv'] as $value )
{
$idArray[] = intval( $value );
}
$db->query( "UPDATE comment SET like1 = like1 + 1 WHERE id IN (".implode(
',', $idArray ).")" );
}
?>
. , PDO, . .