Rigodon Posted July 19, 2015 Share Posted July 19, 2015 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/ Share on other sites More sharing options...
fastsol Posted July 19, 2015 Share Posted July 19, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516789 Share on other sites More sharing options...
Rigodon Posted July 19, 2015 Author Share Posted July 19, 2015 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' Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516791 Share on other sites More sharing options...
fastsol Posted July 19, 2015 Share Posted July 19, 2015 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516792 Share on other sites More sharing options...
Rigodon Posted July 19, 2015 Author Share Posted July 19, 2015 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 :("; } } Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516793 Share on other sites More sharing options...
fastsol Posted July 19, 2015 Share Posted July 19, 2015 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516794 Share on other sites More sharing options...
Solution mac_gyver Posted July 19, 2015 Solution Share Posted July 19, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516795 Share on other sites More sharing options...
Rigodon Posted July 19, 2015 Author Share Posted July 19, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/297367-why-i-need-to-click-twice-to-removing-record-by-using-checkboxphp-and-mysql/#findComment-1516797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.