PHP / MySQL - 在按钮上更新数据库/运行PHP脚本单击

时间:2012-12-05 15:53:37

标签: php html mysql button

我在PHP中有一个“注册”页面,我希望在单击HTML按钮时运行该脚本。

PHP基本上会检查是否所有字段都已填写,检查密码和电子邮件确认是否相同并保存到数据库中。

这是代码:

<?php
$Name = isset($_POST['Name']);
$Surname = isset($_POST['Surname']);

$Username = isset($_POST['Username']);

$Email = isset($_POST['Email']);
$C_Email = isset($_POST['C_Email']);

$Password = isset($_POST['password']);
$C_Password = isset($_POST['c_password']);

$SecQ = isset($_POST['SecQ']);
$SecA = isset($_POST['SecA']);


$con = mysql_connect('localhost', 'admin', 'storefile1234');
mysql_select_db ("storefile");

$check_username = mysql_query("SELECT FROM users WHERE username = '$Username'");
$check_email = mysql_query("SELECT FROM users WHERE Email = '$Email'");


if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

if ($Name == null || $Surname== null || $Username == null || $Password == null || $C_Password == null || $Email == null || $C_Email == null || $SecQ == null || $SecA == null ) {

    echo "Missing details. Please enter all fields.";


} else {

    if(mysql_num_rows($check_username) != 0 && mysql_num_rows($check_email) != 0)
            {
            echo "Username/Email already exists";
            }
            if  ($Email == $C_Email && $Password == $C_Password) {

                $query = "INSERT INTO users (Username, Name,Surname, Password, Email, SecQ, SecA) VALUES ('NULL', ".$Username."', ".$Name."', ".$Surname."', ".$Password."', ".$SecQ."', ".$SecA."', ".$Email.')"';

                mysql_query($query) or die ('Error registering.');

                echo "Greetings, ".$Name.", you have been registered. ";

    }  else {

        echo "Error registering your account. Please try again.";
            }

 }


?>

另外,是推荐吗?

每当我运行此页面Missing details. Please enter all fields.时,都会显示,但未输入任何详细信息。

你是怎么做到的?

3 个答案:

答案 0 :(得分:2)

你想通过isset($_POST['Username']);来获取价值,就像这个功能一样...... 但documentation说:Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

请检查true,坚持null。并在之后转义你的POST数据。

你可以这样做:

$Name = isset($_POST['Name']) ? mysql_real_escape_string($_POST['Name']) : null;


P.S。请再说一遍。不要使用mysql_*功能。他们 DEPRECATED 查看 PDO (或mysqli_*)

答案 1 :(得分:0)

您滥用isset

尝试这样的事情:

$Name = null;
if (isset($_POST['Name'])) {
    $Name = $_POST['Name'];
}

isset仅用于检查是否设置了值。

答案 2 :(得分:0)

有关在首次加载页面时打印该消息的问题,请使用array_key_exists函数测试用户是否已提交某些内容,然后再检查是否有任何字段为空。像这样:

if (array_key_exists('Name', $_POST) || array_key_exists('Surname', $_POST) || ... ) 
    if ($Name == null || $Surname== null || ... ) 
        echo "Missing details. Please enter all fields.";

观察:你不能将isset函数用于同一目的,因为根据php文档,它“确定变量是否设置为并且不是NULL