sample我在放入school_year(syearid)时遇到了问题。我必须把年份自动放在vote.php中怎么做?我这样做是对的吗?有一张图片我的数据结构。看起来像
这是我的代码:
$idnum=$_POST['idnum'];
$stat='voted';
$sqla = "UPDATE student
SET status=?
WHERE idno=?";
$qa = $db->prepare($sqla);
$qa->execute(array($stat,$idnum));
$edittable=$_POST['votes'];
$a=1;
$N = count($edittable);
$YearNow=Date('Y');
//making an option
echo '<label class="control-label col-md-7 col-sm-3 col-xs-12">School Year</label>';
echo '<select name="syearid" class="select2_group form-control" style="text-align:center;" >';
echo'<optgroup label="School Year" style="text-align:center;">';
echo '<?php';
date("Y");
include('../connection/connect.php');
$result = $db->prepare("SELECT * FROM school_year");
$result->bindParam(':syearid', $res);
$result->execute();
for ($i=0; $row = $result->fetch(); $i++) {
$isSelected = ((date("Y") == $row['from_year']) ? " selected" : "");
echo "<option value='".$row['syearid']."'$isSelected>".$row['from_year'].'-'.$row['to_year']."</option>";
echo'?>';
echo'</optgroup>';
echo'</select>';
for($i=0; $i < $N; $i++)
{
$sql = "UPDATE candidates,student,school_year
SET votes=votes+?
WHERE candid =? AND school_year.syearid = candidates.syearid AND school_year.from_year like $YearNow ";
$q = $db->prepare($sql);
$q->execute(array($a,$edittable[$i]));
$sqlas = "INSERT INTO studentvotes(candid,idno,syearid) VALUES (:m,:n,:o)";
$qs = $db->prepare($sqlas);
//this is where my problem is how to put the selected year in another mysql?
$qs->execute(array(':m'=>$edittable[$i],':n'=>$idnum,':o'=>$isSelected));
}
}
header("location: notification.php?". http_build_query($query));
mysql_close($con);
?>
答案 0 :(得分:0)
您正在尝试在syearid
字段中插入字符串(“已选中”或空字符串)。
...
$isSelected = ((date("Y") == $row['from_year']) ? " selected" : "");
...
$sqlas = "INSERT INTO studentvotes(candid,idno,syearid) VALUES (:m,:n,:o)";
...
$qs->execute(array(':m'=>$edittable[$i],':n'=>$idnum,':o'=>$isSelected));
我想,传递的值应该是INT
或DATE
的类型(取决于字段类型)