使用 PHP 将 JSON 数据填充到文本区域中
如果有人能帮助我,那就太好了。我遇到的问题是,我无法使用 PHP 创建 JSON 对象,其中数据是从 MySQL 表中获取的。“角色”表有一个主键“role_id”。这个字段是下拉框中的一个选项。一旦用户选择其中一个值,按下“选择角色”按钮,与该值相关的数据(例如背景、资格)需要填充到下面的文本区域中。目前,一旦按钮被选中按下后,每个文本区域中将显示“未定义”一词,而不是所需的数据。
我遇到了什么问题?非常感谢您的帮助。
编辑 - 数据现在填充在文本区域中,但在“knowledge_and_skills”和“prof_dev_activities”中存在未定义的错误。此外,现在仅填充“角色”表中的第一条记录 - 因此在下拉框中选择值并更改值时对于“role_id”-什么也没有发生。
<?php
header("Content-type: application/json");
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="bcs_test"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query("SELECT * FROM role");
$obj = mysql_fetch_object($result);
$json = json_encode($obj);
echo ($json);
?>
CREATE TABLE role (
role_id INTEGER(5) UNSIGNED NOT NULL AUTO_INCREMENT,
background VARCHAR(5000) NULL,
tasks VARCHAR(5000) NULL,
knowledge VARCHAR(5000) NULL,
training VARCHAR(5000) NULL,
professional VARCHAR(5000) NULL,
qualifications VARCHAR(5000) NULL,
PRIMARY KEY(role_id)
)
It would great if somebody can help me.The problem I have is that I'm unable to create JSON objects with the use of PHP where data is taken from MySQL table.'Role' table has a primary key 'role_id'.This field is an option in the dropdown box.Once the user selects one of the values,presses 'Select Role' button, data related to this value such as background,qualifications needs to be populated in textareas below.At the moment,once the button is pressed, word 'undefined' is displayed in each textarea instead of required data.
What is the problem I'm having? Many thanks for your help.
EDIT - data in now populated in textareas but with undefined errors in 'knowledge_and_skills' and 'prof_dev_activities'.Also,it is now only populated for the first record in the 'role' table - so when selecting values in the dropdown box and changing values for 'role_id' - nothing happens.
<?php
header("Content-type: application/json");
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="bcs_test"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query("SELECT * FROM role");
$obj = mysql_fetch_object($result);
$json = json_encode($obj);
echo ($json);
?>
CREATE TABLE role (
role_id INTEGER(5) UNSIGNED NOT NULL AUTO_INCREMENT,
background VARCHAR(5000) NULL,
tasks VARCHAR(5000) NULL,
knowledge VARCHAR(5000) NULL,
training VARCHAR(5000) NULL,
professional VARCHAR(5000) NULL,
qualifications VARCHAR(5000) NULL,
PRIMARY KEY(role_id)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)