我怎样才能把school_year的id用来?

时间:2016-01-08 08:40:34

标签: php

我的问题是当我像这样保存student_votes时: enter image description here

我想要保存,而不是一年将保存我想保存ID。例如,如果今天的年份是2016年那么它将节省2000(因为它是2015年的ID)。第一张图片是studentvotes表, 它应该像syearid = 2000,如果年份是2015年,或者2016年将是syearid = 2001

这是我的代码:

<?php
require_once('auth.php');
include('connection/connect.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');
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]));
    //I think because of this code
    $sqlas = "INSERT INTO studentvotes(candid,idno,syearid) VALUES (:m,:n,:o)";
    $qs = $db->prepare($sqlas);
    $qs->execute(array(':m'=>$edittable[$i],':n'=>$idnum,':o'=>$YearNow));

}

header("location: notification.php?". http_build_query($query));
mysql_close($con);
?>

需要帮助的人!感谢

1 个答案:

答案 0 :(得分:0)

我终于得到了你的问题:

后:

$qa->execute(array($stat,$idnum));

这样的东西:(我不确定你使用的是什么数据库层,这是mysqli的例子)

/** @var mysqli $con */
$years = array();
$data = $con->query('SELECT syearid, from_year FROM school_year')->fetch_all(MYSQLI_ASSOC);
foreach($data as $row)
  $years[$row['syearid']] = $row['from_year'];

并更新您的

$qs->execute(array(':m'=>$edittable[$i],':n'=>$idnum,':o'=>$YearNow));

$qs->execute(array(':m'=>$edittable[$i],':n'=>$idnum,':o'=>$years[$YearNow]));

您还可以在操作后查询:

UPDATE studentvotes AS sv
  JOIN school_year AS sy ON sv.syearid = sy.syearid
  SET sv.syearid = sy.from_year

和btw,请将这些准备工作放在for循环之上,准备报表的主要目的是效率

$q = $db->prepare("UPDATE candidates,student,school_year 
    SET votes=votes+?
    WHERE candid =? AND school_year.syearid = candidates.syearid AND school_year.from_year like $YearNow");

$qs = $db->prepare("INSERT INTO studentvotes(candid,idno,syearid) VALUES (:m,:n,:o)");

for($i=0; $i < $N; $i++)
{
   $q->execute(array($a, $edittable[$i]));
   $qs->execute(array(':m'=>$edittable[$i],':n'=>$idnum,':o'=>$YearNow));
}