简体   繁体   English

为php中的一件事添加多条记录

[英]Adding multiple records for one thing in php

I currently have a table called paper , which holds all the information for each paper that a user uploads to my system.我目前有一个名为paper的表,其中包含用户上传到我的系统的每篇论文的所有信息。 I also have a table called paper_topics ;我还有一个名为paper_topics的表; this is meant to hold the paper_id and its topic_id from a table called topic .这是为了从名为topic的表中保存paper_id及其topic_id However I'm not sure how I can use PHP to allow the user to select multiple topics and then submit them along with the paper_id to the paper_topics table.但是我不确定如何使用 PHP 允许用户访问 select 多个主题,然后将它们与paper_id一起提交到paper_topics表。

Here is the code I have for uploading the paper.这是我上传论文的代码。

if(!is_dir("paper")) {
mkdir("paper");

}
function savedata(){
    global $_FILES, $_POST, $putItAt;
    $sql = "INSERT INTO `internetcoursework`.`paper` (
    `paper_id`, 
    `username`,
    `title`, 
    `abstract`, 
    `filelocation`, 
    `date_added`) 
    VALUES (NULL,'".mysql_real_escape_string($_POST['username'])."' , '".mysql_real_escape_string($_POST['title'])."',
    '".mysql_real_escape_string($_POST['abstract'])."', '".mysql_real_escape_string($putItAt)."', CURDATE());";
    mysql_query($sql);

    }
$putItAt = "paper/".basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FIleS['uploadedfile']['tmp_name'],$putItAt)) {
savedata();
header("location: listfiles.php");
echo "you have succesfully uploaded";

}else { 
if(copy($_FILES['uploadedfile']['tmp_name'],$putItAt)) {
savedata();
    header("location: listfiles.php");
    } else {
        echo "you totally failed";

        }
        }


?>
'

If I understand your request correctly ("select multiple topics and then submit them along with the paper_id to the paper_topics table") the basic idea is to add a multi select form element to your form (like this http://onl.netools.org/tricks/using_multiple_select.php ), then use that posted value to insert rows into the paper_topics table.如果我正确理解您的请求(“选择多个主题,然后将它们与 paper_id 一起提交到 paper_topics 表”),基本思想是向您的表单添加一个多 select 表单元素(例如http://onl.netools。 org/tricks/using_multiple_select.php ),然后使用发布的值将行插入到 paper_topics 表中。 You'll need to insert the paper and get the paper id first ( http://php.net/manual/en/function.mysql-insert-id.php )您需要先插入论文并获取论文 id ( http://php.net/manual/en/function.mysql-insert-id.php )

Does that answer your question?这是否回答你的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM