繁体   English   中英

HTML表单,帖子没有调用PHP脚本

[英]HTML form with post not calling PHP script in action

我有一个HTML文件中的表单,它将通过PHP脚本将表单中的值发布到数据库中。

不幸的是,当我单击提交按钮时,我的表单将不会调用PHP脚本。 但是,如果我输入PHP文件的URL,PHP文件将运行并在数据库中插入一个空行。 因此,我的猜测是我的HTML文件从不调用PHP脚本。

由于我是PHP和HTML表单的新手,我的代码中是否有任何改进或错误?

这是我的HTML表单:

<form id="frmRegister" action="dbase/insert.php" method="post" >
<table>
    <tr>
        <th><br /></th>
        <td><br /></td>
    </tr>
    <tr>
        <th align="left">First Name:</th>
        <td><input type="text" name="FirstName" /></td>
    </tr>
    <tr>
        <th align="left">Last Name:</th>
        <td><input type="text" name="LastName" /></td>
    </tr>
    <tr>
        <th align="left">Email Address:</th>
        <td><input type="text" name="Email" /></td>
    </tr>
    <tr>
        <th align="left">Phone Number:</th>
        <td><input type="text" name="Phone" maxlength="10"/></td>
    </tr>
    <tr>
        <th align="left">Username:</th>
        <td><input type="text" name="Username" maxlength="10"/></td>
    </tr>
    <tr>
        <th align="left">Password:</th>
        <td><input type="password" name="Password" maxlength="10"/></td>
    </tr>
    <tr>
        <th align="left">Re-Enter Password:</th>
        <td><input type="password" name="Password2" maxlength="10" /></td>
    </tr>
    <tr>
        <th><br /></th>
        <td><br /></td>
    </tr>
    <tr>
        <td align="center"><input type="button" name="btnSubmit" value="Submit" onclick="Validate();"/></td>
        <td align="center"><input type="reset" value="Reset" /></td>
    </tr>
</table>
</form>

这是我的PHP代码:

<?php
// Connect to MySQL
$db = mysql_connect("", "nemo008", "CimesFungo");

if (!$db)
{
    exit("Error - Could not connect to MySQL");
}

$er = mysql_select_db("cs329e_fall11_nemo008", $db);

if (!er)
{
    exit("Error - Could not select the database");
}

$query = "INSERT INTO tblMembers (Username, Password, FirstName, LastName, Email, Phone, Year, Major)
VALUES ('$_POST[Username]', '$_POST[Password]', '$_POST[FirstName]', '$_POST[LastName]', '$_POST[Email]','$_POST[Phone]','$_POST[Year]','$_POST[Major]')";

$result = mysql_query($query);

if (!$result)
{
    print("Error - The query could not be executed");
    $error = mysql_error();
    print("<p>:" . $error . "</p>");
    exit;
}

echo("1 record added");

?>

错误地定义了提交的按钮类型。
它应该是 :-

<input type="submit" ...// instead of type="button"

您可以查看输入标记的工作原理: - https://developer.mozilla.org/en/HTML/Element/input

你错了

<input type="button" ../> 

写而不是

<input type="submit" ../>

if (!er)有一个$缺失。
尝试直接在DBMS中执行查询,因为我收到以下错误

密钥'PRIMARY'重复输入

首先确保您的数据库正确无误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM