0

I am trying to insert data into phpMyAdmin using jQuery and User Agent Switcher for Chrome. I modified the code given in this example: https://www.w3schools.com/php/php_mysql_insert.asp

This data is imported into the database, When I open the file (index.php).

index.php

<script type="text/javascript">
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
      $('body').addClass('mobile');
        <?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`, `device`) VALUES (NULL, '$today', 'mobile' )" ); 
        ?>
    } else {
        <?php
            $today = date('Y-m-d');
            mysqli_query($db,"INSERT INTO `view` (`id`, `date`, `device`) VALUES (NULL, '$today', 'desktop' )" ); 
        ?>
    } 
</script>

phpMyAdmin

----------------------------
|id |    date    | device  |
----------------------------
|1  | 2020-01-15 | mobile  |
|2  | 2020-01-15 | desktop |
----------------------------

This data is sent incorrectly to the phpMyAdmin. It imports both devices (mobile and desktop) into the phpMyAdmin and What code do you recommend to output one device the same code above?

Dharman
  • 30,962
  • 25
  • 85
  • 135
itlearn
  • 45
  • 4
  • You cannot run `sql query` in your browser. You should make an ajax call and let your php code handle `sql query`. example: send request to run `query1` in case of `mobile` and `query2` in case rest. – Aakash Tushar Jan 15 '20 at 06:53
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) While reading that post, remember that PHP is server side and JS/HTML/CSS is client side. – M. Eriksson Jan 15 '20 at 06:54

1 Answers1

0

Your if and else statement are javascript statement and php is server side language so when even javascript renders it's control statement, php already executed that means both php insert queries have been already executed. In case you can also use js there, then prevent the php and make an ajax call after the page loaded successfully.

BlackXero
  • 880
  • 4
  • 17