2016-12-01 86 views
2

我開始在PHP,我想知道如何顯示在HTML文件中的文本框中的MySQL數據。 我已經創建了兩個文件一個HTML和一個PHP,在調用數據到文本發送錯誤的那一刻。如何在文本框中顯示來自mysql的數據?

Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...

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

你應該使用「mysqli_select_db()」而不是「mysql_select_db()」嗎?最好與mysqli調用所有你的後續調用 –

+0

我改變它爲「mysql_select_db」,但仍然是一個錯誤**「警告:mysql_fetch_array()期望參數1是資源,布爾在」** –

回答

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

關於保護你的數據庫,我可能會建議使用準備好的語句來防止sql注入 –

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"; 
} 
?> 
0

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

這意味着您的查詢失敗。它返回false。您沒有使用$_POST['client']可變權利。你忘了報價!

這應該工作:

$query = mysql_query("SELECT city , zipcode FROM data WHERE client='".$_POST["client"]."'"); 

而且你是脆弱到MySQL注入和使用MySQL的mysqli instad。

相關問題