这里完全是新手。我有一个关于插入语法的问题。我希望我的表格看起来像这样,以 ID 作为主键(AI、NN),并且我可以在文档上输入具有相同大小写和名称的多个项目。我目前正在使用一个 php 脚本,该脚本从结果中提取这些信息并将其插入数据库,但在语法方面存在问题。
ID| Case| Name| Document| Date
----------------------------------------------
1 233 alex ITR 2015-1-1
2 233 alex ITR 2015-1-1
3 233 alex NULL 0000-0-0
4 234 ben COC 2015-3-3
5 234 ben VAT 2015-3-3
6 234 ben NULL 0000-0-0
我的代码是这样的:
$sql="INSERT INTO table1 (Case, Name, Document, Date) VALUES ('$casenum','$name','$doc1-5(*this is where I dont know what todo)','$date1-5(this also)
最佳答案
如果您只有特定的文档和日期列表,难道不能只使用数组吗?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$docs =('ITR', 'COC', etc);
$date =('2015-1-1', etc);
// prepare and bind
$stmt = $conn->prepare('INSERT INTO table1 (Case, Name, Document, Date) VALUES (?, ?, ?, ?)');
$stmt->bind_param('ssss', $casenum, $name, $docs[0-4], $date[0-4]);
$stmt->execute();
$stmt->close();
$conn->commit(); //otherwise your data won't save
还要确保始终使用prepare和bind_param以防止sql注入(inject) - 至关重要,否则您的整个数据库可能会变得困惑
关于php - 在具有重复项的单列上插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30042692/