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