我对PHP完全陌生,这是我的第一个PHP项目。我正在做一个学位规划,我有一个选修课程的表,其中有多个行为单个用户。
我有以下php代码<?php
include("config.php");
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
session_start();
$user_id = $_SESSION['user_id'];
$query_select_elective="SELECT * FROM course_elective WHERE user_id=$user_id";
$result_course_elective= mysql_query($query_select_elective,$db_server);
if (!$result_course_elective) die("Database access failed: " . mysql_error());
$i=0;
while($row_elective = mysql_fetch_array($result_course_elective))
{
$SubjectCode[$i]['elective_paper_name']=$row_elective[1];
$SubjectCode[$i]['elective_paper_number']=$row_elective[2];
$SubjectCode[$i]['elective_paper_grade']=$row_elective[3];
$SubjectCode[$i]['elective_paper_sem']=$row_elective[4];
$SubjectCode[$i]['elective_paper_transfer']=$row_elective[5];
$i++;
}
echo $SubjectCode[0]['elective_paper_name'];//I get the name of the elective paper fine at this line
echo <<<_END
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Degree Plan</title>
<link rel="stylesheet" type="text/css" href="Assignment1.css" />
</head>
<body>
<div class="container">
<section id="content">
<form name="degreeplan" method="POST" action="degree_plan_data_science.php">
<section id="Table">
<table class="course_table " style="width:90%" align="center">
<tr class="course_table">
<td class="course_table">1</td>
<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="elective_paper1" value="$SubjectCode[0]['elective_paper_name']" /></td>
<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="elcetive_paper1_number"/></td>
<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="sem_elective_paper1"/></td>
<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="transfer_elective_paper1"/></td>
<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="grade_elective_paper1"/></td>
</tr>
当我回显$SubjectCode[0]['elective_paper_name']
时,我得到了正确的OOPS。
然而,当我想在输入中或换句话说,在我想要"$SubjectCode[0]['elective_paper_name']"
选修论文名称的列中获得相同的值时,我得到的是Array['elective_paper_name']
而不是论文名称。
请帮助。
当涉及到数组时,插值有点棘手。当数组中有对应键的字符串时,可以省略键周围的引号。
value="$SubjectCode[0][elective_paper_name]"
不过,还有另一种方法——这种方法实际上更适合多维数组——可以使用:将它用花括号括起来。
value="{$SubjectCode[0]['elective_paper_name']}"
尝试将值替换为:
value="<?=$SubjectCode[0]['elective_paper_name']?>"
在数组周围使用括号,它应该可以正常工作。
改变<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="elective_paper1" value="$SubjectCode[0]['elective_paper_name']" /></td>
<td class="course_table"><input type="text" style="border:none;background:inherit;width:100%" name="elective_paper1" value="{$SubjectCode[0]['elective_paper_name']}" /></td>