0

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);
?>
2
  • try echo "INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('$date','$tips')"; and show your output here Commented Dec 5, 2013 at 6:20
  • hackers heaven here!!! sanitize your posts or you are into big trouble. or try to use prepared statements. Commented Dec 5, 2013 at 6:21

5 Answers 5

1

try

if( mysqli_query($con,"INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('$date','$tips')"))
{
    echo 'success';
}else{
    echo 'failed';
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

try this

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

1 Comment

what is the error? or you try with mysql_query insterad of mysqli_query
0

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

6 Comments

why are you introducing unnecessary complication in string concatenation?
this is not complication,,, it will parse your data in right way , wheather it is int type or string type ..
you have tried my code ,, is there occuring any error , please let me know
what is wrong with "INSERT INTO daily_tips (PublishingDate,Tips) VALUES ('$date','$tips')"? isnt it more readable and less prone to errors?
actully when we insert data, then we should make sure that our data is parse correctly in comparision to thier data type.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.