从数据库

时间:2017-01-17 19:25:46

标签: php mysql database

我是php的新手。我试图用PHP显示SQL数据库的一些结果。

我有一个包含3个字段的搜索表单。当用户提交此表单时,如果搜索值与数据库匹配,则会显示一些结果。如果搜索值与数据库不匹配,则会显示错误消息。

<form method="post">
  <label>User ID</label>  
  <input name="textinput" type="text" value="">

  <label>Username</label>  
  <input name="textinput" type="text" value="">

  <label>Country</label>
    <select name="selectbasic">
      <option value="">Country 1</option>
      <option value="">Country 2</option>
      <option value="">Country 3</option>
    </select>

    <input type="submit" value="Submit">
</form>

这是我的数据库行

user_id;
user_name
user_email
full_name
country
division
user_bio

结果将显示如下......

User ID: 123
Username: XYZ
User Email: xyz
Full Name: xyz
Country: xyz
Division: xyz
User Bio: xyz

请给我一个解决这个问题的最佳解决方案。

这是我的代码,但此代码仅在user_id匹配时显示结果,但我需要匹配3个字段

<?php
$output = NULL;

if(isset($_POST['submit'])){
    $mysqli = NEW MySQLi("localhost","dbuser","password","dbname");

    $search = $mysqli->real_escape_string($_POST['search']);

    $resultsSet = $mysqli->query("SELECT * FROM data WHERE user_id = '$search'");

    if($resultsSet->num_rows > 0) {
        while($rows = $resultsSet->fetch_assoc())
        {
           $user_id = $rows['user_id'];
           $user_name = $rows['user_name'];
           $user_email = $rows ['user_email'];
           $full_name = $rows ['full_name'];
           $country = $rows ['country'];
           $division = $rows ['division'];
           $user_bio = $rows ['user_bio'];

           $output = "
          <td>$user_id</td>
          <td>$user_name</td>
          <td>$user_email</td>
          <td>$full_name</td>
          <td>$country</td>
          <td>$division</td>
          <td>$user_bio</td>";
        }
    }else{
            $output = "No results";
    }

}
?>

3 个答案:

答案 0 :(得分:0)

首先,您需要与数据库建立连接。

以下是一个例子:

try {
   $conn = new PDO("mysql:host=localhost;dbname = your_databasename","user","password");
}
catch (PDOException $e)
{
   echo "Error: ". $e->getMessage() . "\n" ;
   die();
}

然后,您需要将表单的结果添加到查询中。

示例:

$db = $conn->prepare('SELECT * FROM table_name WHERE user_id=? AND user_name=? AND country=?');
$db->bindParam(1, $_REQUEST["id"], PDO::PARAM_INT, 30);
$db->bindParam(2, $_REQUEST["name"], PDO::PARAM_STR, 30);
$db->bindParam(3, $_REQUEST["country"], PDO::PARAM_STR, 30);
$db->execute();
$data = $db->fetch(PDO::FETCH_BOTH);

$ _REQUEST之后的“[]”之间的名称是来自的名称参数

最后你需要编写代码来输出结果。

类似的东西:

echo "user id:".$data[user_id];
....

答案 1 :(得分:0)

我解决了这个问题。这是我的代码

<?php
$output = NULL;

if(isset($_POST['submit'])){
    //Connect to the database
    $mysqli = NEW MySQLi("localhost","dbuser","password","dbname");

    $search = $mysqli->real_escape_string($_POST['search']);
    $username = $mysqli->real_escape_string($_POST['username']);
    $country = $mysqli->real_escape_string($_POST['country']);

    //Query the database
    $resultsSet = $mysqli->query("SELECT * FROM data WHERE user_id = '$search' AND user_name = '$username' AND country = '$country'");

    if($resultsSet->num_rows > 0) {
        while($rows = $resultsSet->fetch_assoc())
        {
       $user_id = $rows['user_id'];
       $user_name = $rows['user_name'];
       $user_email = $rows ['user_email'];
       $full_name = $rows ['full_name'];
       $country = $rows ['country'];
       $division = $rows ['division'];
       $user_bio = $rows ['user_bio'];

           $output = "
      <td>$user_id</td>
      <td>$user_name</td>
      <td>$user_email</td>
      <td>$full_name</td>
      <td>$country</td>
      <td>$division</td>
      <td>$user_bio</td>";
        }
    }else{
            $output = "No Results Found";
    }

}
?>

搜索表单

<form method="post">
  <label>User ID</label>  
  <input name="search" type="text" value="">

  <label>Username</label>  
  <input name="username" type="text" value="">

  <label>Country</label>
    <select name="country">
      <option value="">Country 1</option>
      <option value="">Country 2</option>
      <option value="">Country 3</option>
    </select>

    <input type="submit" value="Submit">
</form>

答案 2 :(得分:-2)

用这句话你可以从你的表中得到最后一个id $ sql =&#34; SELECT id FROM table_name ORDER BY id DESC LIMIT 1&#34 ;; 然后你可以使用for循环并检查用户输入的字段与数据库中的所有字段。