根据用户输入从数据库中检索数据 [英] Retriving data from database based on user input

查看:83
本文介绍了根据用户输入从数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,我正在学习基础知识,我设计了一种具有动态下拉菜单的表单,该菜单中的选项直接从Access DB中填充.用户选择一个选项后,与该选项有关的信息将显示在操作页面中,但不能正常工作.我花了好几天的时间运行代码,但似乎并没有弄错什么.注释的代码似乎是我的问题根源. 这是代码

I am new to php, and I am learning the basics, I designed a form that has a dynamic drop down menu where the options are populated directly from my Access DB. After the user choose an option, the information related to that option would be displayed in a operate page, but that does not work properly. I run over my code for days and I do not seem to get what is wrong. The commented code seems to be my source of problem. this is the code

<html>
<head>
<title>Menu</title>
</head>
<body>
<?php
// This would be the value passed from the previous php page
$option =$_POST['myDropdown'];

// for testing purposes
print("$option");

// print image of the menu item or dish
print <<< HERE

<p>
<img src = "DishesPictures/Dish-$option.png" border="1" bordercolor="black"
       alt = "die: $option" />

</p>
HERE;

$conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 


 $connString= "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=e:\\ectserver\\naljalidi\\Database\\Menu.mdb";
    //creates the connection object and define the connection string

// for testing purposes
print("$connString");

$rs=$conn->Execute("SELECT ItemID,ItemDesc,Price FROM Menu WHERE ItemID=$option;");
//if (!$rs->EOF)
//{
  // $ItemID=$rs->Fields("ItemID");
  // $ItemDesc=$rs->Fields("ItemDesc");
  // print("$ItemID");
  // print("$ItemDesc");
//}


$rs->Close();


?>
</body>

</html>

我的数据库信息: 数据库名称:菜单 表:只有一个,名为Menu 字段:ItemID(PK,自动编号),ItemDesc(文本),Price(货币)

My DB information: Database Name: Menu Table: only one , named Menu Field: ItemID(PK, AutoNumber), ItemDesc(Text), Price(Currancy)

有帮助吗?谢谢

推荐答案

添加代码:

$conn->SetFetchMode(ADODB_FETCH_ASSOC);  
$rs=$conn->Execute("SELECT ItemID,ItemDesc,Price FROM Menu WHERE ItemID=$option;");
if (!$rs) {  
print $conn->ErrorMsg();  
 } else
{
 $ItemID=$rs->Fields['ItemID'];
 $ItemDesc=$rs->Fields['ItemDesc'];
 print("$ItemID");
 print("$ItemDesc");
}

这篇关于根据用户输入从数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