Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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'; ?>
share|improve this question
I saw this question at least 5 times here in SO, did you Google it ? – alfasin Jan 1 at 8:37
yes, but i dont understand what i'm looking at, so i try to ask the direct question. maybe you can give me the links to those 5 question it will help me a lot :) – Kevin phytagoras Jan 1 at 8:41
1  
I got another idea, why won't you take the code that you don't understand, go over it line-by-line, and try to figure it out ? it will be better for you than someone handing a "ready made" code that works - you might not be able to maintain it... – alfasin Jan 1 at 8:46
BTW, if you have a specific question - a specific line of code that you don't understand how it works - I'll help you gladly! – alfasin Jan 1 at 8:46
You can start with thfollowing tip: don't use mysql_* functions, it's deprecated (see red box) and vulnerable to sql-injection. Use PDO or MySQLi. – alfasin Jan 1 at 8:47
show 1 more comment

marked as duplicate by alfasin, SztupY, Tomasz Wojtkowiak, Maerlyn, Praveen Kumar Jan 1 at 16:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 1 down vote accepted

Please try the following approach:

  1. Change the checkbox 'name' as 'idlaporan[]' (<input type="checkbox" name="idlaporan[]" )
  2. After form submit, selected check box values will be present in the Server side array $_POST['idlaporan']
  3. Use a foreach loop to update values in database.

    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/>";    
    }
    
share|improve this answer
thanks a lot !! it worked !! Thank you very much – Kevin phytagoras Jan 1 at 9:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.