RDX
RDX

Reputation: 31

MySQL INSERT query works but Data is not inserted

I just want to insert into database, but following code is not showing any error nor showing my inserted data in database. Can anybody help me in debugging this code.

<?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);
?>

Upvotes: 0

Views: 1927

Answers (5)

Muthamizhchelvan. V
Muthamizhchelvan. V

Reputation: 1261

Just use the complete code and let me know, its working fine for me and let me know what the data type you are using for 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);
?>

Upvotes: 0

Mohammed Saqib Rajput
Mohammed Saqib Rajput

Reputation: 1370

Make Sure your date format is like this '2013-12-05' ($date = $year."-".$month."-".$day)

and try this code:

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

Upvotes: 1

simply-put
simply-put

Reputation: 1088

try

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

Upvotes: 1

uvais
uvais

Reputation: 416

first of all you do not need to provide else condition remove "else" block

if everything is ok above then try this:

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

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

Upvotes: 0

himanshu bhardiya
himanshu bhardiya

Reputation: 81

try this

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

Upvotes: 0

Related Questions