duangengruan2144 2016-12-09 12:17
浏览 62
已采纳

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

Can I know what's mistake in here? I have two tables in my database. it is this

table

table2

I have written the code as search player thing. I'll put the name or userid in the form and it'll process the information of user.

Here is my code

<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>

</div>
  • 写回答

2条回答 默认 最新

  • dpepbjp126917 2016-12-09 12:37
    关注

    First of all you should update your config.php to mysqli_ functions. and the mysqli_real_escape_string() and mysqli_query() functions need 2 Parameters.
    First $conn, second: the variable

    finally your code should look like this:

    <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>
    

    your config.php should look like this:

    <?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";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?