簡體   English   中英

如何在表中插入多行數據

[英]How to Insert multiple rows of data into a table

我目前對所有編程都是新手,在涉及PHP時我遇到各種各樣的問題,但是我的第一個問題是如何僅使用一個變量將多個數據添加到表中。 例如:

<?php
$mysqli = mysqli_connect('localhost', 'root', 'testpass', 'testdatabase')
or die (mysqli_error());

$test = "INSERT INTO test (test1, test2) VALUES ('test1', 'test2')";
$test .= "INSERT INTO test (test1, test2) VALUES ('test3', 'test4')";

if (!$test){
  echo "Sorry but you information could not be added...";
}
else {
  $test_result = mysqli_query($mysqli, $test);
  echo "Your data has been entered!";
}

當我嘗試向表中添加多個數據時使用此代碼時,它不起作用。 我一次只能添加1行。 我永遠不能一次添加2、3或更多行。 我的代碼中有錯誤嗎? 當我查看其他示例時,人們使用的是mysql,而不是mysqli。 使用mysql的人使用與我上面相同的代碼。 我更喜歡使用mysqli,而不必切換,因為它很小。 回到我的問題,如何將多個數據行添加到表中? 謝謝!

要插入多行,請使用以下命令:

INSERT INTO test (test1, test2) VALUES ('test1', 'test2'), ('test3', 'test4'), ('test5', 'test6')

這將插入3行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM