-4

my code is something like taxi counter but with time when i click on start "time starts going" and when i click on confirm i want the code to collect thae values that have changed in "money" and "time" section and sends it to database i didn't know what's exactly the error in my code

<?php


if (isset($_POST['money'])) { 
sleep(4); 
$servername='localhost';
$username='root';


 $dbname = "khalil";
$conn=mysqli_connect($servername,$username,$password,"$dbname");



 $password='';



 $money=$_POST['money'];


 
 $hours=$_POST['hours'];
 $mins=$_POST['mins'];
 $seconds=$_POST['seconds'];

 $sql = "INSERT INTO `history` (`prix`,`time1`,`date1`) VALUES (`$money`,`mins`,`wassim`)";

 // insert in database 
 $rs = mysqli_query($conn, $sql);
 
 if($rs)
 {
    
    $success= "done";
 }
 
   
}

?>

    $('#confirm').click(function(e) {
        e.preventDefault();
        clearTimeout(timex);
         $.ajax({
         method: "post",
         url : "collect.php",
         data: $('#data').serialize(),
         datatype: "text",
         success : function (response){
             $('#done').html('done'),1000;}
         })});   
   
<form id="data" method="post">
  
        </div>
        <h4>Post 1 </h4>
        <div id="timer">
          <span id="hours">00:</span>
          <span id="mins">00:</span>
          <span id="seconds">00</span>
          <br><span id="money">0 $</span>
        </div>
        <div id="controls">
          <button id="start">Start</button>
          <button id="stop">Stop</button>
          <button id="reset">Reset</button>
          <button id="confirm">confirm</button><br>
         
</form>
   
  • Without any error message, how can we tell what's wrong with your code? It can be anywhere from a javascript error to typo's in PHP to no database connection to empty `$_POST`. Impossible for us to guess. So read the browser console and the PHP logs carefully. Most times javascript and PHP tell you exactly where and what the error is. Try to fix them. Only then, when you run into a problem fixing it, come here and ask a specific question. – Michel May 29 '22 at 10:22
  • @Michel the errors are in php , undefined array key , money, hours, mins and secs – Wassim Jabeer May 29 '22 at 13:12
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 29 '22 at 16:40
  • @dharman can i insert values that came from js function into data base ? if yes it would be a pleasure to help me with it i've been struggling with this problem the past 2 days – Wassim Jabeer May 30 '22 at 08:40

0 Answers0