Jump to content

Why i need to click twice to removing record by using checkbox(php and mysql)


Rigodon
Go to solution Solved by mac_gyver,

Recommended Posts

Hi,

Could someone help me with repair my code. I do not know why i need to click twice to change my data in database. Propably something with condition but i am not sure.

  <?php
                  // validator bledow
                    error_reporting(E_ALL);
                    ini_set('display_errors','1');
            // $p=$_POST['pole'];
            // echo $p.'tal';

        require("connection.php");
        // ładujemy plik z połączeniem
        connection();


              // DISPLAYING THE DATABASE
        $wynik = mysql_query("SELECT * FROM test")
        or die('error query');
          if(mysql_num_rows($wynik) > 0) {
        /* if the result is positive, the display data*/
        echo "<table cellpadding=\"2\" border=1>";
          while($r = mysql_fetch_object($wynik)) {
        echo "<tr>";
        echo "<td>".$r->name."</td>";
        echo "<td>".$r->description."</td>";
        echo "<td class='tdrzecz'>
         <a href=\"index.php?a=edit&id={$r->id}\">EDIT</a>
         <form action='index.php' method='post'>
          <input type='checkbox' value='wartosc' name='checkbox[{$r->id}]'/>
            </td>";
        echo "</tr>";
        }
        '
         
           </form>';
                 }
        echo "</table>";
    echo 'Album Name:<br />
        <input type="text" name="name"  value="" /><br />
        Description:<br />
        <input type="text" name="description" value=""  /><br />
        <select name="select">
  <option value="add">add</option>
  <option value="edit" selected>edit</option>
  <option value="remove">remove</option>
        </select>
 <input type="submit" nazwa="do" value="do" />';
 

       $dane_z_tab = ($_POST['checkbox']);
             // echo "dane_z_tab ".$dane_z_tab.'<br />'; // $dane_z_tab It is an array when you select one checkbox
             // echo gettype($dane_z_tab).'<br />';
            while ($val_checkbox = current($dane_z_tab)) {
                // echo "value_checkbox ".$val_checkbox; //value = "wartosc"
             if ($val_checkbox == 'wartosc') {
                $klucz = key($dane_z_tab).'<br />'; //pulling the key (id record)
                 echo "Klucz: ".$klucz;

             }
                 next($dane_z_tab);  }
       

        $selectOption = $_POST['select'];
         $name = $_POST['name'];
         $description = $_POST['description'];
                 echo "Klucz: ".$klucz;



        if ($selectOption == 'add' and $name and $description) {
          echo 'zaznaczyles dodawnaie';
           $ins = mysql_query("INSERT INTO test SET name='$name', description='$description'");
        }elseif ($selectOption == 'edit') {
          echo 'zaznaczyles edit';
          mysql_query("UPDATE test SET name='$name',description='$description' WHERE id='$klucz'");
        }else if($selectOption == 'remove' and isset($_POST['checkbox'])) {
            mysql_query("DELETE FROM test WHERE id='$klucz'");
          echo 'zaznaczyles remove';
         

            }
      

 

Link to comment
Share on other sites

For starters, you need to move the closing </form> below the submit button.  All the inputs besides the checkbox are not even being submitted with the form cause they are outside of the form.

Thanks for that answer.

 

This problem with that script is still unsolved. This script is placed on 'kontente.bl.ee'

Link to comment
Share on other sites

You need to wrap all the processing and query code from this line down in an if() to check if the form has been posted.

 $dane_z_tab = ($_POST['checkbox']);

Something like this

