【问题标题】:$mysqli-insert_id is not incrementing correctly$mysqli-insert_id 没有正确递增
【发布时间】:2015-05-06 16:57:49
【问题描述】:

我正在尝试使用 $mysqli->insert_id 重命名文件,以便我的程序可以创建多个文件。相反,它只创建一个 id 为 0 的文件,并且每次它覆盖该文件而不是创建一个新文件。我想知道是否需要增加 $mysli->insert_id 或其他内容。

但基本上我希望将每个文件命名为“job_id”.fasta。现在他们都是0.fasta。

我很困惑,因为当我将 mysqli->insert_id 用于我的插入语句时,它会正确地将 job_ids 分配给每个新作业。因此,当我从工作中选择 * 时,我会得到一个包含所有工作 1-100 的巨大列表。我希望将作业创建的文件称为 job_id 而不仅仅是 0。

这是我的代码。

<?php
if(isset($_POST['submit'])){
//      echo "submit1";
            //declare variables to what the user defines them as

            $db = $_POST['database'];
            $evalue = $_POST['evalue'];
            $sequence = $_POST['BlastSearch'];
            $hits = $_POST['hits'];


            //insert the values into the database



            //create a new .fasta file and put the sequence the user wants to search for in that file
            $file = 'uploads/'.$mysqli->insert_id.'.fasta';
            $current = $_POST['BlastSearch'];
            file_put_contents($file, $current);

            //execute the BLAST Tool
            // Do this execute statement if the user inputs his own sequence. (Use new.fasta)


?>

因此 insert_id 增加了将 id 插入数据库中的 job_id 但它不会在我的 $file = 'uploads/'.$mysqli->id 或我的 exec 函数中增加。

【问题讨论】:

  • 为什么在查询之前尝试使用$mysqli-&gt;insert_id?尝试将 //create a new .fasta file 块放在 $mysqli-&gt;query("INSERT INTO `Job`... 查询之后
  • 那是我的问题吗?
  • 好吧,既然你还没有做查询,$mysqli-&gt;insert_id会返回0。你需要在插入后使用$mysqli-&gt;insert_id
  • 警告:当使用mysqli 时,您应该使用参数化查询和bind_param 将用户数据添加到您的查询中。 请勿使用字符串插值来完成此操作,因为您将创建严重的SQL injection bugs从不$_POST数据直接放入查询中。
  • 我将插入语句放在我的文件制作之上,它仍然只返回 0。

标签: php html mysql database mysqli


【解决方案1】:

我猜你在某个地方错过了这个想法。

根据你的代码不清楚你的问题是什么。

让我解释一下为什么我无法理解您的问题。

这是您的代码的转换片段:

$mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', '" . $sequence . "', 'running' , NOW(), NOW())");
$insertedJobId = $mysqli->insert_id;
$mysqli->query("INSERT INTO `BLAST`(`db_name`, `evalue`, `job_id`) VALUES ('" . $db . "','" . $evalue . "', '".$insertedJobId."')") or die(mysqli_error($mysqli));
$insertedBlastId = $mysqli->insert_id; 

//execute the BLAST Tool
// Do this execute statement if the user inputs his own sequence. (Use new.fasta)

exec('/students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/public_html/home/uploads/'.$insertedBlastId.'.fasta -m'.$evalue.' -o outputSEQ -v'.$hits.' -b'.$hits);

那么哪个insert_id id 没有增加? $insertedJobId$insertedBlastId ?

【讨论】:

  • 我会尝试一秒钟。但 Blast 表外键是 job_id
猜你喜欢
  • 2011-08-16
  • 2011-07-27
  • 2017-08-28
  • 1970-01-01
  • 2021-04-13
  • 2015-01-10
  • 2023-03-15
  • 1970-01-01
  • 2013-07-25
相关资源
最近更新 更多