简体   繁体   English

插入每个值的新行

[英]Insert into makes new row for every value

I have a problem with MySQL. 我的MySQL有问题。 I use PHP with mysqli to write data into prepared table. 我使用PHP和mysqli将数据写入准备好的表中。 But if I submit the form, which values have to be written into table, each value makes new row in the table. 但是,如果我提交了必须将值写入表的表单,则每个值都会在表中添加新行。 Here is my code: 这是我的代码:

session_start();
$_SESSION["Field"]=array("Name1","Name2","Name3",...);
if(!empty($_POST)){
foreach($_SESSION["Field"] as $Variable){
if(!empty($_POST[$Variable])){
$_SESSION[$Variable]=$_POST[$Variable];
}}}
if(array_key_exists("Database",$Collection)){
$Database=mysqli_connect("localhost","dbadmin","password","mydatabase");
mysqli_query($Database,"insert into Table1 (PHPSESSID) values ('".session_id()."')");
foreach($_SESSION["Field"] as $Variable){
if(!empty($_POST[$Variable])){
$_SESSION[$Variable]=mysqli_real_escape_string($Database,$_SESSION[$Variable]);
mysqli_query($Database,"insert into Table1 ($Variable) values ('{$_SESSION[$Variable]}')");
}}
mysqli_close($Database);
}

Update: I think, that the problem is, because all variables should(?) be in one mysqli task. 更新:我认为,问题在于,因为所有变量都应该在一个mysqli任务中。 But I didn't find anything about it. 但是我什么都没找到。 Am I right? 我对吗? Should it be somethink like this mysqli_query($Database,"insert into Table1 (Variable1,Variable2,Variable3) values ('Value1','Value2','Value3')"); 是否应该像这样mysqli_query($Database,"insert into Table1 (Variable1,Variable2,Variable3) values ('Value1','Value2','Value3')"); ?

Update 2: I think, that it could be also caused by not committing the insert tasks, but it isn't necessary to commit it, so I don't think, it is the problem. 更新2:我认为,这也可能是由于未提交插入任务而引起的,但是没有必要提交它,所以我不认为这是问题所在。 But I am a beginner in MySQL and SQL, so maybe I am wrong. 但是我是MySQL和SQL的初学者,所以也许我错了。

It both looks as a solution of my problem, but I don't know, how to make it in foreach loop with variables from an array. 两者看起来都可以解决我的问题,但是我不知道如何使用数组中的变量在foreach循环中实现它。 Can anyone help me? 谁能帮我?

I think you need to understand what foreach and INSERT do before you try using them! 我认为您需要先了解foreachINSERT的功能,然后再尝试使用它们! Use the foreach loop to build the SQL query string, not commit to the database. 使用foreach循环来构建SQL查询字符串,而不是提交给数据库。 After the loop has finished, you will have a complete SQL query that you can execute. 循环完成后,您将具有可以执行的完整SQL查询。

EDIT: Forgot the obligatory "Don't forget to escape your values before inserting them into the database!" 编辑:强制性的“不要忘记将您的值插入数据库之前转义您的值!”

EDIT 2: Here's one answer. 编辑2:这是一个答案。 Certainly not elegant and not tested but could do the job: 当然不是很优雅,也没有经过测试,但是可以做到:

session_start();
if(empty($_POST) || !array_key_exists("Database", $Collection)){
    exit;
}
$checklist = array("Name1", "Name2", "Name3");
$query = "INSERT INTO Table1 SET ";
$db = mysqli_connect("localhost", "dbadmin", "password", "mydatabase");

foreach($checklist as $v){
    if(!empty($_POST[$v])){
        $_SESSION[$v] = $_POST[$v];
        $_POST[$v] = mysqli_real_escape_string($db, $_POST[$v]);
        $query .= "$v='$_POST[$v]',"
    }
}

$query .= "PHPSESSID='" . session_id() . "'";

mysqli_query($db, $query);
mysqli_close($db);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM