2014-01-14 36 views
0

這是我的代碼在這裏我得到的問題這是一個輸入類型標籤,而不是從數據庫使用PHP選擇數據。 這裏是這段代碼的第一部分是HTML格式的廣告。第二部分是php 我想選擇數據庫中的數據slect數據相同的頁面,因爲我們看到在渾身溼透的車網站我怎麼能從數據庫使用輸入類型框中選擇數據並在同一頁上打印

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>phpSelect</title> 
</head> 
<body> 
Insert Age for search 
<form action="#" method="post" > 
<input type="text" id="val" name="resValue" /> 
<input type="submit" value="submit" /></form> 
<?php 
if(isset($_POST['submit'])) 
{ 
    $res=$_POST['resValue']; 

    echo $res; 
    } 
//echo $res; 
$con=mysqli_connect("localhost","root","","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
$result = mysqli_query($con,"SELECT * FROM Persons where Age=25"); 
echo "<table border='1'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
</tr>"; 
while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['FirstName'] . "</td>"; 
    echo "<td>" . $row['LastName'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 
mysqli_close($con); 
?> 
</body> 
</html> 
+0

指定錯誤請 –

+0

你必須改變。您的選擇採取文本框。 –

回答

0

嘗試更改SQL本

SELECT * FROM Persons where Age='".mysql_escape_string($formValue['val'])."'

0

您選擇查詢更改爲這一個。

SELECT * FROM Persons where Age='".mysql_escape_string($_POST['val'])."' 
+0

你能否添加一些關於你的代碼如何工作的額外信息? – Max

0

我在你的代碼中發現的第一個問題就在這裏:

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

它應該是:

<input type="submit" value="submit" name="submit" /> 

爲了能夠獲得滿意的結果。下面是代碼:

<?php 
$query = ""; 
if(isset($_POST['submit'])) 
{ 
    $res=$_POST['resValue']; 
    $query = " where Age='$res'" 
} 
//echo $res; 
$con=mysqli_connect("localhost","root","","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$result = mysqli_query($con,"SELECT * FROM Persons $query"); 
echo "<table border='1'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
</tr>"; 
while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['FirstName'] . "</td>"; 
    echo "<td>" . $row['LastName'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 
mysqli_close($con); 
?> 
+0

感謝兄弟幫我這麼一個。 – Smoke

+0

請接受此作爲答案並投票表示有幫助。謝謝。 – user3045072

0

在你的問題形式的所有值通過表格的所有字段的名稱得到\ 所以這裏應該是

<input type="submit" name="submit" value="submit"/> 

Because in $_POST['submit'] submit is same as button's name. 
相關問題