-1

Possible Duplicate:
handling checked checkboxes PHP

i have this problem. I have a webpage that show car registration number and it's violation. And we can change the status of the violation from 1=not treated to 2=treated. I want to use multiple check box to choose which car registration status that i want to change here's the screenshot of my web

)

how i change the status of both car registration number ? here's my webpage code

 <div id="content">
    <div class="content_item">
    <?php
       $con = mysql_connect("localhost","fpjarmul","fpjarmul");
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }

        mysql_select_db("fpjarmul", $con);

        $query = "SELECT * FROM laporan WHERE status = '1'";
        $result = mysql_query($query);

        while($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {?>
            <form action="ubahdata.php" method="post">
                <input type="checkbox" name="idlaporan" value="<?php echo $row['idlaporan'] ?>" /><?php echo "ID : {$row['idlaporan']}" ?><br />
                <?php echo  "Nomor Polisi : {$row['platkendaraan']} <br>" .
                            "Status : {$row['status']} <br>" . 
                            "Tanggal Laporan : {$row['tanggallapor']} <br><br>"; ?>

        <?php   
        } 
        ?>  
                <input type="submit">
            </form>

and here's my script

 <?php include 'header.php'; ?>
 <?php
$con = mysql_connect("localhost","fpjarmul","fpjarmul");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

 mysql_select_db("fpjarmul", $con);

 $sql=("UPDATE laporan set status='2' where idlaporan='$_POST[idlaporan]'");

 if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
?> 
 <?php include 'footer.php'; ?>
4

1 に答える 1

1

次のアプローチを試してください。

  1. チェックボックスの「名前」を「idlaporan[]」に変更します (<input type="checkbox" name="idlaporan[]" )
  2. フォームの送信後、選択したチェック ボックスの値がサーバー側の配列 $_POST['idlaporan'] に表示されます。
  3. foreach ループを使用して、データベースの値を更新します。

    foreach ($_POST['idlaporan'] as $idlaporan) {
    
        $sql=("UPDATE laporan set status='2' where idlaporan='$idlaporan'");    
        if (!mysql_query($sql,$con)) {
           die('Error: ' . mysql_error());
        }
        echo "1 record added<br/>";    
    }
    
于 2013-01-01T09:01:09.567 に答える