不向数据库插入值:

时间:2012-12-09 15:57:12

标签: php html mysql

我正在尝试立即注册!用于餐馆网站的区域,并希望在members_t数据库members表中插入新成员的数据,并且所有成员都在localhost上运行。我正在使用PHP和HTML。此外,我正在使用javaScript在单独的文件中进行表单验证,该文件运行正常!

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']))

块。

2 个答案:

答案 0 :(得分: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按钮的名称应与您的代码中的“性别”相同,而不是“msex”和“fsex”。我在action标记中添加了form属性以及您可以轻松注意到的其他一些修改。

答案 1 :(得分:0)

首先,我已经清理了一些代码,因此看起来很漂亮。然后我删除了你制作的!空白部分,当你已经使用了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代码来检查所有字段是否都填充了数据。如果不是该网站死亡并给他们一个错误消息。它会杀死网站,然后才能将任何内容设置到数据库中。

- 我在评论后对代码做了一些更改,谢谢btw。