0

I am trying to insert data to mysql from ajax with php here is my code can anyone help. I cannot run sql query in your browser. I should make an ajax call and let your php code handle sql query. How to import data into database and phpMyAdmin in javascript

I modified the code given in this example: https://www.derekshidler.com/inserting-form-data-into-mysql-using-php-and-ajax/

index.php

<script type="text/javascript">
$(document).ready(function(){
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
        $('body').addClass('mobile');
        $.ajax({
            type    : "POST",
            url     : "ajax.php",
            data    : {'device': 'mobile'},
            success : function(result) { alert(result); }
        });
    } else {
        $('body').addClass('desktop');
        $.ajax({
            type    : "POST",
            url     : "ajax.php",
            data    : {'device': 'desktop'},
            success : function(result) { alert(result); }
        });
    }
});
</script>

ajax.php

<?php
  $db = mysqli_connect(localhost,root,root,hospital) or die ("Opps, Some thing went wrong!");
  $today = date('Y-m-d');
  mysqli_query($db,"INSERT INTO `view` (`id`, `date`) VALUES (NULL, '$today' )" ); 
?>

This data is not entered in the phpMyAdmin, When I open the file (index.php). What could be the reason and what am I doing wrong?

itlearn
  • 45
  • 4

1 Answers1

0
$db = mysqli_connect(localhost,root,root,hospital) or die ("Opps, Some thing went wrong!");
$today = date('Y/m/d');
mysqli_query($db,"INSERT INTO `view` (`view`) VALUES ('$today' )" ); 
mohsen
  • 142
  • 1
  • 5