Issue in getting autofill value from database table

I get value of mrno and name by get method from the landing page. Its working fine till that point. But the problem start when I tried to get Previous Amount value from the existing table of mysql database using script . Its work fine when I manually enter mrno in first field as it successfully extract Previous Amount value from database. But as per my requirement, I only have to enter advance amount field, but in that case I unable to extract previous Amount value from the database as i didnt do keyup for mrno. How can i achieve that ?? Below is my code .

<?php 
include "config.php";
if (isset($_GET['id'])) {

    $user_id = $_GET['id']; 

    $sql = "SELECT * FROM mis6 WHERE `id`='$user_id'";

    $result = $link->query($sql); 

    if ($result->num_rows > 0) {        

        while ($row = $result->fetch_assoc()) {

                $mrno = $row['mrno'];
		$name1 = $row['name1'];
		;              

        } } }
    ?>                           
                 <form>     
		<label>MR No:</label> <input type="text" name= "mrno"  id= "mrno" value="<?php echo $mrno; ?>"  onkeyup ="GetDetail(this.value)" value="">
		<label>Name:</label> <input type="text" name= "name1"  id= "name1"  value="<?php echo $name1; ?>" >
		<label>Advance Amount</label> <input type="text" name= "advamt"  id= "advamt" >
		<label>Previous Amount:</label> <input type="text" name= "padv"  id= "padv" >
		<input type="submit" name="submit" id="submit" value="Submit"  class="btn btn-success submit_btn invoice-save-btm">

<script>
		// onkeyup event will occur when the user
		// release the key and calls the function
		// assigned to this event
		function GetDetail(str) {
			if (str.length == 0) {
				document.getElementById("padv").value = "";
				
				return;
			}
			else {

				// Creates a new XMLHttpRequest object
				var xmlhttp = new XMLHttpRequest();
				xmlhttp.onreadystatechange = function () {

					// Defines a function to be called when
					// the readyState property changes
					if (this.readyState == 4 &&
							this.status == 200) {
						
						// Typical action to be performed
						// when the document is ready
						var myObj = JSON.parse(this.responseText);

						// Returns the response data as a
						// string and store this array in
						// a variable assign the value
						// received to first name input field
						
						document.getElementById
							("padv").value = myObj[0];
						}  };					
				

				// xhttp.open("GET", "filename", true);
				xmlhttp.open("GET", "gfg1.php?mrno=" + str, true);
				
				// Sends the request to the server
				xmlhttp.send();
			}
		}
	</script>  </form>
// here is gfg1 file code which handle database

<?php

// Get the mrno
$mrno = $_REQUEST['mrno'];

// Database connection
$con = mysqli_connect("localhost", "thehospi_root", "u1m1a1r1", "thehospi_hmis");

if ($mrno !== "") {
	
	// Get corresponding mrno
		
	$query = mysqli_query($con, "SELECT  padv FROM mis14 WHERE mrno ='$mrno'");

	$row = mysqli_fetch_array($query);

	// Get the first name
	$ccc = $row["padv"];
	
}
// Store it in a array
$result = array("$ccc");
// Send in JSON encoded form
$myJSON = json_encode($result);
echo $myJSON;
?> 
1 Like

If you already have the value for mrno because it’s being passed in to your code, why not just run the second query to retrieve padv immediately after you retrieved mrno based on the user id?

if (isset($_GET['id'])) {
    $user_id = $_GET['id']; 
    $sql = "SELECT mrno, name1 FROM mis6 WHERE `id`='$user_id'";
    $result = $link->query($sql); 
    if ($result->num_rows > 0) {        
        while ($row = $result->fetch_assoc()) {  // why is there a while() loop?
            $mrno = $row['mrno'];
            $name1 = $row['name1'];
        }
   // check you have a value for `mrno`, then retrieve `padv` using that value
   // run the query here
    }
 }

You should also look at using Prepared Statements rather than concatenating user-supplied data into queries like that. Also, if you only need two column values, don’t select *, just get those two column values.

second query to retrieve padv immediately after you retrieved mrno based on the user id?

