如何正确地从表单中将数据插入数据库?

时间:2017-03-03 18:50:19

标签: php forms pdo

所以我一直在使用MVC方法处理表单,该方法将使用PDO将数据插入数据库。我有addWine表单,addWine控制器,数据访问模型和wine类模型。提交表单时没有任何操作,并且未填充数据库。谁能找到我在这里做错了什么?

葡萄酒表格

<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>

添加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.";
 }
?>

葡萄酒类模型

<?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;
 }
}
?>

数据访问模型

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();
  }

1 个答案:

答案 0 :(得分:0)

代码现在正在运行。我已更新原始帖子以反映更改。感谢大家的帮助。