doubi9531 2014-08-15 10:38
浏览 29
已采纳

PHP / mysql INSERT

I want to insert some values into a mysql database and thought it can't be that hard. However I was wrong, maybe i am overlooking something simple.

This is my code:

$db = mysqli_connect("localhost", "*****", "*********", "usr_web633_3");
if(!$db)
{
  exit("Error: ".mysqli_connect_error());
}

$sql = "INSERT INTO anmeldungen ('ID', 'vorname', 'name', 'gender', 'str', 'hausnummer', 'telefon', 'gemeinde', 'vegetarier'')
VALUES ('id', 'vorname', 'name', 'gender', 'str', 'hausnummer', 'telefon', 'gemeinde', 'vegetarier')";

$sqlinsert = mysqli_query($db, $sql); 

if($sqlinsert == true) { 
    echo "works!"; 
} 
else { 
    echo "doesn't work:(";
}

It doesn't work like it should.. the values are just placeholders.

Hope you can help!

  • 写回答

4条回答 默认 最新

  • dongzhuner6981 2014-08-15 10:40
    关注

    Escape column and table names with backticks.

    INSERT INTO `anmeldungen` (`ID`, vorname, name, gender, str, hausnummer, telefon, gemeinde, vegetarier)
    VALUES (1, 'vorname', 'name', 'gender', 'str', 'hausnummer', 'telefon', 'gemeinde', 'vegetarier')
    

    And you had a quote too much at the last column name. Use a single backtick instead. But actually you only need to escape reserved words in MySQL

    And watch out for your column data types. You can't insert a string into a number column. So id can probably only be a number.

    And if your id is an auto-increment value then you should not provide a value at all. Use null or leave the column from your insert statement completely.

    INSERT INTO `anmeldungen` (`ID`, vorname, ...
    VALUES (null, 'vorname', ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