yes its based on user-id but from the other database table . how i can run that querry immediately here to retrieve padv value ??? can u guide

You maybe can run a “double query” (aka CTE) to reduce the PHP code?

Just pseudocode. You have to adjust this to your need and add extra conditions if needed.

WITH getid AS (SELECT mrno_id FROM mis6 WHERE id=1)
SELECT padv FROM mis14 WHERE mrno = (SELECT * FROM getid)

As I have not table specs, the above is a wild guess. Maybe try a fiddle for explaining?

I tried this but nothing is happening

if (isset($_GET['id'])) {

    $user_id = $_GET['id']; 

    $sql = "SELECT * FROM mis6 WHERE `id`='$user_id'";

    $result = $link->query($sql); 

    if ($result->num_rows > 0) {        

        while ($row = $result->fetch_assoc()) {

                $mrno = $row['mrno'];
		$name1 = $row['name1'];
		$age1 = $row['age1'];
		$mobno = $row['mobno'];
		$gender1 = $row['gender1'];		
     		$proces  = $row['proces'];
		$doctrname = $row['doctrname'];              

        } } }

if (isset($_GET['mrno'])) {
    $user_id = $_GET['mrno']; 
    $sql = "SELECT mrno, padv FROM mis14 WHERE `mrno`='$user_id'";
    $result = $link->query($sql); 
    if ($result->num_rows > 0) {        
        while ($row = $result->fetch_assoc()) {  // why is there a while() loop?
            $padv = $row['padv'];
           
        }
   // check you have a value for `mrno`, then retrieve `padv` using that value
   // run the query here
    }
 }

Did you change your HTML to include (echo) the value, like it is in the mrno input?

1 Like

You need to use the value of mrno that you retrieve from the first query, don’t you? Or are you passing that into the page as well?

I’d have expected something like this for the second query

$sql = "SELECT mrno, padv FROM mis14 WHERE `mrno`='$mrno'";

Or combine them into a single query using a JOIN

$sql = "select * from mis6 left join mis14 on mis14.mrno = mis6.mrno where mis6.id = '$user_id'";

(That’s off the top of my head, I often get stuff in the wrong sequence)

1 Like

Hi @itsumarnazir !! I think to get the previous amount value from the database table without the mrno manually you should use an onload event to call it when the page is loaded. What do you say? So you could pass the mrno value that you get from the landing page to the function and get the previous amount value from the database table automatically with these changes in your code.

Add an id attribute to your form element, so that you can access it in your script. Such as “MyList” etc.

Add an onload event listener to your form element, so that it will call the GetDetail function when the page is loaded.
document.getElementById(“mylist”).addEventListener(“load”, function() { GetDetail(“<?php echo $mrno; ?>”); });

Remove onkeyup attribute from your mrno input element:

I hope this could help to resolve the issue.

1 Like

offcourse i did

Well you showed us the PHP changes you made, not the HTML. So yes, we have to ask the Helpdesk Level 1 questions to make sure :stuck_out_tongue: We’ve all made the silly mistakes before.

While we’re on the subject…

  • is mrno a Varchar field in the database? You seem to be using it as one. Otherwise you wouldnt put quotes around an int.
  • Are you sure that mis14 contains a record with the mrno you’re referencing? “Doing nothing” would also be a symptom of an empty return from the database… (this relates to what @droopsnoot mentioned in their post - are you sure you should be sending user_id into that query?

Thanks alot Lara. Its solved the issue :slight_smile: stay blessed

yes you can ask , i knew it was my mistake as i didnt added enough info :slight_smile:
yes mrno is varchar in database
yes it have the record which i want to extract

Great, as I was hoping, it worked!! Glad for you and BTW you are welcome your kinds words means a lot. Stay blessed!!

1 Like

As another of your threads suggests this is a patient record system, all the more reason to use Prepared Statements to help improve security, among other things.

1 Like

yes it is patient management system. i already converted my source code as suggested by you and other respected members of this forum :slight_smile:

thanks alot sir! its working absolutely fine.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.