samedi 6 juin 2020

PHP While loop with checkbox - Restaurant categories not inserting into DB

I'm building a restaurant DB and i'm running into a problem with category table and restaurant table.

I can display my categories, but i cant insert them properly. I'm using $_GET this is a administration for inserting new data inside the database. Here is my code

Tables HERE_________________

Table category

  CREATE TABLE IF NOT EXISTS `mjens19_db`.`category` (
 `category_id` INT(10) NOT NULL AUTO_INCREMENT,
 `name` VARCHAR(100) NOT NULL,
 `description` MEDIUMTEXT NULL,
 `image` VARCHAR(255) NULL,
 PRIMARY KEY (`category_id`)
 );

Table restaurant

 CREATE TABLE IF NOT EXISTS `mjens19_db`.`restaurant` (
 `restaurant_id` INT(10) NOT NULL AUTO_INCREMENT,
 `name` VARCHAR(150) NOT NULL,
 `description` MEDIUMTEXT NOT NULL,
 `image` VARCHAR(255) NULL,
 `URL` VARCHAR(255) NULL,
 `email` VARCHAR(100) NULL,
 `openingHours` VARCHAR(255) NULL,
 `CVR` INT(45) NULL,
 `telephone` INT(45) NULL,
 `place_id` INT(10) NOT NULL,
 `category_id` INT(10) NOT NULL,
 PRIMARY KEY (`restaurant_id`),
   FOREIGN KEY (`place_id`) REFERENCES place(`place_id`),
   FOREIGN KEY (`category_id`) REFERENCES category(`category_id`)
 );

 //Php Sent data to database.
<?php
  // tjek, om formular er afsendt
  if (isset($_GET['submit'])) {

    // gem medsendte data i php-variable
    $name = mysqli_real_escape_string($con, $_GET['name']);
    $description = mysqli_real_escape_string($con, $_GET['description']);
    $image = mysqli_real_escape_string($con, $_GET['image']);
    $URL = mysqli_real_escape_string($con, $_GET['URL']);
    $email = mysqli_real_escape_string($con, $_GET['email']);
    $openingHours = mysqli_real_escape_string($con, $_GET['openingHours']);
    $CVR = mysqli_real_escape_string($con, $_GET['CVR']);
    $telephone = mysqli_real_escape_string($con, $_GET['telephone']);
    $place_id = mysqli_real_escape_string($con, $_GET['place_id']);
    $category_id = mysqli_real_escape_string($con, $_GET['category_id']);

    // opbyg forespørgsel
    $query = "
      INSERT INTO restaurant
        (name, description, image, URL, email, openingHours, CVR, telephone, place_id, category_id)
        VALUES
        ('$name', '$description', '$image', '$URL', '$email', '$openingHours', '$CVR', '$telephone', '$place_id', '$category_id')
      ";
     echo $query;
    // send forespørgsel
    $result = mysqli_query($con, $query);
}
?>

Here is the code with checkbox that i cant get to work:

          $query = "
            SELECT *
            FROM category
            ORDER BY name
          ";
          // send forespørgsel
          $result = mysqli_query($con, $query);
          $category = array();
      // gennemløb resultater og generér liste
          while ($row = mysqli_fetch_assoc($result)) 
          {
            $category = explode(",", $row['category_id']);
            ?>

            <input type="checkbox" 
            id="<?php echo $row['category_id']; ?>"
            name="menucategori[]" 
            value="<?php echo $row['name']; ?>"
            <?php echo (in_array($row['category_id'],$category))?>> <?php echo $row['name']; ?>
            </input>
            <?php
          } // end while
        ?>

I hope you can see what im missing ? :S




Aucun commentaire:

Enregistrer un commentaire