MySQL INSERT查询有效,但未插入数据

时间:2013-12-05 06:09:10

标签: php mysql sql

我只想插入数据库,但是下面的代码没有显示任何错误,也没有显示我在数据库中插入的数据。任何人都可以帮我调试这段代码。

<?php
$tips = $_POST["tips"];
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];
$date = $year."-".$month."-".$day;

//echo "$date: $tips";

//========================
//  DataBase Connectivity
//========================
$con=mysqli_connect("localhost","root","","mydb");

// Check connection
if (mysqli_connect_errno($con))
  {
    echo "Failed to connect to Database";
    exit;
  }

else{
// Perform INSERT query
    mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('$date','$tips')");
    echo "data inserted"
    exit;
}
mysqli_close($con);
?>

6 个答案:

答案 0 :(得分:1)

if( mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('$date','$tips')"))
{
    echo 'success';
}else{
    echo 'failed';
}

答案 1 :(得分:1)

确保您的日期格式类似于'2013-12-05'($ date = $ year。“ - ”。$ month。“ - ”。$ day)

并尝试以下代码:

mysqli_query($ con,“INSERT INTO daily_tips(PublishingDateTips)VALUES('”。$ date。“','”。$ tips。“')”); echo“数据插入”; 出口;

答案 2 :(得分:0)

试试这个

mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('".$date."','".$tips."')");

答案 3 :(得分:0)

首先你不需要提供else条件删除“else”块

如果一切正常,请尝试以下方法:

 $sql= mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES  ('".$date."','".$tips."')");

 if( $sql) {
 echo "data inserted";
 } else {
 echo "not inserted";
 }

答案 4 :(得分:0)

只需使用完整的代码并告诉我,它的工作正常,让我知道您为PublishingDate使用的数据类型是什么?

<?php
$tips = "Today tips";
$day = "12";
$month = "01";
$year = "2012";
$date = $year."-".$month."-".$day;

//echo "$date: $tips";

//========================
//  DataBase Connectivity
//========================
$con=mysqli_connect("localhost","root","","test");

// Check connection
if (mysqli_connect_errno($con))
  {
    echo "Failed to connect to Database";
    exit;
  }

else{
// Perform INSERT query
    mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('$date','$tips')");
    echo "data inserted";
    exit;
}
mysqli_close($con);
?>

答案 5 :(得分:-1)

尝试此查询

mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES (".$date.",".$tips.")");