我想要做的是,在用户提交他/她想要的主题后,代码访问数据库并保存与数组相关的所有主题。现在我希望它保存表格中的所有数据' class'进入数组成绩簿[]。我尝试了一个foreach循环,但它无法正常工作。我想保存成绩簿[0]中第一行的所有寄存器,然后保存成绩簿[1]中的第二个寄存器,依此类推。此外isset($_POST['subject'])
没有通过,所以它给了我这个警告:
注意:未定义的变量:C:\ xampp \ htdocs中的主题...
<?php
mysql_connect('localhost','root','root');
mysql_select_db('school');
if(isset($_POST['subject'])){
$result = mysql_query("SELECT * FROM class WHERE subject=$subject");
foreach ($result as $gradebook) {
$gradebook[]=array('id'=>$id,'subject'=>$subject,'homework'=>$homework,'dueDate'=>$dueDate);
echo "$gradebook <br>";
}
/* $table = '';
$table .= '<table border="1">';
for ($i = 0; $i < count($gradebook['subject']); $i++) {
$table .= '<tr>';
for ($j = 0; $j < count($gradebook['students']); $j++) {
$table .= '<td width="50"> </td>';
}
$table .= '</tr>';
}
$table .= '</table>';*/
}
?>
<form action="" method="post">
<table border="0" width="200">
<tr>
<td width="80"><label>Subject</label></td>
<td width="120"><input type="text" name="subject"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Create Table"></td>
</tr>
</table>
</form>
<br />
<br />
这是我的数据库
-- phpMyAdmin SQL Dump
-- version 4.1.6
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jul 25, 2014 at 05:52 AM
-- Server version: 5.6.16
-- PHP Version: 5.5.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `school`
--
-- --------------------------------------------------------
--
-- Table structure for table `class`
--
CREATE TABLE IF NOT EXISTS `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subject` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
`unit` varchar(5) COLLATE utf8_spanish_ci NOT NULL,
`homework` varchar(50) COLLATE utf8_spanish_ci NOT NULL,
`dueDate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=11 ;
--
-- Dumping data for table `class`
--
INSERT INTO `class` (`id`, `subject`, `unit`, `homework`, `dueDate`) VALUES
(1, 'Math', 'UI', 'Math work1', '2014-04-04'),
(2, 'Math', 'UI', 'Math work2', '2014-05-12'),
(3, 'Science', 'UI', 'Science work1', '2014-04-09'),
(4, 'Home Economics', 'UII', 'Home economics work1', '2014-04-16'),
(5, 'Math', 'UII', 'Math work 3', '2014-05-12'),
(6, 'Math', 'UIII', 'Math work4', '2014-05-21'),
(7, 'Science', 'UII', 'Science work2', '2014-05-14'),
(8, 'Physics', 'UI', 'Physics work1', '2014-05-06'),
(9, 'Math', 'UIII', 'Math work5', '2014-07-16'),
(10, 'Math', 'UIV', 'Math work6', '2014-07-14');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<br />
<br />
答案 0 :(得分:0)
错误可能是由于未定义的变量$subject
,因此在您的查询中请尝试以下方法:
if(isset($_POST['subject'])){
$result = mysql_query("SELECT * FROM class WHERE subject LIKE '%".$_POST['subject']."%'");
if(!$result) {
die("Database query failed: " . mysql_error());
}while ($row = mysql_fetch_array($result)) {
$gradebook[]=array('id'=>$row['id'],'subject'=>$row['subject'],'homework'=>$row['homework'],'dueDate'=>$row['dueDate']);
}
$table = '';
$table .= '<table border="1">';
foreach ($gradebook as $row)
{
$table .= '<tr>';
$table .= '<td>' . $row['id'] . '</td>';
$table .= '<td>' . $row['homework'] . '</td>';
$table .= '<td>' . $row['dueDate'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
print($table);
}
?>
<form action="" method="post">
<table border="0" width="200">
<tr>
<td width="80"><label>Subject</label></td>
<td width="120"><input type="text" name="subject"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Create Table"></td>
</tr>
</table>
</form>
<br />
<br />