gpt4 book ai didi

php - 将多行插入从while循环获取的mysql

转载 作者:可可西里 更新时间:2023-11-01 08:47:52 26 4
gpt4 key购买 nike

我是 stackoverflow 的新手,对这个问题的标题有点困惑,但让我解释一下我的问题是什么。

我正在开发调查系统,我可以选择让管理员从后端输入问题和答案,所以在前端我使用表单中的 while 循环显示问题和答案。

我的问题是如何编写代码在mysql中输入多个值(客户填写的调查)。我在这里附上我的代码。

$(function() {
$('form').bind('submit', function(){
$.ajax({
type: 'post',
url: "/prs/pageCode.php",
data: $("form").serialize(),
success: function() {
alert("Data Saved, Press Next!");
}
});
return false;
});
});

表格如下

<form name="form" id="form" class="form-horizontal" action="#"
method="POST">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Name</label>
<div class="controls">
<input name="pname" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input name="pemail" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label>We would like to post your comments to internet rating sites,
while we may use your name or an alias name. Please select your
desire:</label>

</div>
<div class="control-group">
<label class="control-label" for="focusedInput"></label>
<div class="controls">
<p>
<label> <input type="radio" name="choice" value="yes" id="choice_0">
Yes, use my name as entered above
</label> <label> <input type="radio" name="choice" value="no"
id="choice_1" onChange="enabletextbox();"> Use the alias name, as
I enter below
</label> <input id="alias" name="alias" type="text"
class="input-xlarge focused">
</p>
</div>

</div>
</fieldset>
</div>


<div class="tab-pane" id="tab2">

<fieldset>
<?php
$i=1;
while($row=mysqli_fetch_array($questions)){
?>
<div class="control-group">

<label class="control-label" for="focusedInput">(<?php echo $i;?>) <?php
$questionid = $row['question_id'];
$question = $row['question'];
echo $row['question']; ?></label>
<div class="controls">
<?php
if($row['answer_type']=="Ratings") {
echo "
<p>

Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High

</p>
";
}
else if ($row['answer_type']=="Comments") {
echo "<textarea name='answer' cols='' rows=''></textarea>";
}
$i++;
echo "<br />";

$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$surveyid,$questionid,'$question','$rating$i','$_POST[answer]')";


$result2 = mysqli_query($con,$query2);

if(!$result2) {
echo mysqli_error($result2);
}


?>

</div>
</div>
<?php }?>
</fieldset>

</div>



<div class="tab-pane" id="tab3">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Testimonial about
your care by <?php echo $_SESSION['doctorname'];?>
</label>
<div class="controls">
<textarea name="review" cols="5" rows="5" required></textarea>
</div>

</div>
<div class="form-actions">
<input type="submit" class="btn btn-primary"
value="Save & Press Finish"></input>
</div>
</fieldset>

页面代码.php

<?php
session_start();

require_once('config.php');

$con=mysqli_connect(HOST,USER,PASSWORD,DATABASE);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$alias = $_POST['choice'];
if($alias=="yes")
$aliasname = $_POST['pname'];
else
$aliasname = $_POST['alias'];
$sql="INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review)
VALUES
('$_SESSION[doctorid]','$_SESSION[doctorname]',$_SESSION[surveyid],'$_POST[pname]','$_POST[pemail]','$aliasname','$_POST[review]')";
$result = mysqli_query($con,$sql);

//$result2 = mysqli_query($con,$_SESSION['insertallquery']);

if(!$result) {
echo mysqli_error($result);
}

?>

我实际上想将答案插入到客户填写的数据库中。

最佳答案

您需要将答案存储在 review 表或您希望的单独表中。

但总的来说,据我所知,您有一个单独的表用于questions,其中包含question ids

因此,您需要在 review 表或单独的表中添加列,并将 question id 作为字段名称。

并将相应的答案存储在该用户的那些列中。

例如。考虑在现有的 reviews 表中添加列:

INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review,q1,q2,q3...)

根据您的问题数量依次类推。

此外,当您向 questions 表添加新问题时,您必须确保使用 ALTER TABLE 在此表中添加一列。


要在 while 循环中获取问题的答案,请创建一个数组。并使用迭代数组的 while 循环将这些值插入到 mysql 中。

while 循环中的查询将是这样的:

 INSERT INTO reviews (q.$i) VALUES ($answer[$i]) WHERE docter_id='$_SESSION[doctorid]'

关于php - 将多行插入从while循环获取的mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22905937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com