1

我正在尝试立即注册!餐厅网站的区域,并希望在所有运行members_t的数据库表中插入新成员的数据。我为此目的使用 PHP 和 HTML。此外,我正在一个单独的文件中使用 javaScript 进行表单验证,效果很好!memberslocalhost

PHP的代码:

<?php
$user="root";
$password="";
$database="members";
$con = mysql_connect('localhost',$user,$password);
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con) or die( "Unable to select database");
if(isset($_POST['sign_up']) && !empty($_POST['sign_up']))
{
    $sql = "INSERT INTO members_t('Name', 'Email', 'Password', 'Phone', 'Address', 'Sex', 'More') VALUES('".$_POST['username']."','".$_POST['email']."','".$_POST['passid_1']."','".$_POST['zip']."','".$_POST['address']."','".$_POST['sex']."','".$_POST['desc']."');";
    $resultDI = mysql_query($sql, $con) or die(mysql_error());
    mysql_close($con);
    echo "Successfolly run database query!";
}
else 
{
    echo("Failed to update database!!!");
}
?>

HTML 代码:

<html>
<body>
<h2 class="letter_spacing">Not a Member?<span><br>Sign Up Now:</br></span></h2>
<form id = "register" name="registration" method = "post" onSubmit="return formValidation();">
<ul>
<li><label for="username">* Full Name:</label></li>
<li><input type="text" name="username" size="50" /></li>
<li><label for="email">* Email:</label></li>
<li><input type="text" name="email" size="50" /></li>
<li><label for="passid_1">* Desired Password:</label></li>
<li><input type="password" name="passid_1" size="12" /></li>
<li><label for="passid_2">* Re-Enter Password:</label></li>
<li><input type="password" name="passid_2" size="12" /></li>
<li><label for="zip">* Contact Number:</label></li>
<li><input type="text" name="zip" /></li>
<li><label for="address">* Address:</label></li>
<li><input type="text" name="address" size="50" /></li>
<li><label id="gender">* Sex:</label></li>
<li><input type="radio" name="msex" value="Male" /><span>Male</span></li>
<li><input type="radio" name="fsex" value="Female" /><span>Female</span></li>
<li><label for="desc">Anything More:</label></li>
<li><textarea name="desc" id="desc" cols="40" rows="4"></textarea></li>
<li><label for="note" ><h6>Note: All feilds marked with * are necessary</h6></label></li>
<li><input class="button1" type="submit"  name="sign_up" value="Sign Up!" /></li>
</ul>
</form>
</body>
</html>

我试图将代码保存在一个名为 insert.php 的单独文件中,并将操作字段添加到 HTML 表单标记中,但没有用。

我永远无法将数据插入数据库。似乎 PHP 代码从未进入

if(isset($_POST['sign_up']) && !empty($_POST['sign_up']))

堵塞。

4

2 回答 2

0

尝试这个:

<form id="register" action="" method="post" name="registration" onSubmit="return formValidation();">
    <input type="text" name="username" size="50" />
    <input type="text" name="email" size="50" />
    <input type="password" name="passid_1" size="12" />
    <input type="password" name="passid_2" size="12" />
    <input type="text" name="zip" />
    <input type="text" name="address" size="50" />
    <input type="radio" name="sex" value="Male" /><span>Male</span>
    <input type="radio" name="sex" value="Female" /><span>Female</span>
    <textarea name="desc" id="desc" cols="40" rows="4"></textarea>
    <input class="button1" type="submit"  name="sign_up" value="Sign Up!" />
</form>

<?php
if (isset($_POST['sign_up']) && !empty($_POST['sign_up'])) {
    // escape all submitted data before inserting into database
    foreach ($_POST as $key => $value) {
        $_POST[$key] = mysql_real_escape_string(strip_tags($value));
    }

    $result = mysql_query("
        INSERT INTO members_t (Name, Email, Password, Phone, Address, Sex, More)
        VALUES ('{$_POST['username']}', '{$_POST['email']}', '{$_POST['passid_1']}', '{$_POST['zip']}', '{$_POST['address']}', '{$_POST['sex']}', '{$_POST['desc']}')
    ") or die(mysql_error());

    if (mysql_affected_rows() == 1) {
        echo "Successfully run database query!";
    } else {
        echo("Failed to update database!!!");
    }
}
?>

请注意,radio按钮的名称应与您的代码中的“sex”而不是“msex”和“fsex”相同。我已经action在标签中添加了属性form以及您可以轻松注意到的其他一些修改。

于 2012-12-09T16:17:43.837 回答
0

首先,我对代码进行了一些清理,使其看起来既漂亮又流畅。然后我删除了你制作的 !empty 部分,当你已经使用 isset 时,看不到你想要验证它实际上是空的原因。HTML:

<?php
$hostname = "";
$user = "root";
$password = "";
$database = "members";

$desc = $_POST['desc'];

$con = mysql_connect($hostname, $user, $password);
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con) or die( "Unable to select database");
if(isset($_POST['sign_up']))
{
    if(isset($_POST['username'])){
        $username = $_POST['username'];
    }
    else {
        echo "The username is not set"; die;
    }
    if(isset($_POST['email'])){
        $email = $_POST['email'];
    }
    else {
        echo "The email is not set"; die;
    }
    if(isset($_POST['zip'])){
        $zip = $_POST['zip'];
    }
    else {
        echo "The zip code is not set"; die;
    }
    if(isset($_POST['address'])){
        $address = $_POST['address'];
    }
    else {
        echo "The gender is not set"; die;
    }
    if(isset($_POST['sex'])){
        $sex = $_POST['sex'];
    }
    else {
        echo "The gender is not set"; die;
    }
    if(isset($_POST['passid_1'])){
        $passid = $_POST['passid_1'];
    }
    else {
        echo "The password is not set"; die;
    }
    if(isset($_POST['passid_2'])){
        $passid2 = $_POST['passid_2'];
    }
    else {
        echo "The re-entered password is not set"; die;
    }

    if($passwid == $passid2){
        $correctpid = $passwid;
    }
    else {
        echo "The passwords do not match"; die;
    }
    $sql = "INSERT INTO members_t('Name', 'Email', 'Password', 'Phone', 'Address', 'Sex', 'More') VALUES('$username', '$email','$correctpid', '$zip', '$address', '$sex', '$desc');";
    mysql_query($sql) or die(mysql_error());
    mysql_close($con);
    echo "Successfolly run database query!";
}
else 
{
    echo("Failed to update database!!!");
}
?>

我已经编写了 php 代码来检查所有字段是否都填充了数据。如果不是,该站点就会死掉并给他们一个错误消息。它会在将任何内容设置到数据库之前杀死网站。

——我在评论后对代码做了一些修改,谢谢顺便说一句。

于 2012-12-09T16:29:24.493 回答