0

I have look all over youtube and forms and I still can't find an answer that works. I want use a php loop to update database values. I need something like the two last loops below. Thanks in advance.

<?php

    if (isset($_POST['update'])) {

        for ($i = 1; $i <= 6; $i++) {
             $heading[$i] = $_POST['heading' . $i];
        }

        for ($i = 1; $i <= 9; $i++) {
             $des[$i] = $_POST['des' . $i];
        }

        for ($i = 1; $i <= 6; $i++) {
             $update = mysqli_query($connect, "UPDATE info1 SET heading[$i]='$heading[$i]' WHERE id= $i");
        }

        for ($i = 1; $i <= 9; $i++) {
            $update = mysqli_query($connect, "UPDATE info1 SET des[$i]='$des[$i]' WHERE id= $i");
        }

        header('Location: admin.php?success');
        exit();
    }
?>
4
  • 2
    stop assuming your queries succeeded. if (!$update) { die(mysqli_error($connect)); } so you can get told about your sql syntax errors. Commented Jan 7, 2016 at 21:31
  • I recommend doing more research on how to build modern applications with PHP. webdeveloper.gdemolished.com/… Commented Jan 7, 2016 at 21:32
  • Unless you have a column named des[0], - des[100], these queries are going to fail Commented Jan 7, 2016 at 21:33
  • Also, consider using name=heading[] and name=des[] in your form, so that the $_POST variables will be arrays. Then you don't need the first two loops. Commented Jan 7, 2016 at 21:43

1 Answer 1

1

Remove the first [i] from your queries... try these

    for ($i = 1; $i <= 6; $i++) {
         $update = mysqli_query($connect, "UPDATE info1 SET heading='$heading[$i]' WHERE id= $i");
    }

    for ($i = 1; $i <= 9; $i++) {
        $update = mysqli_query($connect, "UPDATE info1 SET des='$des[$i]' WHERE id= $i");
  }
Sign up to request clarification or add additional context in comments.

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.