我一直在尝试使用此代码来将两个数组插入到sql db中。数据来自从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");
}
答案 0 :(得分:0)
您在这里做的是将数组设置为数组的第一个元素。
$a = array();
$a[] = $_POST['q'];
$b = array();
$b[] =$_POST['qc'];
$e = array();
$e[] = $_POST['qid'];
$f = array();
$f[] = $_POST['qcid'];
因此,$a
将是一个长度为单个元素,然后是一个等于$_POST['q']
的数组
我同意上面关于所使用的代码等的评论,但坚持使用上面的代码而不是上面的代码会得到一个loopable数组......
$a = (array)$_POST['q'];
$b = (array)$_POST['qc'];
$e = (array)$_POST['qid'];
$f = (array)$_POST['qcid'];