3

I have made a sql row as type: datetime.

The input for datetime should be like: 2013-02-02 10:13:20

You get the drill like: 0000-00-00 00:00:00

Now In php I use date("Y-m-d H:i:s");(current date and time) to insert it into my database. There are other variables as well. Those variables get inserted,but the datetime row stays: 0000-00-00 00:00:00

I first got this code when everything except the date worked:

$resultaat = $mysqli2->query("INSERT INTO questions  (title, description, username, date_made) VALUES ('" . $title . "','" . $description . "', '".$username ."','".date("Y-m-d H:i:s")."')");

but all the information in the databases will get "" around them.(Which could have caused the date to jump to 0000-00-00 00:00:00

Now when I try to insert again with other information, it wont even insert anymore. My problems:

  • What is the real problem for the date to set to 0000-00-00 00:00:00? Is it the automatic ""?
  • If it is the "", how can I lose them?

EDIT:

Nvm I lost the "" It wasn't the problem that cause the insert to fail at the second try. - Why wont it insert in it anymore after I inserted once?

Now these aren't really different questions because it's about the same problem but here's my code and yes I know SQL Injection, I'll fix it later:

if (isset($_POST['title'])) {
$alles_goed=true;
$description=$_POST['description'];
$title=$_POST['title'];
$username=$_SESSION['username'];

if ($title=''){ 
$alles_goed=false;
echo'title is empty';
}

if($alles_goed==true){
    $resultaat = $mysqli2->query("INSERT INTO questions  (title, description, username, date_made) VALUES ('" . $title . "','" . $description . "', '".$username ."','".date("Y-m-d H:i:s")."')");

}
}
3
  • 4
    Print final SQL query. Then see if the value if correct. Commented Sep 26, 2013 at 7:33
  • Store date("Y-m-d H:i:s") value in a variable and assign to it and check whether it is inserting Commented Sep 26, 2013 at 7:34
  • @SaranyaSadhasivam still not inserting Commented Sep 26, 2013 at 7:40

2 Answers 2

5

Try this:

$a=date("Y-m-d H:i:s");
if (!$resultaat = $mysqli2->query("INSERT INTO questions  (title, description, username, date_made) VALUES ('$title','$description','$username','$a')"))
{
   printf("Errormessage: %s\n", $mysqli2->error);
   exit;
}

and check if there is any error produced.

As to why is inserting it only once, you might have unique field.

Sign up to request clarification or add additional context in comments.

8 Comments

I really have no idea what fixed it but when I did this. (Btw you forgot the "") it worked, but I already tried to put the date("y-m-d h:i:s") in a variable. I think I fixed it myself but I dont know how.
Where did I forgot "" ?
VALUES ('" . $title . "','" . $description . "', '".$username ."','".$a."')"
I didn't. My query is perfectly legal without "". Try it yourself.
In PHP, you can put variable inside double quotes. Try it for yourself.
|
5

Just use a now() on MySQL

$resultaat = $mysqli2->query("INSERT INTO questions  (title, description, username, date_made) VALUES ('" . $title . "','" . $description . "', '".$username ."',now()");

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.