我是 MySQL 和 PHP 的新手。我有一个 HTML 表单,我想在其中将 1 个变量传递给我的 PHP 代码,然后在我的数据库上运行一个查询,以获取在“序列”列下保存该变量的记录。当我对要查找的“序列”进行硬编码时,我可以正常运行它,但是当我尝试使用变量时,我得到一个错误。
任何帮助将不胜感激!或者更好的方法来做到这一点。
这是我的错误:Unknown column 'amg002' in 'where clause'
这是我的代码;
$serial= $_POST['Serial'];
echo $serial;
//Connect To Database
$link = mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
echo "Connected to MySQL<br />";
//Select the database - 'SiteInfo'
// Collects data from "SiteInfo" table
//****This is where I am running into the error***
$sql = 'SELECT * FROM `SiteInfo` WHERE `Serial` ='.$serial;
// This works!!!
//$sql = 'SELECT * FROM `SiteInfo` WHERE `Serial` ="amg002";';
$data = mysql_query($sql)
or die(mysql_error());
// puts the "SiteInfo" info into the $info array
$info = mysql_fetch_array( $data );
//Print out the contents of the entry
echo "Site Name: ".$info['SiteName'] . "<br /";
Print "Serial Number: ".$info['Serial'] . "<br />";
Print "Location: ".$info['Location'] . "<br />";
// Close the database connection
mysql_close($link);
echo "Connection Closed. <br />";