3

我一直在尝试使用此代码来获取两个数组以插入到 sql 数据库中。数据来自从 sql 表生成的表单。我的初衷是更新从问题和答案生成表单的表格。但是,我相信将它们插入新表会更容易,当需要信息时,我可以从该表中调用。无论哪种方式,都有两条重要的信息需要传输并准确排列。这些是问题来自的行和问题所附的采访ID。这是我的代码。

 $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>";

}

处理页面的其他代码是:

    $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");

    }
4

1 回答 1

0

您在这里所做的是将数组设置为数组的第一个元素。

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

那么$a将是长度上的单个元素,然后是一个等于的数组$_POST['q']

我同意上面关于使用的代码等的评论,但是坚持这个而不是上面的代码会给你一个可循环的数组......

$a = (array)$_POST['q'];
$b = (array)$_POST['qc'];
$e = (array)$_POST['qid'];
$f = (array)$_POST['qcid'];
于 2013-06-24T16:42:06.780 回答