Jump to content

Can't find error in php mysql jquery


dataxspy
Go to solution Solved by Barand,

Recommended Posts

Basically nothing happens, I know clicking the button works and connecting to the db works but I'm unsure how to diagnose what is wrong with the javascript.  It could be a syntax error but I've scanned for variable errors, wrong spelling.

driver.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>My Website</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
  </head>
  <body>
  
  <form method="POST">
  <input type="text" id="search" size="2">
  <input type="button" name="submit" id="submit" value="Search Drivers">
  </form>

  <table id="tbl_driver" style="display:none">
  <tr><th>Last Name</th><td id="Last_Name"></td></tr>
  <tr><th>First Name</th><td id="First_Name"></td></tr>
  </table>

  <script>
  $(document).ready(function() {

    $("#submit").click(function() {
      
      $.ajax({
        url:"fetch.driver.info.php",
        type:"POST",
        data:{driver:$("#search").val()},
        dataType:"JSON",
        success:function(data) {
          $("#tbl_driver").show();
          $("#Last_Name").text(data.Last_Name);
          $("#First_Name").text(data.First_Name);

          alert( "Handler for .click() called." );
        }
      })

    });

  });
  </script>
  
  </body>
</html>

fetch.driver.info.php

<?php
require 'db.inc.php';

$id = $_POST["driver"];

$sql = "select * FROM Drivers WHERE Driver_ID = $id";

$query = mysqli_query($con,$sql);
$data = array();
while($row=mysqli_fetch_array($query))
{
    $data['Last_Name']=$row['Driver_Last_Name'];
    $data['First_Name']=$row['Driver_First_Name'];
}

echo json_encode($data);
?>

db.inc.php

<?php
$con = new mysqli("localhost","root","","United_Limousine");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}
?>

 

Edited by dataxspy
Link to comment
Share on other sites

edit: repeats advice already given - use the browser's developer console network tab to see what is being sent from the javascript and returned by the server-side code.

next, your search requires an exact match with existing data. you should use a select/option menu instead.

also, don't put external, unknown, dynamic values directly into an sql query. use a prepared query instead. you would probably want to switch to the much simpler PDO database extension, since using the mysqli extension with a prepared query is overly complicated and inconsistent.

don't unconditionally output raw database connection/query errors onto a web page. the connection error contains the database hostname/ip, username, if you are using a password or not, and web server path information. when a hacker intentionally triggers a connection error, by flooding your site with requests that consume all the available connections, the existing connection error handling will give them 2 or all 3 pieces of information they need to connect to your database. instead use exceptions for database statement errors (the latest php versions automatically does this for mysqli connection errors anyways, so your existing connection error handling will never be executed) and in most cases simply let php catch and handle any database exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) this will let you remove any database statement error handling logic you have now, simplifying the code.

for a query that is expected to match at most one row of data, don't use a loop. just fetch that single row of data.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.