我需要将数据从一个网站(基于HTML5)发布到PHPMyAdmin上的一个数据库中。我已经将 * 数据库创建为sewcute
* 和 * 一个名为members
的表 *。如果有必要,我将使用XAMMP。
在这段代码中,我想将注册表单中的数据发布到member
数据库。
格式:
<form id="maintext" action="scdb.php" method="post">
<fieldset id="specialRequest">
<legend>Registration Information</legend>
<label>
<input id="nameinput" name="UserName" placeholder="User Name" type="text">Create User Name
</label>
<label>
<input id="password" name="password" placeholder="Password" type="text">Password
</label>
<label for="nameinput">
<input id="nameinput" name="FirstName" placeholder="First Name" type="text">First Name
</label>
<label for="nameinput">
<input id="nameinput" name="LastName" placeholder="Last Name" type="text">Last Name
</label><label>
<input id="addrinput" name="address" placeholder="Your Address" type="text">Street Address
</label> <label>
<input id="zipinput" name="zip" placeholder="12345" type="text">Zip Code
</label>
<label>
<input id="emailinput" name="email" placeholder="addr@example.com" type="email"> Email
</label>
<label>
<input id="phoneinput" name="phone" placeholder="(123)456-7890" type="text">Phone
</label>
<label><input id="submit" type="submit" value="Submit" /></label>
</fieldset>
</form>
PHP:
<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "sewcute";
$dsn = ("mysql:host=$localhost;dbname=$sewcute");
TRY {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
$username1 = $_POST['username1'];
$password1 = $_POST['password1'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
if (isset($_POST['UserID'])) {
$id = $_POST['UserID'];
$sql = "UPDATE members SET"
. "UserName=".$conn->quote($username1)
. "password=".$conn->quote($password1)
. "FirstName=".$conn->quote($firstname)
. "LastName".$conn->quote($lastname)
. " WHERE UserID = ".$conn->quote($id);
$members = $conn->query($sql);
} else {
$sql = "INSERT INTO members("
. "UserName, password, FirstName, LastName"
. " ) VALUES ("
. $conn->quote($username1).","
. $conn->quote($password1).","
. $conn->quote($firstname).","
. $conn->quote($lastname).")";
$members = $conn->query($sql);
}
} elseif (isset($_GET['ID'])) {
$userEditDataRows = $conn->query('SELECT * FROM members WHERE UserID ='.$conn- >quote($_GET['UserID']));
if (sizeof($userEditDataRows)>0) {
$row = $userEditDataRows[0];
$username1 = $row['username1'];
$password1 = $row['password'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$id = $_GET['UserID'];
}
}
$table .= '</table>';
} catch (PDOException $e) {
exit("Connection failed: " . $e->getMessage());
}
?>
下面是表结构的图片:
我在XAMPP上的一个文件夹中有所有的网站文件。当我从表单提交数据时,我看到一个空白屏幕,什么也没有发生。我转到数据库,但什么也没有更新。有人能给我指出正确的方向吗?顺便说一句,这不是一个生产网站,它只是用于课堂。
1条答案
按热度按时间zazmityj1#
您似乎正在使用未声明的DSN数据库名称变量:
您可能想在这里使用
$database
,或者只使用字符串“sewcute”。