Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php form submit - Q2

Tags:

forms

php

i am sorry for the dummy question. Here is my simple PHP form with two SQL tables and with the ADD submit button I would like to move people from Test1 to Test2. Many thing are fine:( only the submit button does't work therefore no feedback from Test2 table.

Revised: Submit now works great

Q2 - still don't get the checkboxs to work:( - please

Could somebody show me how to track back such an error like this please?

<?php include("db_connect.php");?>
<html>
  <head></head>
  <body>
    <form method="post" action="moving.php">
      <table border="1">
        <tr>
          <td>
            <?php
              $left='SELECT * FROM test1 ORDER BY name ASC';
              $result1=mysql_query($left);
              $count=mysql_num_rows($result1);
              while($resulta = mysql_fetch_array($result1)) {
                  ?>
                  <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $resulta['id']; ?>"/> <? echo $resulta['name']; ?>
                  <br />
            <?php } ?>
          </td>
          <td><input type="submit" id="add" name="add" value="Add" /></td>
          <td>
            <?php
              $rigth='SELECT * FROM test2,test1 WHERE test2.collect=test1.id ORDER BY test1.name ASC';
              $result2=mysql_query($right);
              while($resultb = mysql_fetch_array($result2)) {
                  echo $resultb['id'] ;
                  echo "<br />";
              }
            ?>
          </td>
        </tr>
      </table>
      <?php
        // Check if add button active, start this
        if (isset($_POST['add'])) {
            for ($i=0;$i<$count;$i++) {
                $add_id = $checkbox[$i];
                if ($add_id=='1') {
                  $sql = "INSERT INTO test2 (status, collect) VALUES(1, 1)";
                  $result = mysql_query($sql);
                }
            }

            // if successful redirect to delete_multiple.php
            if ($result){
                echo "<meta http-equiv=\"refresh\" content=\"0;URL=moving.php\">";
            }
        }
        mysql_close();
      ?>
    </form>
  </body>
</html>

thanks:) from a beginner

like image 618
TryHarder Avatar asked Aug 22 '26 15:08

TryHarder


1 Answers

where does $count come from?

Try using count($_POST['checkbox']) instead on your INSERT statement. Then you can iterate over the checkboxes using:

for ($c = 0; $c < count($_POST['checkbox']); $c++){
  $checkbox = $_POST['checkbox'][$c];
  ...INSERT action...
}
like image 162
Brad Christie Avatar answered Aug 24 '26 05:08

Brad Christie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!