TryHarder
TryHarder

Reputation: 750

php form submit - Q2

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

Upvotes: 3

Views: 200

Answers (3)

TryHarder
TryHarder

Reputation: 750

the solution - not nice but it works - thanks for all the comments and help!!!

<?php include("db_connect.php");?>

<html>
<head>
</head>
<body>
<form method="post" action="test.php">

New:

<?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_add[]" type="checkbox" id="checkbox_add[]" value="<? echo $resulta['id']; ?>"/> <? echo $resulta['name']; ?>
<br />
<?php
   }
?>

</td> <td><input type="submit" id="add" name="add" value="Add" /><br /><input type="submit" id="delete" name="delete" value="Del" /></td><td>
<?php
$right='SELECT test2.id, test1.name FROM test2, test1 WHERE test1.id=test2.collect AND test2.status=1';
$result2=mysql_query($right);
while($resultb = mysql_fetch_array($result2))
  {
?>
   <input name="checkbox_del[]" type="checkbox" id="checkbox_del[]" value="<?php echo $resultb['id']; ?>"/>, <?php echo $resultb['id']; ?>, <? echo $resultb['name']; ?>
<br />
<?php
  }
?>
</td></tr></table>


<?php
// Check if add button active, start this
if (isset($_POST['add'])) {
  for ($c = 0; $c < count($_POST['checkbox_add']); $c++){
    $checkbox_add = $_POST['checkbox_add'][$c];
    $sql = "INSERT INTO test2 (status, collect) VALUES(1, ".$checkbox_add.")";
    echo $sql;
    $result = mysql_query($sql);
    if($result){
      echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
      }
    }
  }
elseif (isset($_POST['delete'])) {
  for ($c = 0; $c < count($_POST['checkbox_del']); $c++){
    $checkbox_del = $_POST['checkbox_del'][$c];
    echo date("Y-m-d");
    $sql = "UPDATE  test2 SET status='2', log='".date('Y-m-d')."' Where id=".$checkbox_del;
    echo $sql;
    $result = mysql_query($sql);
    if($result){
      echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
     }
    }
  }
elseif (isset($_POST['new'])) {
    $sql = "INSERT INTO test1 (status, name) VALUES(1, '".$_POST['newitem']."')";
    echo $sql;
    $result = mysql_query($sql);
    if($result){
      echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
    }
  }
mysql_close();
?>

</form>

</body>
</html>

Upvotes: 0

outis
outis

Reputation: 77450

In the sample code, you store the statement in a variable named $rigth, but you (try to) execute a statement stored in a variable named $right.

There are a couple things you can do to catch errors.

  1. Try static code analysis; some tools can tell you if a variable is used only once (an indication it may be a typo).
  2. Handle errors. Some functions return a special value if there's an error (False is a common one); for these functions, there is usually a related function that will return error information. Some functions will throw an exception; catch them where they can be appropriately taken care of. System error messages shouldn't be displayed to non-admin users so you don't disclose too much information.
  3. Use an interactive debugger. For example, install the Xdebug extension on your development server (you do use a dev server, right?) and use an Xdebug compatible debugger.

Upvotes: 2

Brad Christie
Brad Christie

Reputation: 101614

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...
}

Upvotes: 3

Related Questions