I'm trying to add a number to a column called ID in my database. What should happen is when there no data in the database this script will insert the first line with the ID numbered as 1 but the issue is it's not working.
I think my checking of null data is the issue can one of you help me by pointing out what should I do.
My code
<?php
/**
* @author SiNUX
* @copyright 2013
*/
include ('connect.php');
$lastId = mysql_query("SELECT ID FROM poiinfo");
$row = mysql_fetch_array($lastId);
if (is_null($row['ID'])){
$nId = $row['ID'];
$nId = 0;
$nId = $nId++;
$addId = "INSERT INTO poiinfo(`ID`) VALUES ('$nId')";
mysql_query($addId);
}else {
$lId = $row['ID'];
$lId = $lId + 0;
$lId++;
}
?>
this query which you are executing
INSERT INTO poiinfo('ID') VALUES ('$nId')
will most likely fail because the columnname
was enclosed by single quote. The quote should be reomved or replace it with backtick, eg
INSERT INTO poiinfo(ID) VALUES ('$nId')
or
INSERT INTO poiinfo(`ID`) VALUES ('$nId')
and so the question is, when do I use backtick? It's you are using a name which is on the list of MySQL's reserved keyword,
and lastly, you must pass the variable containing the query in mysql_query()
mysql_query($addId);
you will have to reset the auto increment field ID
so that you can insert 1 if table is empty