如何在php mysql中将多行数据插入多行

时间:2015-06-18 04:15:51

标签: php mysql

我已从表中检索数据,并将所有检索到的数据存储在每行的另一个表中。我尝试了以下代码,但它只插入“

$roll_no = $_POST['roll_no'];
$name = $_POST['name'];
$class = $_POST['class'];
$section =  $_POST['section'];
$m_am = $_POST['m_am'];
$a_pm = $_POST['a_pm'];
$date = $_POST['date'];
echo $a_pm .'<br>'.$m_am.'<br>'.$roll_no;

/*$sql_2 = mysql_query("INSERT INTO stud_class_attendance (`sca_rollno`, `sca_name`, `sca_class`, `sca_section`,`sca_am`, `sca_pm`,                     
?>"

3 个答案:

答案 0 :(得分:0)

使用mysqli代替mysql来防止黑客攻击 并验证用户输入使用htmlentities()htmlspecialchars()

<?php
$roll_no = htmlspecialchars($_POST['roll_no']);
$name = htmlspecialchars($_POST['name']);
$class = htmlspecialchars($_POST['class']);
$section =  htmlspecialchars($_POST['section']);
$m_am = htmlspecialchars($_POST['m_am']);
$a_pm = htmlspecialchars($_POST['a_pm']);
$date = htmlspecialchars($_POST['date']);
echo $a_pm .'<br>'.$m_am.'<br>'.$roll_no;

$sql_2 = mysqli_query("INSERT INTO stud_class_attendance (`sca_rollno`, `sca_name`, `sca_class`, `sca_section`,`sca_am`, `sca_pm`, `date`) 
values ('$roll_no','$name','$class','$section','$m_am','$a_pm','$date');
$sql_2->execute();
?>

答案 1 :(得分:0)

  1. 你需要一个循环......
  2. 执行您的第一个查询。从第一个查询中获取所有记录。迭代它们并在数据库中逐个插入
  3. 参见示例

    return View(countries.ToList());
    

答案 2 :(得分:0)

$sql='
    INSERT INTO `stud_class_attendance` (
        `sca_rollno`, `sca_name`,`sca_class`, `sca_section`,`sca_am`, `sca_pm`
    ) 
    SELECT rollno,name,class,section,a_am,a_pm FROM `student`
';

$sql2=mysqli_query($sql);
$sql2->execute();