3

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

9
  • coming back to the same page.if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=moving.php\">"; } I believe:) Commented Aug 28, 2011 at 0:30
  • 1
    you should have "moving.php" as your "action" in your tag form. the meta refresh will refresh the page. Commented Aug 28, 2011 at 0:31
  • 1
    Off topic: don't use SELECT *; select only the columns you need. The mysql extension is outdated and on its way to deprecation. Use mysqli or PDO (both of which support prepared statements) instead. In almost all cases, <br/> isn't semantic. Tables shouldn't be used for layout. In your code, a list is semantically appropriate. Commented Aug 28, 2011 at 0:39
  • thanks for the advice. Could I ask you to show me the right way instead of the <table>. I can do the mysqli:) thanks again Commented Aug 28, 2011 at 0:50
  • 1
    @Andras: tables are only for data that has a two dimensional structure, rather than for data that has a 2D structure imposed on it for display. HTML is a structural language, not a formatting one. CSS is for formatting. On another topic, code should be indented for readability. Indenting can be tricky when mixing languages (such as PHP and HTML); consistency is the most important thing when it comes to these conventions. Commented Aug 28, 2011 at 1:03

3 Answers 3

3

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...
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks very much!!!!! I am getting blind on this code... head and fingers not on the same speed!!!! thanks again:)
2

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.

2 Comments

thanks @outis really helpful comment unfortunately hosted web so I've got my laptop. Any other tool than Xdebug? thanks
Your dev server should be completely under your control and publically inaccessible; your laptop is the perfect candidate. You can install an AMP package (such as XAMPP or WampServer) on your laptop, making it your dev server. Development requires configuring the server in a way that should never be done on a public server for reasons of efficiency and security.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.