从Mysql / PHP获取名称或UserID的用户信息

时间:2016-12-09 12:17:43

标签: php mysql

我能知道这里的错误吗? 我的数据库中有两个表。 就是这个

table

table2

我把代码编写为搜索引擎的东西。我将名称或用户ID放在表单中,它将处理用户的信息。

这是我的代码

<html lang="en">
 <head>
 <meta charset="utf-8" />
   <title></title>
 </head>
 <body>
 <form action="" method="post">
 Search: <input type="text" name="term" />
 <input type="submit" value="Submit" />
 </form>
 <?php
 include('config.php');
 if (!empty($_REQUEST['term']))
	{
	$term = mysql_real_escape_string($_REQUEST['term']);
	$sql = " select u.* from users u inner join ranks r ON (u.UserID = r.UserID) where u.UserID = '%" . $term . "%'";
	$r_query = mysql_query($sql);
	while ($row = mysql_fetch_array($r_query))
		{
		echo 'Name: ' . $row['Name'];
		echo '<br /> Cash: ' . $row['Cash'];
		echo '<br /> Score: ' . $row['Score'];
		echo '<br /> Race: ' . $row['Race'];
		echo '<br /> Horseshoe: ' . $row['Horseshoe'];
		}
	}
?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您应该在查询中使用like,如下所示

$sql = " select u.* from users u inner join ranks r ON (u.UserID = r.UserID) where u.UserID like '%" . $term . "%'";

答案 1 :(得分:0)

首先,您应该将config.php更新为mysqli_个功能。 并且mysqli_real_escape_string()mysqli_query()功能需要2个参数。
首先$conn,第二个:变量

最后你的代码应该是这样的:

<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title></title>
</head>
<body>
<form method="POST">
    Search: <input title="searchfield" required type="text" name="term"/>
    <input type="submit" name="submit" value="Submit"/>
</form>
<?php

include('config.php');

if (isset($_POST["submit"]) && !empty($_POST["submit"])) {
    $term = mysqli_real_escape_string($conn, $_REQUEST["term"]); //make sure the $conn isset
    $sql = "SELECT u.* FROM users u INNER JOIN ranks r ON (u.UserID = r.UserID) WHERE u.UserID LIKE '%" . $term . "%'"; // change = to LIKE
    $r_query = mysqli_query($conn, $sql);

    while ($row = mysqli_fetch_array($r_query)) {
        echo 'Name: ' . $row['Name'];
        echo '<br /> Cash: ' . $row['Cash'];
        echo '<br /> Score: ' . $row['Score'];
        echo '<br /> Race: ' . $row['Race'];
        echo '<br /> Horseshoe: ' . $row['Horseshoe'];
    }
}
?>

</body>
</html>

您的config.php应如下所示:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "idkw0t";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>