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

TryHarder
  • 750
  • 1
  • 9
  • 22
  • coming back to the same page.if($result){ echo ""; } I believe:) – TryHarder Aug 28 '11 at 00:30
  • 1
    you should have "moving.php" as your "action" in your tag form. the meta refresh will refresh the page. – Book Of Zeus Aug 28 '11 at 00:31
  • 1
    Off topic: don't use [`SELECT *`](http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-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, [
    ](http://brainstormsandraves.com/articles/semantics/structure/#br) isn't semantic. [Tables](http://davespicks.com/writing/essays/notables.html) shouldn't be used for layout. In your code, a [list](http://www.w3.org/TR/html401/struct/lists.html) is semantically appropriate.
    – outis Aug 28 '11 at 00:39
  • thanks for the advice. Could I ask you to show me the right way instead of the . I can do the mysqli:) thanks again
    – TryHarder Aug 28 '11 at 00:50
  • thanks BookOfZeus I changed it now:) – TryHarder Aug 28 '11 at 00:55
  • 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](http://en.wikipedia.org/wiki/Indent_style) 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. – outis Aug 28 '11 at 01:03
  • Also, be careful about editing questions. Don't invalidate answers. If you have a truly new issue, it deserves a new question. – outis Aug 28 '11 at 01:04
  • thanks again! I am happy to learn so I enjoy being educated! thanks again. (but I still don't see yet how you mean to use CSS instead of . Do you mean every data should have a spn or even a div to fit in?
    – TryHarder Aug 28 '11 at 01:08
  • @AndrasSebestyen let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2927/discussion-between-outis-and-andras-sebestyen) – outis Aug 28 '11 at 01:13

3 Answers3

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...
}
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • thanks very much!!!!! I am getting blind on this code... head and fingers not on the same speed!!!! thanks again:) – TryHarder Aug 28 '11 at 00:34
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.
Community
  • 1
  • 1
outis
  • 75,655
  • 22
  • 151
  • 221
  • thanks @outis really helpful comment unfortunately hosted web so I've got my laptop. Any other tool than Xdebug? thanks – TryHarder Aug 28 '11 at 01:01
  • 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. – outis Aug 28 '11 at 01:11
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>
TryHarder
  • 750
  • 1
  • 9
  • 22