简体   繁体   English

PHP问题插入与auto_increment

[英]PHP issue insert with auto_increment

I'm pretty new to PHP code. 我对PHP代码非常陌生。 I have a tabel that I want to insert in to but for some reason the insert statement does not work. 我有一个要插入的表格,但是由于某种原因,insert语句不起作用。 The tabel has an ScanID column that AUTO_INCREMENT 's. 该表格具有一个AUTO_INCREMENT的ScanID列。 I've tried a few things like just leaving the ScanID out of the statement but that didn't work either ( I also tried swapping the NULL with ' '). 我已经尝试了一些操作,例如仅将ScanID保留在语句之外,但这还是不起作用(我还尝试将NULL与''交换)。 I was able to insert in to other tables where there isn't an ID that AUTO_INCREMENT's so my pretty sure that my connection works. 我能够插入其他表中没有AUTO_INCREMENT的ID的情况,因此我可以肯定我的连接有效。

<?php

$xml=simplexml_load_file("someFile.xml");
$con =new PDO("mysql:host=localhost;dbname=testDB",'root','');



$ScanType="someType";
$start_date=$xml ->start_datetime;
$end_date=$xml ->finish_datetime;

$TargetTargetID="1";
$stmt=$con->prepare('insert into
Scan(ScanID, ScanType, start_date, end_date, TargetTargetID) values
(:ScanID, :ScanType, :start_date, :end_date, :TargetTargetID)');
$stmt->bindValue('ScanID',NULL);
$stmt->bindValue('ScanType',$ScanType);
$stmt->bindValue('start_date',$start_date);
$stmt->bindValue('end_date',$end_date);
$stmt->bindValue('TargetTargetID',$TargetTargetID);
$stmt->execute();

$ScanID=$con->lastInsertId();
echo $ScanID;
?> 

EDIT: this worked for me 编辑:这为我工作

$stmt=$con->prepare('insert into
Scan(ScanType, start_date, end_date, TargetTargetID) values
(:ScanType, :start_date, :end_date, :TargetTargetID)');

$stmt->bindValue('ScanType',$ScanType);
$stmt->bindValue('start_date',$start_date);
$stmt->bindValue('end_date',$end_date);
$stmt->bindValue('TargetTargetID',$TargetTargetID);
$stmt->execute();

No need to pass auto increment column value in insert query just remove from columns and values to 无需在插入查询中传递自动增量列值,只需从列和值中删除即可

$stmt=$con->prepare('insert into
Scan(ScanType, start_date, end_date, TargetTargetID) values
(:ScanType, :start_date, :end_date, :TargetTargetID)');

Remove from bind values 从绑定值中删除

$stmt->bindValue('ScanID',NULL);

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

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