MySQL和PHP插入不起作用

时间:2016-06-08 10:29:32

标签: php mysql

我正在尝试为我的用户创建注册表单。我创建了PHP如下:

<?php

define('DB_HOST', 'mysql7.000webhost.com');
define('DB_NAME', ' <M PUTTING DATABASE USERNAME HERE> ');
define('DB_USER','<IM PUTTING USERNAME HERE>');
define('DB_PASSWORD','<M PUTTING PASSWORD HERE>');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());


function NewUser()
{
    $username= $_POST['username'];
    $email = $_POST['email'];
    $password =  $_POST['password'];
    $query = "INSERT INTO members (username,email,password) VALUES ('$username','$email','$password')";
    $data = mysql_query ($query)or die(mysql_error());
    if($data)
    {
    echo "YOUR REGISTRATION IS COMPLETED...";
    }
}

function SignUp()
{
if(!empty($_POST['username']))   //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
    $query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die(mysql_error());

    if(!$row = mysql_fetch_array($query) or die(mysql_error()))
    {
        newuser();
    }
    else
    {
        echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
    }
}
}
if(isset($_POST['submit']))
{
    SignUp();
}
?>

这是我的HTML用户界面页面

<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script> 
$(function(){
});
  $("#header").load("header.html"); 
  $("#footer").load("footer.html"); 
</script> 
<title>Mases Krikorian Website</title>
<link rel="stylesheet" type="text/css" href="index.css">
<link rel="stylesheet" type="text/css" href="login.css">
<style>
h2{
font-family: "Arial Black", Gadget, sans-serif;
}
td{
padding-left: 5px;
padding-right: 5px;
}
</style>
<div id="header"></div>
<div class="bgpic"></div>
</head>
<body>
<div class="container">
<div id="content">

<form method="POST" action="create.php">
<h1>Registration Form</h1>
            <div>
                <input type="username" placeholder="Username" required="" id="username" />
            </div>
                        <div>
                                 <input type="password" placeholder="Password" required="" id="password" />

                        </div>
            <div>
                <input type="email" placeholder="Email" required="" id="email" />
            </div>

                         <div>
            </div>

            <div>
                <input id="submit" type="submit" name="submit" value="Sign Up">

            </div>
</form><!-- form -->





</div>
</div>
</body>
<div id="footer"></div> 

请注意我没有任何数据库问题,我的主要问题是如果我删除了对空函数的检查,我的用户名变量将返回空白。此外,UI页面名称为create.html,php页面名称为create.php,表名称为成员。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

这真的很简单,你现在会很惊讶。

更改用户名和所有

的代码
 <input type="username" placeholder="Username" required="" id="username" />

 <input type="text" name="username" placeholder="Username" required="" id="username" />

您可能会发现两个使用过的混乱和#34;用户名&#34;在不允许的类型中,您也没有name属性,因此它不会将其数据发送到下一个php页面。

我相信你找到了答案但请更改上面提到的其他两个元素。

建议用户使用NETBEANS,因为它会显示此类错误,这将有所帮助。

祝你好运

答案 1 :(得分:0)

  1. 请仔细检查此代码

    $query = mysql_query("SELECT * FROM members WHERE username ='$_POST[username]' AND password = '$_POST[password]'") or die(mysql_error());
    
  2. 在您的&#39; create.php&#39;文件,仔细检查此代码

    newuser();
    
  3. 在您的.html文件中 更改这3行

    <input type="username" placeholder="Username" required="" id="username" />
    
    <input type="password" placeholder="Password" required="" id="password" />
    
    <input type="email" placeholder="Email" required="" id="email" />
    

    <input type="text" placeholder="Username" required="" id="username" name="username"/>
    
    <input type="password" placeholder="Password" required="" id="password" name="password"/>
    
    <input type="email" placeholder="Email" required="" id="email" name="email"/>