如何把id代替年份?

时间:2016-01-12 05:52:03

标签: php mysql

每个人都请我真的需要帮助,我有一个关于如何将id放在current_year下的问题。有关图像中的示例,即我的数据结构sample image。有什么方法可以保存它的id而不是我今年使用$YearNow=Date('Y');的当前年份,如果当前年份是2015年,如果我将其提交到数据库,那么它会保存当前年份如2000

=======================

如果我的电脑年份是2016年,那么2016年= 2001年(syearid)。在我的school_year表中,我有(syearid(pri), from_year, to_year). And my studentvotes(studeid(pri)autoincremtn,candid,idno,syearid(外键)). Therefore if the year of my pc is 2016 as you can see that 2016(from_year)is under of the 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
        SET votes=votes+?
        WHERE candid =? ";
        //don't know it's correct?


 $years = array();
        $sq12="select * from studentvotes,school_year where studentvotes.syearid = school_year.syearid AND school_year.from_year = $YearNow";
        $result1= mysql_fetch_assoc($sq12);
            for($i=0; $row = $result->fetch(); $i++){
             $years[$row['syearid']] = $row['from_year'];


  } 
    $q = $db->prepare($sql);
    $q->execute(array($a,$edittable[$i]));

    //$this part will have something to update
    $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)

您正在将mysqlmysqli扩展名混合在一起,并使用错误的变量$result1。您可以使用这样来获取年ID

$sq12="select studentvotes.syearid from studentvotes,school_year where studentvotes.syearid = school_year.syearid AND school_year.from_year = $YearNow";

$result = $db->prepare($sq12);
$result->execute();
$result_final = $result->fetchAll(PDO::FETCH_COLUMN, 0);
$yearid = $result_final[0]; // get year id

现在您可以将其插入查询

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

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