MySql中把一个表的数据插入到另一个表中的SQL语句

1.如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:

INSERT INTO 目标表 SELECT * FROM 来源表;
insert into insertTest select * from insertTest2;

2.如果只希望导入指定字段,可以用这种方法:
INSERT INTO 目标表 (字段1, 字段2, ...) SELECT字段1, 字段2, ... FROM 来源表;(这里的话字段必须保持一致)

insert into insertTest2(id) select id from insertTest2;
发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

MySQL获取数据插入一个

04-09

<div class="post-text" itemprop="text"> <p>I am fetching the data from the MySQL table in a while loop, and inserting the form data into another MySQL table in action page in foreach, but I do not get the correct value of the radio button, I am attaching my code, please help.</p> <pre><code><?php $i = 1; $j = 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']; ?> <input type="hidden" name="questionid[]" value="<?php echo $questionid; ?>" /> <input type="hidden" name="question[]" value="<?php echo $question; ?>" /> <?php 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> "; $i++; } else if ($row['answer_type'] == "Comments") { echo "<textarea name='answer[]' cols='' rows=''></textarea>"; $j++; } echo "<br />"; ?> </div> </div> <?php } ?> </code></pre> <p>Action File Code</p> <pre><code>foreach($_POST['questionid'] as $key=>$questionid){ $questionid = $_POST['questionid'][$key]; $answer = $_POST['answer'][$key]; $result3 = mysqli_query($con, "select question,answer_type from questions where question_id=$questionid;"); while($row = mysqli_fetch_array($result3)) { $question = $row['question']; $answer_type = $row['answer_type']; if($answer_type == "Comments") { $query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_freeresponse) values(1,$_SESSION[surveyid],$questionid,'$question','$answer')"; $result2 = mysqli_query($con,$query2); if(!$result2) { echo mysqli_error($result2); } } else if($answer_type == "Ratings") { $query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating) values(1,$_SESSION[surveyid],$questionid,'$question',$key)"; $result2 = mysqli_query($con,$query2); if(!$result2) { echo mysqli_error($result2); } } } $i++; } </code></pre> <p>Output</p> <p><img src="https://i.stack.imgur.com/9Uoxt.png" alt="enter image description here"></p> <p>I want to store the ratings displayed as a radio button, I guess its taking the counter incremented as variable $key, I don't know how to store the values of the radio button.</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览