HTML 将数据提交到一个使用 PHP 的谷歌应用程序引擎 MySQL 数据库

发布时间: 2016/10/14 20:29:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我一直在试图创建简单的 web 窗体,将用来将数据提交到 Google App Engine 数据库使用 MySQL。数据库很好的连接和 try catch 语句似乎工作正常。问题是当我单击提交按钮时,数据不被提交到数据库。任何帮助将是很多赞赏,我敢肯定有是我忽略了什么只是一些小错误。

HTML 表单

                    <form class="form-horizontal" role="form" action="connection.php" method="post">
                    <div class="form-group">
                         <label for="patientName" class="col-sm-2 control-label">Patient Name</label>
                        <div class="col-sm-10">
                            <input type="Name" class="form-control" id="patientName" name="patientName">
                        </div>
                    </div>
                    <div class="form-group">
                         <label for="Address1" class="col-sm-2 control-label">Address 1</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" rows='3' id="Address1" name="Address1"></input>
                        </div>
                    </div>
                    <div class="form-group">
                         <label for="Address2" class="col-sm-2 control-label">Address 2</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" rows='3' id="Address2" name="Address2"></input>
                        </div>
                    </div>
                    <div class="form-group">
                         <label for="Address3" class="col-sm-2 control-label">Address 3</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" rows='3' id="Address3" name="Address3"></input>
                        </div>
                    </div>
                    <div class="form-group">
                         <label for="postCode" class="col-sm-2 control-label">PostCode</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="postCode" name="postCode">
                        </div>
                    </div>
                    <div class="form-group">
                         <label for="symptoms" class="col-sm-2 control-label">Symptoms</label>
                        <div class="col-sm-10">
                            <textarea type="text" class="form-control" rows='5' id="symptoms" name="symptoms"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                         <label for="contactNumber" class="col-sm-2 control-label">Contact Number</label>
                        <div class="col-sm-10">
                            <input type="Tel" class="form-control" id="contactNumber" name="contactNumber">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                             <button type="submit" class="btn btn-default">Log Call Details</button>
                        </div>
                    </div>
                </form>

PHP 连接文件

<?php

use google\appengine\api\users\User;
use google\appengine\api\users\UserService;

try {
$db = new pdo('mysql:host=111.111.111.11:3306;dbname=MyDB',
'root',
'password'
);

$patientName = $_POST["patientName"];
$address1 = $_POST["Address1"];
$address2 = $_POST["Address2"];
$address3 = $_POST["Address3"];
$postCode = $_POST["postCode"];
$symptoms = $_POST["symptoms"];
$contactNumber = $_POST["contactNumber"];

$sql = "INSERT INTO patient (patientName, patientAddress1, patientAddress2, patientAddress3, patientPostcode, PatientSymptoms, patientPhoneNumber)
VALUES ($patientName, $address1, $address2, $address3, $postCode, $symptoms, $contactNumber)";


$db->execute($sql);


} catch (PDOException $ex) {
echo "Could not connect to the database.";
exit;
}

$db = null;
echo "Woo-hoo!";
?>

我大概应该还提到 HTML 页面使用引导。

解决方法 1:

你可以试试下面这个代码片断,它的工作,我没有不使用的 $_POST 数据硬编码的值,但希望它可以帮助 !

更多信息︰ http://php.net/manual/en/pdo.prepare.php

$username="test12";
$role="test1";
$sql = "INSERT INTO test (username, role) VALUES (:username, :role)";
$conn = new pdo('mysql:host=127.0.0.1:3306;dbname=home',
'root',
'password'
);
$q = $conn->prepare($sql);
$q->execute(array(':username'=>$username, ':role'=>$role));
赞助商