if(isset($_POST['do'])) // Checks the post var from the name of your submit button
{
$dane_z_tab = ($_POST['checkbox']);
             // echo "dane_z_tab ".$dane_z_tab.'<br />'; // $dane_z_tab It is an array when you select one checkbox
             // echo gettype($dane_z_tab).'<br />';
            while ($val_checkbox = current($dane_z_tab)) {
                // echo "value_checkbox ".$val_checkbox; //value = "wartosc"
             if ($val_checkbox == 'wartosc') {
                $klucz = key($dane_z_tab).'<br />'; //pulling the key (id record)
                 echo "Klucz: ".$klucz;

             }
                 next($dane_z_tab);  }
       

        $selectOption = $_POST['select'];
         $name = $_POST['name'];
         $description = $_POST['description'];
                 echo "Klucz: ".$klucz;



        if ($selectOption == 'add' and $name and $description) {
          echo 'zaznaczyles dodawnaie';
           $ins = mysql_query("INSERT INTO test SET name='$name', description='$description'");
        }elseif ($selectOption == 'edit') {
          echo 'zaznaczyles edit';
          mysql_query("UPDATE test SET name='$name',description='$description' WHERE id='$klucz'");
        }else if($selectOption == 'remove' and isset($_POST['checkbox'])) {
            mysql_query("DELETE FROM test WHERE id='$klucz'");
          echo 'zaznaczyles remove';
}
Link to comment
Share on other sites

I do this and actually does not work..

  <?php
                  // validator bledow
                    error_reporting(E_ALL);
                    ini_set('display_errors','1');
            // $p=$_POST['pole'];
            // echo $p.'tal';

        require("connection.php");
        // ładujemy plik z połączeniem
        connection();


              // DISPLAYING THE DATABASE
        $wynik = mysql_query("SELECT * FROM test")
        or die('error query');
          if(mysql_num_rows($wynik) > 0) {
        /* if the result is positive, the display data*/
        echo "<table cellpadding=\"2\" border=1>";
          while($r = mysql_fetch_object($wynik)) {
              echo "<tr>";
              echo "<td>".$r->name."</td>";
              echo "<td>".$r->description."</td>";
              echo "<td class='tdrzecz'>
         <form action='index.php' method='post'>
          <input type='checkbox' value='wartosc' name='checkbox[{$r->id}]'/>
            </td>";
        echo "</tr>";
        }
                 
        echo "</table>";
        echo 'Album Name:<br />
          <input type="text" name="name"  value="" /><br />
        Description:<br />
          <input type="text" name="description" value=""  /><br />

        <select name="select">
          <option value="add">add</option>
          <option value="edit" selected>edit</option>
          <option value="remove">remove</option>
        </select>

          <input type="submit" nazwa="submit" value="submit" />
         
          </form>';




       if(isset($_POST['submit'])) // Checks the post var from the name of your submit button
    {
      $dane_z_tab = ($_POST['checkbox']);
             // echo "dane_z_tab ".$dane_z_tab.'<br />'; // $dane_z_tab It is an array when you select one checkbox
             // echo gettype($dane_z_tab).'<br />';
            while ($val_checkbox = current($dane_z_tab)) {
                // echo "value_checkbox ".$val_checkbox; //value = "wartosc"
             if ($val_checkbox == 'wartosc') {
                $klucz = key($dane_z_tab).'<br />'; //pulling the key (id record)
                 echo "Klucz: ".$klucz;

             }
                 next($dane_z_tab);  }
       

         $selectOption = $_POST['select'];
         $name = $_POST['name'];
         $description = $_POST['description'];
                 echo "Klucz: ".$klucz;



        if ($selectOption == 'add' and $name and $description) {
          echo 'zaznaczyles dodawnaie';
           $ins = mysql_query("INSERT INTO test SET name='$name', description='$description'");
        }elseif ($selectOption == 'edit') {
          echo 'zaznaczyles edit';
          mysql_query("UPDATE test SET name='$name',description='$description' WHERE id='$klucz'");
        }else if($selectOption == 'remove' and isset($_POST['checkbox'])) {
            mysql_query("DELETE FROM test WHERE id='$klucz'");
          echo 'zaznaczyles remove';
}} else {
  echo "no :(";
}
}
Link to comment
Share on other sites

  • Solution

the reason it appears that your data isn't updated until after you have submitted the form twice, is because you are retrieving and displaying the data near the top of your code, with the form processing code at the end.

 

the form processing code should be near the top of your code, with the display code after it, so that any changes made to the data will be present when you retrieve the data to display it.

Link to comment
Share on other sites

 

You need to actually use html name attributes, not your native language.  Change this line

<input type="submit" nazwa="submit" value="submit" />

To this

<input type="submit" name="submit" value="submit" />

I missed it. thanks.

 

 

the reason it appears that your data isn't updated until after you have submitted the form twice, is because you are retrieving and displaying the data near the top of your code, with the form processing code at the end.

 

the form processing code should be near the top of your code, with the display code after it, so that any changes made to the data will be present when you retrieve the data to display it.

Och, you are totally right. Thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.