drbouzlxb92333332 2016-07-27 14:28
浏览 28

遵循创建mysql的代码是多么安全 - 每年重置字段,安全性也考虑来自多个请求的输入

I HAVE tables in this test code:

table WORKER -> name varchar(50) , spec varchar(50) UNIQYE

table KEYGEN- > id INT AUTO_INCREMENT , year varchar(50)

spec should be like = 2009/54.

CODE: for checking and reseting (truncating) keygen table.

$insertedid=""; //to put last inserted id from keygen 
$spec="";   //spec number generated from year + $insertedid in table keygen
$year=date("Y");  //current year

$query="SELECT * from keygen WHERE year='{$year}' ";

    if($noviRed=$mysqli->query($query))
    {
        if($noviRed->num_rows==0)
        {
            $truncate="truncate keygen"; 
                if($mysqli->query($truncate)) {echo"<h2> TRUNCATING  TABLE OK </h2>";}  else { exit("fatal error");}

            $query2="INSERT INTO keygen(year) values('{$year}') ";
                if($mysqli->query($query2)) {$insertedid=$mysqli->insert_id;} else {exit("fatal error");}
        }
        else
        {

            $query2="INSERT INTO keygen(year) values('{$year}') ";
                if($mysqli->query($query2)) {$insertedid=$mysqli->insert_id;} else {exit("fatal error");}
        }
    }
    else {exit("fatal error");}

CODE: for inserting data into worker table.

$spec=$year."/".$insertedid; //generating spec number

if($insertedid!="" AND $spec!="")   
{
    $query3="INSERT INTO worker(name,spec) values('{$name}','{$spec}') ";   

    if($noviRed=$mysqli->query($query3)){echo"<h1>SUCCES </h1>";}
    else {exit("fatal error");}
}
  • 写回答

1条回答 默认 最新

  • doudi5892 2016-07-27 15:23
    关注

    Not entirely safe.

    If multiple requests come in at once near the beginning of a year, the TRUNCATE query may end up running twice, as the SELECT will return zero rows for both requests. This would probably result in a unique key collision on one of the subsequent inserts.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测