【问题标题】:Post Form Data To phpmyadmin Database将表单数据发布到 phpmyadmin 数据库
【发布时间】:2014-06-05 02:06:29
【问题描述】:

我需要将数据从网站(基于 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 上的一个文件夹中。当我从表单提交数据时,我得到一个空白屏幕,没有任何反应。我去数据库,但没有任何更新。谁能指出我正确的方向?顺便说一句,这不是一个生产网站,它只是为了上课。

【问题讨论】:

  • PhpMyAdmin 是一款用于管理 MySql 数据库的软件。您的数据库是 MySql,而不是 PhpMyAdmin。您可能认为我在迂腐,但作为开发人员,在寻求帮助时,了解差异并准确表达自己对您来说很重要。
  • “到 phpmyadmin 上的数据库(MySql)(如托管)”。感谢您的关注,我们将非常感谢您回答我的实际问题,而不是在语义上被误导的咆哮。
  • 除非它根本没有托管在 phpmyadmin 上。 Phpmyadmin 只是查看 mysql 数据库的一种方式,它不能“托管”任何东西。我不是在咆哮(反正我不这么认为),我试图帮助你说出你的问题,以便有更多的机会得到答案。对不起,如果我遇到错误的方式。
  • this page 的一半左右,在常见问题解答部分您将看到有关如何打开错误报告的指南(“不起作用”部分),这可能会帮助您查看一些错误消息。然后,您可以将该信息编辑到您的问题中,使其更易于回答。

标签: php mysql database html phpmyadmin


【解决方案1】:

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

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

大概,你的意思是在那里使用$database,或者只是字符串“sewcute”。

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2012-12-02
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多