繁体   English   中英

从同一HTML表单向数据库中插入多行

[英]Inserting multiple rows into the database from the same html form

我想以不同的方式(即多个条目)插入数据库,但是输入取自相同的形式。

我有一个叫做questionForm的表格。 它是静态形式,具有用于插入问题的文本字段相应的文本字段,例如为该问题分配标记 ,该问题的复杂性级别 ,该问题中存在的tag1到tag8 (该问题中的关键字)等。

现在,我希望在提交表单时将这些问题中的每一个插入我的数据库中。 但是在将表单中输入的每个问题的标签与数据库中每个问题的标签进行比较之前,以确保没有重复 我该怎么做呢。

我知道可以通过简单的形式将一个条目插入数据库。 有人可以帮我吗。

我现在编码已经快到一半了。 我现在可以将条目插入表单,但是不能正确检查重复项。 这是我的问题表格和php文件-

问题表格测试任务

输入主题详细信息。

月份和年份:学期:主题:分支机构:

include('connectionfile.php');

$cnt = count($_POST['field1']);
$my= $_POST['my'];
$sem= $_POST['sem'];
$subj= $_POST['subj'];
$branch= $_POST['branch'];

if ($cnt > 0) {

        for ($i=0; $i<$cnt; $i++) 
    {

    $t1 = $_POST['field5'][$i]; 
    $t2 = $_POST['field6'][$i];
    $t3 = $_POST['field7'][$i];
    $t4 = $_POST['field8'][$i];
    $t5 = $_POST['field9'][$i];

$result = "SELECT * FROM paper WHERE (((`tag1` LIKE '%".$t1."%') OR (`tag1` LIKE '%".$t2."%') OR (`tag1` LIKE '%".$t3."%') OR (`tag1` LIKE '%".$t4."%') OR (`tag1` LIKE '%".$t5."%')) AND ((`tag2` LIKE '%".$t2."%') OR (`tag2` LIKE '%".$t3."%') OR (`tag2` LIKE '%".$t4."%') OR (`tag2` LIKE '%".$t5."%')) AND ((`tag3` LIKE '%".$t3."%') OR (`tag3` LIKE '%".$t4."%') OR (`tag3` LIKE '%".$t5."%')) AND ((`tag4` LIKE '%".$t4."%') OR (`tag4` LIKE '%".$t5."%')) AND ((`tag5` LIKE '%".$t5."%')) ) ;" ; // which checks if the tags in that question match with the tags of any other question in the db.

$sql= mysql_query($result) OR die(mysql_error()) ;

$duplicates = mysql_num_rows($sql);


if( $duplicates > 0)
echo "No entry entered for Entry #$i since it may lead to duplicates."; // no entry is inserted in insertArr[] if it leads to duplication

else
  $insertArr[] = "('" .$_POST['field1'][$i]. "', '" .$_POST['field2'][$i]. "', '" .$_POST['field3'][$i]. "', '" .$_POST['field4'][$i]. "', '" .$_POST['field5'][$i]. "', '" .$_POST['field6'][$i]. "', '" .$_POST['field7'][$i]. "', '" .$_POST['field8'][$i]. "', '" .$_POST['field9'][$i]. "', '".$my."', '".$sem."', '".$subj."', '".$branch."')";
        }

$query1 = "INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES " . implode(", ", $insertArr);

mysql_query($query1) or trigger_error("Insert failed: " . mysql_error());


    }

echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");

mysql_close($id_link);
?> 

quest.php

 include('connectionfile.php'); $cnt = count($_POST['field1']); $my= $_POST['my']; $sem= $_POST['sem']; $subj= $_POST['subj']; $branch= $_POST['branch']; if ($cnt > 0) { for ($i=0; $i<$cnt; $i++) { $t1 = $_POST['field5'][$i]; $t2 = $_POST['field6'][$i]; $t3 = $_POST['field7'][$i]; $t4 = $_POST['field8'][$i]; $t5 = $_POST['field9'][$i]; $result = "SELECT * FROM paper WHERE (((`tag1` LIKE '%".$t1."%') OR (`tag1` LIKE '%".$t2."%') OR (`tag1` LIKE '%".$t3."%') OR (`tag1` LIKE '%".$t4."%') OR (`tag1` LIKE '%".$t5."%')) AND ((`tag2` LIKE '%".$t2."%') OR (`tag2` LIKE '%".$t3."%') OR (`tag2` LIKE '%".$t4."%') OR (`tag2` LIKE '%".$t5."%')) AND ((`tag3` LIKE '%".$t3."%') OR (`tag3` LIKE '%".$t4."%') OR (`tag3` LIKE '%".$t5."%')) AND ((`tag4` LIKE '%".$t4."%') OR (`tag4` LIKE '%".$t5."%')) AND ((`tag5` LIKE '%".$t5."%')) ) ;" ; // which checks if the tags in that question match with the tags of any other question in the db. $sql= mysql_query($result) OR die(mysql_error()) ; $duplicates = mysql_num_rows($sql); if( $duplicates > 0) echo "No entry entered for Entry #$i since it may lead to duplicates."; // no entry is inserted in insertArr[] if it leads to duplication else $insertArr[] = "('" .$_POST['field1'][$i]. "', '" .$_POST['field2'][$i]. "', '" .$_POST['field3'][$i]. "', '" .$_POST['field4'][$i]. "', '" .$_POST['field5'][$i]. "', '" .$_POST['field6'][$i]. "', '" .$_POST['field7'][$i]. "', '" .$_POST['field8'][$i]. "', '" .$_POST['field9'][$i]. "', '".$my."', '".$sem."', '".$subj."', '".$branch."')"; } $query1 = "INSERT INTO paper (question, marks_allotted, complexity, chp_no, tag1, tag2, tag3, tag4, tag5, monthandyear, semester, subject_name, branch_name) VALUES " . implode(", ", $insertArr); mysql_query($query1) or trigger_error("Insert failed: " . mysql_error()); } echo("<pre>\\n"); print_r($_POST); echo("</pre>\\n"); mysql_close($id_link); ?> 

在运行时, 尽管我输入的问题与db中的现有标签没有匹配的标签,但还是给了我以下内容:没有为条目#0输入任何条目,因为它可能导致重复。 没有为条目1输入任何条目,因为它可能导致重复。 注意:未定义的变量:第41行的insertArr

警告:implode()[function.implode]:第41行传递了无效的参数

注意:插入失败:SQL语法错误; 检查与您的MySQL服务器版本相对应的手册,以获取在第43行的''附近使用的正确语法

请引导我

好吧,例如

<input type="text" name="tag1" />
<input type="text" name="tag2" />

从表单发布到PHP

那么你在PHP中

$tag1 = $_POST['tag1']; $tag2 = $_POST['tag2'];
$sql = "SELECT tag1, tag2 FROM db1 WHERE tag1 = '$tag1' OR tag2 = '$tag2';";
//query the $sql value i.e. with $query = mysql_query($sql) (which is not good example since its deprecated)
//then make a check:
$num = mysql_num_rows($result);
if ($num > 0) {
echo "There are matches with one or more tags you've entered";
}
else {
mysql_query("INSERT INTO db1 (tag1, tag2) VALUES ('$tag1', '$tag2')");
echo "Tags inserted";
}

请记住,该示例非常差且设计不当,但希望您能理解。 在其他sql库中,对于找到的行有等效的方法,因此您必须选择是否存在匹配项,如果为true,则使它们更改其标记。

第一个答案中的这段代码可以做到

if ($num > 0) {

回显“存在与您输入的一个或多个标签匹配的内容”; }

如果数字大于1,则将执行此代码,并且将忽略插入块

暂无
暂无

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

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