我正在尝试根据从表单输入发布的char来显示mysql数据库中的记录。但我不能让这个工作。 我的HTLM代码在
之下<form action='headcode_Db_connect.php' method='POST'>
<input type="text" name="headcode" value="" maxlength="2">
</form>
我的PHP代码
<?php
$headcode = $_POST['headcode'];
echo $headcode;
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="789852"; // Mysql password
$db_name="work"; // Database name
$tbl_name = "headcodes";
// Connect to server and select database.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//Retrieve all data from the table
$sql = "SELECT * FROM $tbl_name WHERE LIKE '%$headcode%'";
$result1 = mysql_query($sql, $link);
// if successfully, displays message "Successful".
if($result1){
$connect = "connected";
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
while ($row = mysql_fetch_assoc($result1)){
$number = $row['number'];
}
echo $number;
?>
我一直收到错误。发送的数据将为1A或2A,并应显示该记录的数据库中的数字。如果我将WHERE LIKE '%$headcode%'";
更改为WHERE headcode = '1A'";
,我会得到17的结果,这是正确的。那么如何使用POST变量来提供这个结果呢。
答案 0 :(得分:0)
WHERE LIKE '%$headcode%'"
如果您搜索at
(例如),则以下内容全部匹配:
at, bat, cat, rat
但是,如果您丢失了第一个%
,则必须以您的搜索字词开头
WHERE LIKE '$headcode%'"
将返回以at
开头的所有单词。希望这有帮助!
答案 1 :(得分:0)
我使用下面的代码。
<?php
$headcode = $_POST['headcode'];
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="789852"; // Mysql password
$db_name="work"; // Database name
$tbl_name = "headcodes";
// Connect to server and select database.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//Retrieve all data from the table
$sql = "SELECT * FROM $tbl_name WHERE headcode = '$headcode'";
$result1 = mysql_query($sql, $link);
if($result1){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
while ($row = mysql_fetch_assoc($result1)){
$number = $row['number'];
}
echo $number;
?>