如何让我的html / php表单正确地将数据插入我的数据库

时间:2015-12-08 01:47:52

标签: php html mysql database

对于我的数据库项目的最后一部分,我们将通过修改PHP文件来创建工作的html表单。如果您查看html表单链接,然后查看我的错误屏幕截图链接,您将看到"提取/下降经度/纬度"在与其他值不同的表中。 (地点和预订)

这个表单在提交时,应该将这些数据上传到我创建的在线虚拟数据库,但我不知道我应该怎么做,因为它们在不同的表中。在php文件中执行此操作的唯一方法是使用多个" insert"声明?

HTML Form

ERD

如果需要,以下是我的创建表:

CREATE TABLE Customer (
CustomerID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
Phone VARCHAR(45) NULL,
CustomerAddress VARCHAR(45) NOT NULL,
PRIMARY KEY (CustomerID));

CREATE TABLE Location (
Address VARCHAR(100) NOT NULL,
Latitude VARCHAR(45) NULL DEFAULT ' ',
Longitude VARCHAR(45) NULL,
PRIMARY KEY (Address));


CREATE TABLE Employee (
EmployeeID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
PRIMARY KEY (EmployeeID));


CREATE TABLE Truck (
LicensePlate CHAR(20) NOT NULL,
color VARCHAR(45) NULL,
capacity VARCHAR(45) NULL,
PRIMARY KEY (LicensePlate));

CREATE TABLE Shifts (
ShiftTime DATETIME NOT NULL,
PRIMARY KEY (ShiftTime));

CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeWorking INT NULL,
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeWorking),
FOREIGN KEY (EmployeeWorking) REFERENCES Employee(EmployeeID),
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime));

CREATE TABLE Reservation (
ReservNum INT NOT NULL,
ReserveDate DATE NULL,
PickupTime VARCHAR(45) NOT NULL,
NumOfPassengers INT NULL,
sheduledTime VARCHAR(45) NULL,
ActualPickupTime VARCHAR(45),
ActualTime VARCHAR(45),
PricePaid VARCHAR(45),
DriverHourlyRate DECIMAL(7,2) NOT NULL,
PassEmployeeHourlyRate DECIMAL (7,2) NOT NULL,
DriverSalary VARCHAR(10),
PassEmployeeSalary VARCHAR(10),
Customer_CustomerID INT,
Truck_LicensePlate char(20) NOT NULL,
Employee_EmployeeID_Driver INT,
Location_Address_Pickup VARCHAR(100),
Employee_EmployeeID_Passenger INT,
Location_Address_Drop VARCHAR(100),
PRIMARY KEY (ReservNum),
FOREIGN KEY (Customer_CustomerID) REFERENCES Customer (CustomerID),
FOREIGN KEY (Truck_LicensePlate) REFERENCES Truck (LicensePlate),
FOREIGN KEY (Employee_EmployeeID_Driver) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Pickup) REFERENCES Location (Address),
FOREIGN KEY (Employee_EmployeeID_Passenger) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Drop) REFERENCES Location (Address));

如果你的意思是php的代码,这里是我们给予修改的一个例子。我还没有开始这种特定的形式。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>InsertNewCustomer PHP Page</title>
    <style type="text/css">
        h1 {text-align: center; color: blue}
        h2 {font-family: Ariel, sans-serif; text-align: left; color: blue}
        p.footer {text-align: center}
        table.output {font-family: Ariel, sans-serif}
    </style>
</head>
<body>
<?php

    // Connect to the Database
    try {
        $pdo = new PDO("mysql:host=localhost;dbname=Goober", 
            'root', 'password');
        //  $pdo->setAttribute(PDO::ATTR_ERRMODE,   
        //          PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
            echo 'PDO Connection failed: ' . $e->getMessage();  
    }

    // Create short variable names
    $Driver = $_POST["Driver"];
    $DateOfShift = $_POST["DateOfShift"];
    $DesiredShift = $_POST["DesiredShift"];


    // Create SQL statement to INSERT new data
    $SQLINSERT = "INSERT INTO EmployeeShifts (Driver,DateOfShift,DesiredShift)";
    $SQLINSERT .= "VALUES('$Driver','$DateOfShift', '$DesiredShift')";

    // Prepare the statement to execute
    $sqlprep = $pdo->prepare($SQLINSERT);


    // Test existence of result
    echo "<h1>
            Goober EmployeeShifts Table
        </h1>
        <hr />";
    // Test existence of result
        if ($sqlprep->execute()){

        echo "<h2>
            New Shift Added:
        </h2>
        <table>
            <tr>";
            echo "<td>Driver:</td>";
            echo "<td>" . $Driver . "</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td>DateOfShift:</td>";
            echo "<td>" . $DateOfShift . "</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td>DesiredShift:</td>";
            echo "<td>" . $DesiredShift . "</td>";
            echo "</tr>";
        echo "</table><br /><hr />";
        }
        else {
            exit ("SQL Statement Error: " . $SQL);
        }

    // Create SQL statement to read CUSTOMER table data
    $SQL = "SELECT * FROM EmployeeShifts";

// Prepare the statement to execute
$sqlprep = $pdo->prepare($SQL);
// Run statement and test existence of recordset
    if (!$sqlprep->execute())
   {
        exit ("SQL Statement Error: ");
   }


?>
    <!--  Page Headers -->
    <h1>
        The Heather Sweeney Designs CUSTOMER Table
    </h1>
    <hr />
    <h2>
        CUSTOMER
    </h2>
<?php     
    // Table headers
    echo "<table class='output' border='1'>
            <tr>
                <th>Driver</th>
                <th>DateOfShift</th>
                <th>DesiredShift</th>
            </tr>";

// Table data
while($RecordSetRow = $sqlprep->fetch())
        {
        echo "<tr>";
        echo "<td>" . $RecordSetRow['Driver'] . "</td>";
        echo "<td>" . $RecordSetRow['DateOfShift'] . "</td>";
        echo "<td>" . $RecordSetRow['DesiredShift'] . "</td>";

        }
    echo "</table>";

?>
    <br />
    <hr />
    <p class="footer">  
        <a href="../HSD/index.html">
            Return to Home Page
        </a>
    </p>
    <hr />
</body>

1 个答案:

答案 0 :(得分:0)

不知道你在哪里

$SQLINSERT = "INSERT INTO EmployeeShifts (Driver,DateOfShift,DesiredShift)";
    $SQLINSERT .= "VALUES('$Driver','$DateOfShift', '$DesiredShift')";

但你可以试试吗

$stmt = $pdo->prepare("INSERT INTO EmployeeShifts (Driver,DateOfShift,DesiredShift) VALUES(:Driver,:DateOfShift,:DesiredShift)";
$stmt->bindParam(':Driver', $Driver);
$stmt->bindParam(':DateOfShift', $DateOfShift);
$stmt->bindParam(':DesiredShift', $DesiredShift);

$stmt->execute();

或者只是删除抽动&#39;在您现有的查询中

 $SQLINSERT = "INSERT INTO EmployeeShifts (Driver,DateOfShift,DesiredShift)";
        $SQLINSERT .= "VALUES($Driver,$DateOfShift, $DesiredShift)";