我无法运行我的PHP程序,但收到了一些错误

时间:2016-05-16 01:29:32

标签: php mysql html5 web phpmyadmin

这是我收到警告的图像

enter image description here

我是新手,我真的很想学习PHP。



<?php
	include 'mysql.conf.php';

	if(isset($_POST['submit']))
	{
		if(!empty($_POST['user']) && !empty($_POST['password']))
		{
			$user = $_POST['user'];
			$password = $_POST['password'];
			$select = "SELECT * FROM admin where user=$user && password=$password";
			$sql=mysql_query($select) or die(mysql_error());
			if($sql)
			{
				echo "Login";
			}
			else
			{
				echo "Cannot Login";
			}
		}
	}

?> 
&#13;
&#13;
&#13;

下面是我的mysql.conf.php代码

&#13;
&#13;
<?php
	$name = "localhost";
	$user = "root";
	$pass = "gasamul";

	$connect = mysql_connect($name, $user, $pass) or die(mysql_error());

	$db = "portfolio";
	if($connect)
	{
		$db = mysql_select_db($db) or die(mysql_error());
		if($db)
		{
			//echo 'Database Connect';
		}
	}
	else
	{
		echo 'Database can\'t connect';
	}
?>
&#13;
&#13;
&#13;

我的数据库名称是投资组合。我还将我的表管理员命名为。

1 个答案:

答案 0 :(得分:0)

由于错误明确表明mysql_*扩展程序已经deprecated,并且建议您使用mysqli_*PDO扩展程序。

让我们尝试第一个选项 - mysqli_*。我们首先在mysql.conf.php

中建立与您的数据库的连接
$connect = new mysqli("localhost", "root", "gasamul", "portfolio");

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

然后,我们可以继续logintest.php

include 'mysql.conf.php';

if(isset($_POST['submit']) && !empty($_POST['user']) && !empty($_POST['password']))
{

    if($stmt = $con->prepare("SELECT * FROM admin WHERE user = ? AND password = ?")){ /* PREPARE YOUR QUERY  */
        $stmt->bind_param("ss", $_POST['user'], $_POST['password']); /* BIND THESE TWO VARIABLES TO THE QUERY ABOVE RESPECTIVELY; s STANDS FOR strings */
        $stmt->execute(); /* EXECUTE THE QUERY */
        $stmt->store_result(); /* STORE THE RESULT */
        $noofrows = $stmt->num_rows; /* GET THE NUMBER OF RESULT */
        if($noofrows > 0){ /* CHECK IF RESULT IS MORE THAN 0 */
            echo "Login";
        } else {
            echo "Cannot Login";
        }
        $stmt->close(); /* CLOSE THE PREPARED STATEMENT */
    }

}

您还可以查看password_hash()以确保在数据库中保护您的密码。