1

Below is a simplified table, but in reality there are quite a lot of rows. On the front-end there are some select boxes where each one echo out all the headlines as a possible option. If I select the option of 'swap me' using the checkbox name of 'one_six' to 'swap to this' then submit, the value of main_nav for that row would change and the 'swap me' would reset to 0.

There are a few select boxes and when I hit submit not only should the main_nav update on the correct row to the correct number, any row in the table that hasn't been selected as an option should be set to 0.

At the moment it won't submit for some reason and update. I get no errors. I have no idea how to reset all the existing rows if not selected and submitted by any select box. Any ideas? I have included my code below.

+----+--------------------------+----------+
| id | headline                 | main_nav |
+----+--------------------------+----------+
| 1  | a cool headline          | 17       |
+----+--------------------------+----------+
| 2  | just another hadline     | 15       |
+----+--------------------------+----------+
| 3  | some other fun thing     | 11       |
+----+--------------------------+----------+
| 4  | swap me                  | 16
+----+--------------------------+----------+
| 5  | here is another headline | 12       |
+----+--------------------------+----------+
| 6  | headline 6               | 14       |
+----+--------------------------+----------+
| 7  | headline 7               | 13       |
+----+--------------------------+----------+
| 8  | swap to this             | 0        |

<?php
    require_once("../db/db.php");
    if (isset($_POST['submit'])) {      
        $sql = $conn->prepare("UPDATE pages SET main_nav=? WHERE id=?");
        $one_one = $_POST['one_one'];
        $one_two =$_POST['one_two'];
        $one_three = $_POST['one_three'];
        $one_four = $_POST['one_four'];
        $one_five= $_POST['one_five'];
        $one_six = $_POST['one_six'];
        $one_seven = $_POST['one_seven'];
$sql->bind_param("iiiiiii", $one_one, $one_two, $one_three, $one_four, $one_five, $one_six, $one_seven);    
        if($sql->execute()) {
            $success_message = "Edited Successfully";
        } else {
            $error_message = "Problem in Editing Record";
        }
    }
$sql = "SELECT * FROM pages";
$result = $conn->query($sql);
$conn->close();     
?>
PeterT
  • 39
  • 5
  • Your update query contains _one_ placeholder, but you are trying to bind _seven_ parameters to it. Enable proper PHP error reporting if you have not done that already, and don’t output an error message only saying “problem”, but go _ask_ the database what went wrong. (At least while you are developing, later on in production that is a different story of course.) – misorude Oct 19 '18 at 11:58
  • You also have no `WHERE` clause, so the code as written should be updating _all_ rows in `pages` and setting `main_nav` to the value in `$one_one`, which probably isn't what you want. – dossy Oct 19 '18 at 13:51
  • I think you need same amount place holders as the amount of your parameters. – S4NDM4N Oct 19 '18 at 16:41
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Jan 15 '20 at 21:20

0 Answers0