将表单数据发布到phpmyadmin数据库

时间:2014-04-21 02:54:53

标签: php mysql database html5 phpmyadmin

我需要将网站上的数据(基于HTML5)发布到phpmyadmin上的数据库。我将数据库创建为sewcute和一个名为members的表。如果重要,我会使用XAMMP。

在此代码中,我想将数据从注册表单发布到成员数据库。

表格:

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

这是表格结构的图片

Database Structure

我将所有网站文件放在XAMPP上的文件夹中。当我从表单提交数据时,我得到一个空白屏幕,没有任何反应。我去了数据库,但没有更新。谁能指出我正确的方向?顺便说一句,这不是一个生产网站,它只是为了上课。

1 个答案:

答案 0 :(得分:0)

您似乎在使用未声明的DSN数据库名称的变量:

$database = "sewcute";
$dsn = ("mysql:host=$localhost;dbname=$sewcute");

据推测,你打算在那里使用$ database,或者只使用字符串“sewcute”。