使用PHP插入mySQL但是phpmyadmin

时间:2016-07-30 09:23:24

标签: php mysql phpmyadmin

尝试插入mySQL数据库时,我没有收到任何错误,但phpmyadmin查询中没有显示任何错误。这是我试图插入的php文件的代码。我知道这不是最好的方法,但它是我迄今所拥有的。

<?php

require 'steamauth/steamauth.php';
require 'php/main.php';
require 'php/db.php';

$title = $_POST["title"];
$desc = $_POST["desc"];
$date = $_POST["date"];
$skills = $_POST["skills"];
$budget = $_POST["budget"];
$tc = $_POST["tc"];
$steamid = $steamprofile['steamid'];
$steamname = $steamprofile['personaname'];
$avatar = $steamprofile['avatarmedium'];
$dateposted = date("m-d-y");


// insert into db

if(1==1){

  $insert = $link->query("INSERT INTO livepost (title, desc, date, skills, budget, tc, steamid, steamname, avatar, dateposted)
  VALUES ('$title', '$desc', '$date' , '$skills' , '$budget', '$tc', '$steamid', '$steamname', '$avatar', '$dateposted')");

  header('Location: index');

} else {

  header('Location: index?insertdidntwork');

}

3 个答案:

答案 0 :(得分:3)

在mysql中,date和desc是keywords。您必须转义列名称:

  $insert = $link->query("INSERT INTO livepost (title, `desc`, `date`, skills, budget, tc, steamid, steamname, avatar, dateposted)
  VALUES ('$title', '$desc', '$date' , '$skills' , '$budget', '$tc', '$steamid', '$steamname', '$avatar', '$dateposted')");

提示1:了解准备好的状态7

提示2:执行SQL语句后检查错误。

提示3:你的if else语句没有检查插入是否有效。

答案 1 :(得分:0)

@Jens 已经指出了keywords

使用准备好的语句。

<?php
if(1==1){

  $insert = $link->prepare("INSERT INTO livepost (title, `desc`, `date`, skills, budget, tc, steamid, steamname, avatar, dateposted) VALUES (?,?,?,?,?,?,?,?,?,?)");
  $insert->bind_param("sss",$title,$desc,$date,$skills,$budget,$tc,$steamid,$steamname,$avatar,$dateposted );
  $insert->execute();

  header('Location: index');

} else {

  header('Location: index?insertdidntwork');

}

可能是我认为问题在于这一行。 $dateposted = date("m-d-y");

$dateposted = date("Y-m-d");日期应为Y-m-d格式。

尝试这样一次。并且,请回复。

答案 2 :(得分:0)

也许你可以尝试这个 并且你应该总是使用$ mysqli-&gt; error()函数来了解你有什么类型的错误

   <?php
$title = $_POST["title"];
$desc = $_POST["desc"];
$date = $_POST["date"];
$skills = $_POST["skills"];
$budget = $_POST["budget"];
$tc = $_POST["tc"];
$steamid = $steamprofile['steamid'];
$steamname = $steamprofile['personaname'];
$avatar = $steamprofile['avatarmedium'];
$dateposted = date("m-d-y");
// insert into db
if (1 == 1) {
    $insert = $link ->query("INSERT INTO livepost (`title`,`desc`,`date`,`skills`,`budget`,`tc`,`steamid`,`steamname`,`avatar`,`dateposted`)

    VALUES ('".$title."', '".$desc."', '".$date."' , '".$skills."' , '".$budget."', '".$tc."', '".$steamid."', '".$steamname."', '".$avatar."', '".$dateposted."')") or die($mysqli -> error . __LINE__);
    header('Location: index');
} else {
    header('Location: index?insertdidntwork');
}
?>