我正在起诉自动完成以显示数据库中的值。该文件如下所示:
自动完成.php
<?php
require_once "../includes/conf.php";
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
//$mysqli=mysql_connect('localhost','root','','autofield') or die("Database Error");
$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql) or die(mysql_error());
if($result)
{
while($row=mysql_fetch_array($result))
{
echo $row['vName']." </n>".$row['id'];
}
}
?>
上面的文件引用了将在文本文件中显示的名称。除此之外,我还想将id作为隐藏字段传递,以便我可以在php中处理数据
我应该怎么做?
您可以使用隐藏的输入类型来实现此目的。
<input type="hidden" value=".$row['id']."/>
试试这个:
$array = array();
while($row=mysql_fetch_array($result))
{
array_push($array ,array("value"=>$row['id'],"label"=>$row['vName']));
}
在jquery代码中:
terms.push( ui.item.label );
$( "#hidtextboxid" ).val( ui.item.value);
确保在代码中创建一个隐藏字段。
检查这个:如何在 jquery UI 自动完成中使用 JSON 传递隐藏的 ID?
我不想用mysql_query
回答你的问题,原因有两个:
1. mysql_query官方状态已弃用:mysql 扩展已弃用,将来将被删除:改用 mysqli 或 PDO
2. 容易受到 SQL 注入的影响,请参阅 如何防止 PHP 中的 SQL 注入?
使用 PDO(PHP 数据对象(代替,它是安全的并且是面向对象的以下是一些教程,可以在 12 个视频中掌握这一点 http://www.youtube.com/watch?v=XQjKkNiByCk
将您的 MySQL 实例替换为此实例
// instance of pdo
$config['db'] = array
(
'host' => '',
'username' => '',
'password' => '',
'dbname' => ''
);
$dbh = new PDO('mysql:host=' . $config['db']['host'] .
';dbname=' . $config['db']['dbname'],
$config['db']['username'],
$config['db']['password']);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
global $dbh;
//dbh 只是对象的自定义名称,您可以将其命名为 数据库
编辑凭据,接下来让我们查询您的代码,如果实例不在同一文件上,即包含连接脚本,则在启动 SQL 之前调用 global $dbh;
,否则将对象带到当前文件
所以你的代码将看起来像这样
<?php
global $dbh;
//lets prepare the statement using : to input what ever variables we need to (securely)
$displayData= $dbh->prepare("SELECT vName,id FROM employee WHERE vName LIKE :my_data ORDER BY vName");
$displayData->bindValue(':my_data', $my_data , PDO::PARAM_STR);
//then we execute the code
$displayData->execute();
//store the result in array
$result = $displayData->fetchAll();
print_r($result); //take a look at the structured
//depending on the structure echoing could be like **echo $result[0][theIdYouTrynaGet];**
?>
那么我将如何在另一页中检索它呢?
<html>
<?php
include_once '/*the path of your file where the above is happening*/';
<input type="hidden" value="<?php echo $result['pass in your parameters'] ?>"/>
?>
<html>