dsa88885555 2013-01-22 09:49
浏览 92
已采纳

如果数据库为空,如何添加数据?

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++;
}

?>
  • 写回答

2条回答 默认 最新

  • drk7700 2013-01-22 09:52
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!