如何在文本框中显示mysql的数据?

时间:2016-12-01 08:02:56

标签: php html mysql

我从php开始,我想知道如何在html文件中的文本框中显示mysql数据。 我创建了两个文件,一个html和一个php,在调用数据到文本时发送错误。

  

错误:    警告:mysql_fetch_array()要求参数1为资源,布尔值在...中给出

PHP文件

<?php

$server= "localhost";
$user = "root";
$password = "";
$bd = "test";

$conexion = mysql_connect($server,$user,$password) or die ("alert(error)");
mysql_select_db($bd,$conexion);
$query = mysql_query("SELECT city , zipcode  FROM data WHERE client='$_POST[client]'");

while($rowvalor = mysql_fetch_array($query))
{
        $city = $rowvalor['city'];
        $code = $rowvalor['zipcode'];
}
mysql_close($conexion);
?>

Html文件

<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<body>
<form action = "test.php" method="post">
 <button id="btnsearch"  onclick="search()" style= "background: rgb(242, 243, 243); position: relative; left: 155px"; >Search</button> 

<input id="txtclient" name="client" type="text" class="form-control" value= '<?php echo $client; ?>'/><span class="fa arrow"></span>

 <input id="txtcode" name="zipcode" type="text" class="form-control" value= '<?php echo $zipcode; ?>'/><span class="fa arrow"></span>

<input id="txtcity" name="city" type="text" class="form-control" value= '<?php echo $city; ?>' /><span class="fa arrow"></span>

</form>
</body>

3 个答案:

答案 0 :(得分:0)

你正在使用mysql_*函数和mysqli_functions,可能出现了什么问题。我建议您仅使用PDOmysqli_*函数。在您的代码中,您也使用mysql_select_db函数错误,您需要随机播放2个参数。

mysqli示例

$conexion = mysqli_connect($server, $user, $password) or die ("alert(error)");
mysqli_select_db($conexion, $bd);
$query = mysqli_query($conexion, "SELECT city , zipcode  FROM data WHERE client='$_POST[client]'");

while ($rowvalor = mysqli_fetch_array($query)) {
        $city = $rowvalor['city'];
        $code = $rowvalor['zipcode'];
}
mysqli_close($conexion);

您还在查询中使用了一个帖子值,因此您应该转义$ _POST值以保护它。

$client = mysqli_real_escape_string($conexion, $_POST['client']);
$query = mysqli_query($conexion, "SELECT city , zipcode  FROM data WHERE client='$client'");

答案 1 :(得分:0)

请更新您的php文件,如下所示:

<?php

$server = "localhost";
$user = "root";
$password = "";
$bd = "test";

// Create connection
$conn = mysqli_connect($server, $user, $password, $bd);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT city , zipcode  FROM data WHERE client='$_POST[client]'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        $city = $row['city'];
        $code = $row['zipcode'];
    }
} else {
    echo "0 results";
}
?>

答案 2 :(得分:0)

  

警告:mysql_fetch_array()期望参数1是资源,   

中给出的布尔值

这意味着您的查询失败。它返回 'reply_markup'=>json_encode([ 'inline_keyboard'=>[ [ ['text'=>'Num1'],['text'=>'Num2'] ], [ ['text'=>'Num3'] ], ] ]) ]); } 。您没有使用false变量权限。你忘记了引号!

这应该有效:

$_POST['client']

此外,你很容易受到mysql注入并使用mysq的mysqli instad。