注册页面PHP出错

时间:2014-02-27 13:38:24

标签: php html mysqli

我正在为我的网站创建一个注册页面,这是我收到的错误消息:

Connected Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/k1003140/www/Group8/connectivity-sign-up.php on line 23 Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/k1003140/www/Group8/connectivity-sign-up.php on line 23

这是PHP代码:

<?php

include_once("db_connection.php");

function NewUser()
{
    $fullname = $_POST['name'];
    $userName = $_POST['user'];
    $email = $_POST['email'];
    $password =  $_POST['password'];
    $query = "INSERT INTO Customer (fullname,userName,email,password) VALUES ('$fullname','$userName','$email','$password')";
    // $data = mysqli_query ($db_connection, $query) or die(mysqli_error());
    $data = mysqli_query ($db_connection, $query) or die(mysqli_error($db_connection));
    if($data)
    {
        echo "YOUR REGISTRATION IS COMPLETED...";
    }
}


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

        if(!$row = mysqli_fetch_array($db_connection, $query) or    die(mysqli_error($db_connection)));
        {
            newuser();
        }
        else
        {
            echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
        }
    }
}
if(isset($_POST['submit']))
{
    SignUp();
}

?>

我通过My​​sqli连接,但我不知道如何使用它。

3 个答案:

答案 0 :(得分:1)

mysqli_query期望第一个参数作为连接,您只是从文档

传递查询

程序风格

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

所以行

$data = mysqli_query ($query)

首先应该有连接变量

$data = mysqli_query ($connection, $query)

同样需要连接链接的mysqli_error

string mysqli_error ( mysqli $link )

所以完整的代码看起来像

data = mysqli_query ($connection, $query) or die(mysqli_error($connection));

只需使用您的实际连接变量

更改$connection

另一方面,我不会补充说你有mysql injection的风险,你真的应该使用预备语句,因为你已经使用了mysqli并支持它。 Learn more here

答案 1 :(得分:0)

注意行号。你需要在第23行进行Fabio的改变。

答案 2 :(得分:-1)

当您在过程风格中使用mysqli时,只需将代码写为: -

$mysqliLink = new mysqli('', '', '', '');

当你想执行一个sql语句时,它写成: -

$sqlStatement = '';
$query = mysql_query($mysqliLink, $sqlStatement);

在您的情况下,您必须通过以下函数解析链接连接: -

function NewUser($mysqliLink)
{
$fullname = $_POST['name'];
$userName = $_POST['user'];
$email = $_POST['email'];
$password =  $_POST['password'];
$query = "INSERT INTO Customer (fullname,userName,email,password) VALUES ('$fullname','$userName','$email','$password')";
$data = mysqli_query ($mysqliLink, $query);

// complete you code.

}

我所写的内容非常简单,你必须考虑更多的东西,如防止注射,......,..,等等。