Stop sql query from updating all columns in each loop

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 into db table to know current voting score 
         $query = mysql_query("SELECT like1 FROM comment WHERE id = '$com_id[$i]'");

         # increase or dicrease voting score
         if ($data = mysql_fetch_array($query)) {

               $like = ++$data['like1'];

               # update new voting score
               $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, . .

+4
3

:

<?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 = like + 1 WHERE id IN (".implode( ',', $idArray ).")" );
}
+2

? 'lkcv', , FOR lkcv.

, 50 50 , 50 .

, "like1" UPDATE, ...

UPDATE comment SET like1 = like1 + 1 WHERE id = '$com_id[$i]'

UPDATE comment SET like1 = like1 + 1 WHERE id = '$com_id'

, .

0

Why do you need to use select and update to increase the value by 1? why not use directly in mysql? This reduces code complexity. Try this as shown below and check the values. If the problem still exists, debug it with print_r, echo and exit.

<?php
if(isset($_POST['lkcv']) && isset($_POST['like'])){
  if (is_array($_POST['lkcv'])) {
    foreach($_POST['lkcv'] as $value){
               $query2 = mysql_query("UPDATE comment SET like1  = like1 + 1 where id = '$value'");
    }
  } 
}
?>

Hint: do not use sizeof (). Use count () instead. link.

0
source

Source: https://habr.com/ru/post/1599484/


All Articles