4

i would like to help with my code how to edit HTML input date into MySQL TIMESTEMP format cause my SQL code is invalid and data are not added to my database, Thanks for help

also i asking to check this code on bottom cause 2nd SQL deppends on 1st SQL, thanks

if (isset($_POST['pridaj_anime_submit'])) {
   $sql_vloz_anime = "INSERT INTO anime (a_name, a_year, a_translated_min, a_translated_max, a_rate_min, a_rate_max, a_edit, a_condition)
                              VALUES ('$_POST[nazov]', '$_POST[rok]', '0', '$_POST[pocet]', '8', '10', '$_POST[preklad]', '$_POST[stav]')";
   mysqli_query($connect_to_db , $sql_vloz_anime);
   $sql_ziskaj_a_id_pridaneho_anime = "SELECT * FROM anime WHERE a_name = '$_POST[nazov]'";
   $run_sql_ziskaj_a_id_pridaneho_anime = mysqli_query($sql_ziskaj_a_id_pridaneho_anime);
   $a_id_ziskane = "";
while ($db_data = mysqli_fetch_assoc($run_sql_ziskaj_a_id_pridaneho_anime)) {
    $a_id_ziskane = $db_data['a_id'];
}

   $sql_vloz_anime_info = "INSERT INTO anime_info (a_id, a_img, a_start, a_stop, a_time_ep, a_akihabara)
                                        VALUES ('$a_id_ziskane' , '$_POST[obrazok]', '$_POST[zaciatok]', '$_POST[koniec]', '$_POST[cas]', '$_POST[akihabara]')";
   mysqli_query($connect_to_db , $sql_vloz_anime_info);
}
3
  • Learn to use parameters. Save yourself syntax problems and protect your code from SQL injection. Commented Jul 7, 2017 at 11:37
  • 1
    Please take a look at bobby-tables.com - Your code runs at a really high risk of SQL injection. NEVER put user-inputs directly in querys, use prepared statements instead. For more information see the posted link. Commented Jul 7, 2017 at 11:38
  • @Twinfriends checked it :) thx gonna go fix whole project Commented Jul 7, 2017 at 11:45

2 Answers 2

12
$input_date=$_POST['date'];
$date=date("Y-m-d H:i:s",strtotime($input_date));

This will convert input date into MySQL Timestamp compliant format. Please DO NOT put POST values directly in your queries. Your website or web application will be hacked in no time through SQL Injections.

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

Comments

2

MySql datetime considered yyyy-mm-dd h:i:s format.

Your input date is like dd-mm-yyyy h:i:s and so on.

Then use convert date to yyyy-mm-dd h:i:s. Read strtotime manual.

For e.g.

$inputDate = '06-06-2017 05:21:34';
$mysqlDate = date("Y-m-d H:i:s",strtotime($inputDate));

// MySql Query.
INSERT INTO table_name SET `your_field`='$mysqlDate'

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.