我正在尝试在php上建立一个先例报告网站, 我已经建立了一个表格,可以生成所选月份的21日到下个月的20日之间的日期。数据输出为一个表,其行数等于该月的天数。每行包含8个参数。
问题是我无法将数据插入数据库。
我创建了一个包含所有参数值的数组,并尝试循环mysql查询。
这是我编写的用于插入数据的类 `
public function Table($userid, $rdate, $rday, $rpresence, $rway, $rkm, $rexpence, $rfile, $report){
$query = $this->dbh->prepare(\'INSERT INTO time (rdate, rday, rway, rkm, rexpence, rfile, rprecence, report) VALUES'
for ($i = 1; $i <= $countdate; $i++) {
$query.=($_POST['rdate'][$i]
.', '.$_POST['rday'][$i]
.', '.$_POST['rway'][$i]
.', '.$_POST['rkm'][$i]
.', '.$_POST['rexpence'][$i]
.', '.$_POST['rfile'][$i]
.', '.$_POST['rpresence'][$i]
.', '.$_POST['report'])\')';
if ($i>$countdate) {
$query .= ',';
}
}
}
`
这是表格代码
<?php
//require_once 'inc.php';
?>
<?php
if(isset($_POST['submit'])){
$month = $_POST['month'];
$year = $_POST['year'];
}
if(isset($_POST['adken'])){
$month = $_POST['month'];
$year = $_POST['year'];
$userid = $_SESSION['id'];
$report = $_POST['month']['year'];
$rdate = $_POST['rdate'][$i];
$rday = $_POST['rday'][$i];
$rway = $_POST['rway'][$i];
$rkm = $_POST['rkm'][$i];
$rexpence = $_POST['rexpence'][$i];
$rfile = $_FILES['rfile']['name'][$i];
$rpresence = $_POST['rpresence'][$i];
echo var_dump($_POST);
$update = new User();
$update->Table($rdate, $rday, $rpresence, $rway, $rkm, $rexpence, $rfile, $report);
}
$this_year = date( "Y", strtotime($year)); //
$next_year = date( "Y", strtotime($year." +1 year")); //
$this_month = date( "m", strtotime($month)); //
$next_month = date( "m", strtotime($month." +1 month")); //
$previous_month = date('m')-1; //
$this_day = date('D'); //
$num_days = cal_days_in_month(CAL_GREGORIAN, $this_month, $this_year); //כ
$jan = date( "m", strtotime('January'));
?>
<html>
<head>
</head>
<body>
<form name="adken" action="" method="POST" enctype="multipart/form-data" >
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>date</th>
<th>day</th>
<th>presence</th>
<th>way</th>
<th>km</th>
<th>expence</th>
<th>add file</th>
</tr>
</thead>
<tbody>
<tr>
<?php// echo $this_month;?><br />
<?php// echo $next_month;?><br />
<?php// echo $_SESSION['id'];?>
<?php
if($month == 'December'){
for ($i=21; $i<=$num_days ; $i++)
echo'
<td><input type="text" name="rdate['.$i.']" class="readonly" value="'.$i.'/'.$this_month.'/'.$this_year.'" readonly></td>
<td><input type="text" name="rday['.$i.']" class="readonly" value="'.date('D').' " readonly></td>
<td><input name="rpresence['.$i.']" type="checkbox" value="1"/></td>
<td><textarea name="rway['.$i.']" cols="20" rows="2"></textarea></td>
<td width="10%"><textarea name="rkm['.$i.']" cols="3" rows="2"></textarea></td>
<td><textarea name="rexpence['.$i.']" cols="20" rows="2"></textarea></td>
<td><input class="form-control" type="file" name="rfile['.$i.']" id="file" value="בחר קובץ"></td>
<td class="hidden"><input type="text" name="report" value="'.$year.' '.$month.'" readonly></td>
</tr>';
for ($i=1; $i<=20 ; $i++)
echo '
<td><input type="text" name="rdate['.$i.']" class="readonly" value="'.$i.'/'.$jan.'/'.$next_year.'" readonly></td>
<td><input type="text" name="rday['.$i.']" class="readonly" value="'.date('D').'" readonly> </td>
<td><input name="rpresence['.$i.']" type="checkbox" value="1"/></td>
<td><textarea name="rway['.$i.']" cols="20" rows="2"></textarea></td>
<td width="10%"><textarea name="rkm['.$i.']" cols="3" rows="2"></textarea></td>
<td><textarea name="rexpence['.$i.']" cols="20" rows="2"></textarea></td>
<td><input class="form-control" type="file" name="rfile['.$i.']" id="file" value="בחר קובץ"></td>
<td class="hidden"><input type="text" name="report" value="'.$year.' '.$month.'" readonly></td>
</tr>';
}else{
for ($i=21; $i<=$num_days ; $i++)
echo '
<td><input type="text" name="rdate['.$i.']" class="readonly" value="'.$i.'/'.$this_month.'/'.$this_year.'" readonly></td>
<td><input type="text" name="rday['.$i.']" class="readonly" value="'.date('D').'" readonly> </td>
<td><input name="rpresence['.$i.']" type="checkbox" value="1"/></td>
<td><textarea name="rway['.$i.']" cols="20" rows="2"></textarea></td>
<td width="10%"><textarea name="rkm['.$i.']" cols="3" rows="2"></textarea></td>
<td><textarea name="rexpence['.$i.']" cols="20" rows="2"></textarea></td>
<td><input class="form-control" type="file" name="rfile['.$i.']" id="file" value="בחר קובץ"></td>
<td class="hidden"><input type="text" name="report" value="'.$year.' '.$month.'" readonly></td>
</tr>';
for ($i=1; $i<=20 ; $i++)
echo '
<td><input type="text" name="rdate['.$i.']" class="readonly" value="'.$i.'/'.$next_month.'/'.$this_year.'" readonly></td>
<td><input type="text" name="rday['.$i.']" class="readonly" value="'.date('D').' " readonly></td>
<td><input name="rpresence['.$i.']" type="checkbox" value="1"/></td>
<td><textarea name="rway['.$i.']" cols="20" rows="2"></textarea></td>
<td width="10%"><textarea name="rkm['.$i.']" cols="3" rows="2"></textarea></td>
<td><textarea name="rexpence['.$i.']" cols="20" rows="2"></textarea></td>
<td><input class="form-control" type="file" name="rfile['.$i.']" id="file" value="בחר קובץ"></td>
<td class="hidden"><input type="text" name="report" value="'.$year.' '.$month.'" readonly></td>
</tr>';
} ?>
</tbody>
</table>
<p>
<input name="password" type="text" value="Password" />
<br />
<input type="submit" name="send" value="Send" />
<input type="submit" name="adken" value="Update" />
</p>
</form>
</body>
</html>
这是我的数据库
- Database: `mop`
-- Table structure for table `time`
--
CREATE TABLE IF NOT EXISTS `time` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) DEFAULT NULL,
`rdate` date DEFAULT NULL,
`rday` varchar(10) DEFAULT NULL,
`rpresence` int(1) DEFAULT NULL,
`rway` longtext,
`rkm` int(10) DEFAULT NULL,
`rexpence` int(255) DEFAULT NULL,
`rfile` mediumtext,
`report` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Constraints for dumped tables
-- Constraints for table `time`
--
ALTER TABLE `time`
ADD CONSTRAINT `time_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE CASCADE;
请帮助,即使是最小的帮助也会很棒!
我在过去的五天里坚持这一点。
插入数据的其他建议会很棒。
感谢名单 戴夫
POST UPDATE
这是应将所有数据插入数据库的当前Table
方法。
public function Table($rdate, $rday, $rpresence, $rway, $rkm, $rexpence, $rfile, $report){
$query = "INSERT INTO time (rdate, rday, rway, rkm, rexpence, rfile, rpresence, report) VALUES ";
$values = "";
foreach($rdate AS $key => $value) {
if(strlen($values) > 0) $values .= ", ";
$values .= "('".$value."', '".$rdate[$key]."', '".$rday[$key]."', '".$rway[$key]."', ".$rkm[$key].", ".$rexpence[$key].", '".$rfile[$key]."', ".$rpresence[$key].", '".$report[$key]."')";
}
$query .= $values;
try {
$query = $this->dbh->prepare($query);
return $query->execute();
} catch(PDOException $e) {
return $e;
}
}
当我运行脚本时,我收到以下错误:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\mop\inc\userclass.php on line 128
。
第128行是foreach($rdate AS $key => $value) {
答案 0 :(得分:0)
试试这个:
public function Table($userid, $rdate, $rday, $rpresence, $rway, $rkm, $rexpence, $rfile, $report) {
$query = "INSERT INTO time (userid,rdate, rday, rway, rkm, rexpence, rfile, rpresence, report) VALUES ";
$values = "";
foreach($rdate AS $key => $value) {
if(strlen($values) > 0) $values .= ", ";
$values .= "(".$userid.",'".$value."', '".$rdate[$key]."', '".$rday[$key]."', ".$rkm[$key].", ".$rexpence[$key].", '".$rfile[$key]['name']."', ".$rpresence[$key].", '".$report[$key]."')";
}
$query .= $values;
try {
$query = $dbh->query($query);
return $query->execute();
} catch(PDOException $e) {
return $e;
}
}
另一个建议是,在实际为数据添加文件的路径之前,应该执行move_uploaded_file(将它们放在稍后可供您访问的文件夹中,以便在您的应用程序中使用,例如/文件/ file_name.extension)。
还有一件事,变量$ userid在你的函数中没有用(插入内部没有使用该值)所以我也没有使用它。
另外请确保更改以下内容:
if(isset($_POST['adken'])){
$month = $_POST['month'];
$year = $_POST['year'];
$userid = $_SESSION['id'];
$report = $_POST['month']['year'];
function formatter_date($date) {
return date('Y-m-d', strtotime($date));
}
$rdate = array_map("formatter",$_POST['rdate']);
$rday = $_POST['rday'];
$rway = $_POST['rway'];
$rkm = $_POST['rkm'];
$rexpence = $_POST['rexpence'];
$rfile = $_FILES['rfile'];
$rpresence = $_POST['rpresence'];
$update = new User();
$update->Table($rdate, $rday, $rpresence, $rway, $rkm, $rexpence, $rfile, $report);
}
答案 1 :(得分:0)
最后,我得到了一个问题的答案。 请看一下:
这是表格方法:
public function Table($userid, $rdate, $rday, $rway, $rkm, $rexpence, $rfile, $rpresence, $report){
$query = "INSERT INTO time (userid, rdate, rday, rway, rkm, rexpence, rfile, rpresence, report) VALUES ";
$values = "";
foreach($rdate as $key => $value) {
if(strlen($values) > 0) $values .= ", ";
$values .= "('".$userid."', '".$rdate[$key]."', '".$rday[$key]."', '".$rway[$key]."', '".$rkm[$key]."', '".$rexpence[$key]."', '".$rfile[$key]['name']."', '".$rpresence[$key]."', '".$report."')";
}
$query .= $values;
try {
$a = $this->dbh->prepare($query);
return $a->execute();
} catch(PDOException $e) {
return $e;
}
}
我有很多问题,我不知道从哪里开始...
其他一切都保持不变。 感谢所有帮助过的人