Home » Php » php – How to write an array to a database

php – How to write an array to a database

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need to write a program to increment a counter in phpMyAdmin every time a checkbox is submitted for a register type module. This is the code I currently have:

echo "<tr>
  <td>".$row['bookingID']."</td>
  <td>".$row['firstName']."</td>
  <td>".$row['surname']."</td>
  <td><input type='checkbox' name='present[]' value='".$row['bookingID']."'</td>
  <td><input type='checkbox' name='incident[]' value='".$row['bookingID']."'</td>
</tr>";

Any help would be great – thanks
Below is the code used for when a user signs up to the system

if (isset($_POST['signup-submit'])) {

  require 'dbh.inc.php';

  $username = $_POST['uid'];
  $email = $_POST['mail'];
  $firstname = $_POST['fname'];
  $surname = $_POST['sname'];
  $password = $_POST['pwd'];
  $passwordVerif = $_POST['pwd-verify'];

$sql = "SELECT username FROM users WHERE username=? OR email=?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
      header("Location: ../signup.php?error=sqlerror");
      exit();
    }
    else {
      mysqli_stmt_bind_param($stmt, "ss", $username, $email);
      mysqli_stmt_execute($stmt);
      mysqli_stmt_store_result($stmt);
      $resultCheck = mysqli_stmt_num_rows($stmt);
      if ($resultCheck > 0) {
        header("Location: ../signup.php?error=usernameoremailtaken");
        exit();
      }
      else {

        $sql = "INSERT INTO users (username, email, firstName, surname, pswd) VALUES (?, ?, ?, ?, ?)";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
          header("Location: ../signup.php?error=sqlerror");
          exit();
        }
        else {
          $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
          mysqli_stmt_bind_param($stmt, "sssss", $username, $email, $firstname, $surname, $hashedPwd);
          mysqli_stmt_execute($stmt);
          header("Location: ../login.php?signup=success");
          exit();
        }
      }
    }
  }

Edit: I’ve got it so that it’s outputting the number of checkboxes that were selected but this is only as an integer value, where as I need it as a value to output the number of the bookingID.

$present = $_POST["present"];
$incident = $_POST["incident"];
echo count($present);
echo count($incident);
How to&Answers: