以混合顺序在表中插入值

时间:2018-09-29 15:32:03

标签: php sql

我想按混合顺序在表中插入值。我有这段代码,但是有些不起作用!

<?php
session_start();
$name = $_SESSION['A_User'];
$ans=array("VALUES (1),","VALUES (2),","VALUES (3),","VALUES (4),","VALUES 
(5),","VALUES (6),","VALUES (7),","VALUES (8),","VALUES (9),","VALUES 
(10),","VALUES (11),","VALUES (12),","VALUES (13),","VALUES (14),","VALUES 
(15),","VALUES (16),","VALUES (17),","VALUES (18),","VALUES (19),","VALUES 
(20),","VALUES (21),","VALUES (22),","VALUES (23),","VALUES (24),","VALUES 
(25),","VALUES (26),","VALUES (27),","VALUES (28),","VALUES (29),");
rsort($ans);
$clength=count($ans);
for($x=0;$x<$clength;$x++)
{
$sql = "INSERT INTO $name (Songs_Name)
echo'$ans[$x]';
";}

if ($conn->query($sql) === TRUE) {
echo"hooray";
}
?>

1 个答案:

答案 0 :(得分:0)

2件事 sql语法sql字符串创建 php允许您保存for循环:

<?php
session_start();
$name = $_SESSION['A_User'];
$ans=array("(1)","(2)",...);


$sql .= "INSERT INTO $name (Songs_Name) ". implode(",", $ans) ;


if ($conn->query($sql) === TRUE) {
echo"hooray";
}
?>