简体   繁体   中英

How Can I Insert Data Into Database From A Form Properly?

so I've been working on a form using an MVC approach that will insert data into a database using PDO. I have the addWine form, addWine controller, data access model and wine class model. On submission of the form nothing happens and the database isn't populated. Can someone pinpoint what I have done wrong here?

Wine Form

<form action= "" method="POST">
    <table>
     <b>Fill the form to add new wine</b><p><p>
    <tr>
    <td> Wine ID:     </td>
    <td><input type="text" name= "wineID"/></td>
    </tr>

    <tr>
    <td>Wine Country ID: </td>
    <td><input type="text" name="wineCountryID"/></td>
    </tr>

    <tr>
    <td>Wine Size ID: </td>
    <td><input type="text" name="wineSizeID"/></td>
    </tr>

    <tr>
    <td>Wine Rating ID: </td>
    <td><input type="text" name="wineRatingID"/></td>
    </tr>

    <tr>
    <td>Wine Colour ID: </td>
    <td><input type="text" name="wineColourID"/></td>
    </tr>

    <tr>
    <td>Wine Package ID: </td>
    <td><input type="text" name="winePackageID"/></td>
    </tr>

    <tr>
    <td>Wine Category ID: </td>
    <td><input type="text" name="wineCategoryID"/></td>
    </tr>

    <tr>
    <td>Wine Code: </td>
    <td><input type="text" name="wineCode"/></td>
    </tr>

    <tr>
    <td>Wine Price: </td>
    <td><input type="text" name="wineCountryID"/></td>
    </tr>

    <tr>
    <td>Wine Description: </td>
    <td><input type="text" name="wineDescription"/></td>
    </tr>

    <tr>
    <td>Wine Rating: </td>
    <td><input type="text" name="wineRating"/></td>
    </tr>

    <tr>
    <td>Wine Image: </td>
    <td><input type="text" name="wineImage"/></td>
    </tr>

  </table><p><br>

  <center><input type="submit" name= "addWineButton" value="Add Wine"></center><p><p><p><p>
  </form>

Add Wine Controller

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
require_once('../Model/dataAccess.php');
require_once('../Model/wine.class.php');

$status = false;

if(isset($_POST["addWineButton"])){

$wineID = $_POST["wineID"];
$wineCountryID = $_POST["wineCountryID"];
$wineSizeID = $_POST["wineSizeID"];
$wineRatingID = $_POST["wineRatingID"];
$wineColourID = $_POST["wineColourID"];
$packageID = $_POST["packageID"];
$wineCategoryID = $_POST["wineCategoryID"];
$wineCode = $_POST["wineCode"];
$price = $_POST["price"];
$description = $_POST["description"];
$wineRating = $_POST["wineRating"];
$wineIMG = $_POST["wineIMG"];

$wineAdd = new Wine();
$wineAdd->wineID = htmlentities($wineID);
$wineAdd->wineCountryID = htmlentities($wineCountryID);
$wineAdd->wineSizeID = htmlentities($wineSizeID);
$wineAdd->wineRatingID = htmlentities($wineRatingID);
$wineAdd->wineColourID = htmlentities($wineColourID);
$wineAdd->packageID = htmlentities($packageID);
$wineAdd->wineCategoryID = htmlentities($wineCategoryID);
$wineAdd->wineCode = htmlentities($wineCode);
$wineAdd->price = htmlentities($price);
$wineAdd->description = htmlentities($description);
$wineAdd->wineRating = htmlentities($wineRating);
$wineAdd->wineIMG = htmlentities($wineIMG);

addWine($wineAdd);
$status = "$description has been updated.";
 }
?>

Wine Class Model

<?php

class Wine {


var $wineID;
var $wineSizeID;
var $wineCountryID;
var $wineRatingID;
var $wineColourID;
var $wineCode;
var $price;
var $description;
var $wineRating;
var $wineIMG;
var $packageID;
var $wineCategoryID;

function __get($name){
return $this->$name;
}
function __set($name,$value){
$this->$name=$value;
 }
}
?>

Data Access Model

function addWine($wineAdd){
global $pdo;
$statement = $pdo->prepare('INSERT INTO Wine
(wineID, wineCountryID, wineSizeID, wineRatingID, wineColourID, packageID,
wineCategoryID, wineCode, price, description, wineRating, wineIMG) VALUES  (?,?,?,?,?,?,?,?,?,?,?,?)');
$statement->execute([$wineAdd->wineID,
                     $wineAdd->wineCountryID,
                     $wineAdd->wineSizeID,
                     $wineAdd->wineRatingID,
                     $wineAdd->wineColourID,
                     $wineAdd->packageID,
                     $wineAdd->wineCategoryID,
                     $wineAdd->wineCode,
                     $wineAdd->price,
                     $wineAdd->description,
                     $wineAdd->wineRating,
                     $wineAdd->wineIMG]);
  $statement->fetch();
  }

The code is working now. I've updated the original post to reflect the changes. Thanks everyone for all the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM