简体   繁体   English

从动态表单插入多个数组

[英]Multiple Array insert from dynamic form

I have been trying to work with this code to get two arrays to insert into the sql db. 我一直在尝试使用此代码来将两个数组插入到sql db中。 The data is coming from a form that is generated from an sql table. 数据来自从sql表生成的表单。 My original intention was to update a the table where the form was generated from questions and answers. 我的初衷是更新表格,其中表格是从问题和答案中生成的。 However I beleive it would be easier to just insert them into a new table and when the info is needed I can call from that table. 但是我相信,将它们插入新表会更容易,当需要信息时我可以从该表调用。 Either way though there are two important piecesofinformationthat hae to be transfered and line up exactly. 无论哪种方式,都有两个重要的信息片段可以被转移并准确排列。 Those are the row in which the question is from and the interview id that the question is being attached to. 这些是问题所在的行和问题所附的访谈ID。 Here is my code. 这是我的代码。

 $result = mysql_query("SELECT * FROM interviews WHERE id='$int'");
                        while($row = mysql_fetch_array($result)){ $a = $row[1]; $b =   $row[2]; $c = $row[3]; $d = $row[4]; $e = $row[5]; $f = $row[7]; $g = $row[8]; $h = $row[9]; }

                    $i = array();   
                    $result = mysql_query("SELECT * FROM dbinter WHERE interview_id='$int'");
                        while($row = mysql_fetch_array($result)){ $i[] = $row[2]; }

                    $l = array();   
                    $result = mysql_query("SELECT * FROM form_custom WHERE attached='$cid' && date='$e'");
                        while($row = mysql_fetch_array($result)){ $l[] = $row[0]; }

                    echo "<font style='font-size: 45px;'>$a $b</font>";

                                    echo "<p><font style='font-size: 20px;'>$c
                                    <br />$d</font></p>";

                    echo "<form method='POST' name='interview' action='interviewsheet.php?cid=$cid&int=$int&stat=proc'>";
                    echo "<div style='margin-top:50px;'><input type='submit' value='COMPLETE INTERVIEW'></div>";

                    foreach($i as $j){
                    $result = mysql_query("SELECT * FROM form WHERE id='$j'");
                    while($row = mysql_fetch_array($result)){ $k = $row[2]; $p = $row[0];}
                    echo "
                    <div style='margin: 20px;'>
                    <font styler='font-size: large; font-weight: bold;'>$k</font><br />
                    <textarea rows='5' cols='60' style='border: none;' name='q'></textarea>
                    <input type='hidden' name='qid' $value='$p'>
                    </div>
                    ";}

                    foreach($l as $m){
                    $result = mysql_query("SELECT * FROM form_custom WHERE id='$m'");
                    while($row = mysql_fetch_array($result)){ $n = $row[2]; $q = $row[0];}
                    echo "
                    <div style='margin: 20px;'>
                    <font styler='font-size: large; font-weight: bold;'>$n</font><br />
                    <textarea rows='5' cols='60' style='border: none;' name='qc'></textarea>
                    <input type='hidden' name='qcid' $value='$q'>
                    </div>
                    ";}
                    echo "</div>";

} }

the other code for the processing page is: 处理页面的其他代码是:

    $a = array();
    $a[] = $_POST['q'];
    $b = array();
    $b[] =$_POST['qc'];
    $e = array();
    $e[] = $_POST['qid'];
    $f = array();
    $f[] = $_POST['qcid'];

    $frm = "f";
    $frmc = "fc";


    foreach($a as $key=>$value){
        $avalue = mysql_real_escape_string($value);
        $evalue = mysql_real_escape_string($e[$key]);
        $sql = "INSERT INTO answers (qid,cid,inta,frm,ans) VALUES ('$evalue','$cid','$int','$frm','$avalue')";
                if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
    }

    foreach($b as $key=>$value){
        $bvalue = mysql_real_escape_string($value);
        $fvalue = mysql_real_escape_string($f[$key]);
        $sql = "INSERT INTO answers (qid,cid,inta,frm,ans) VALUES ('$fvalue','$cid','$int','$frmc','$bvalue')";
                if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
    }





    header("Location: hr.php?act=int&stat=pend");

    }

What you're doing here is setting an array to be the first element of an array. 你在这里做的是将数组设置为数组的第一个元素。

$a = array();
$a[] = $_POST['q'];
$b = array();
$b[] =$_POST['qc'];
$e = array();
$e[] = $_POST['qid'];
$f = array();
$f[] = $_POST['qcid'];

So $a will then be a single element in length and that is then an array equal to $_POST['q'] 所以$a将是一个长度为单个元素,然后是一个等于$_POST['q']的数组

I agree with the comments above about the code used etc, but sticking this in instead of the code above will get you a loopable array... 我同意上面关于所使用的代码等的评论,但坚持这个而不是上面的代码将得到一个loopable数组...

$a = (array)$_POST['q'];
$b = (array)$_POST['qc'];
$e = (array)$_POST['qid'];
$f = (array)$_POST['qcid'];

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

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