-2

I am creating a portal in which I am selecting the value in the input field on the basis of last entry in the database.

For example

if last value of column return_no =1 than next time it will take automatically 2 and so on.

Below is my code for more clarity

HTML Code

<div class="form-group">
     <label class="control-label col-md-3 col-sm-3 col-xs-12">Client Name*</label>
     <div class="col-md-9 col-sm-9 col-xs-12">
     <?php
     $sql = "select vname from vendor";
     $stmt = $conn->prepare ( $sql );
     $result = $stmt->execute();
     ?>
     <select class="select2_single form-control" tabindex="-1" name="vname" id="vname" onChange="getinfo(this.value)" required>
        <option value=""></option>
        <?php
        foreach($stmt as $row){
        ?>
        <option value="<?php echo $row['vname'];?>"><?php echo $row['vname'];?></option>
        <?php
        }
        ?>
     </select>
     </div>
</div>

<div class="form-group">
     <label class="control-label col-md-3 col-sm-3 col-xs-12">Return No</label>
     <div class="col-md-9 col-sm-9 col-xs-12">
     <input type="number" class="form-control" placeholder="Return No" name="rno" id = "rno" required="required">
</div>

Script

function getinfo(vname){
    var vname = $('#vname').val();
    //alert(sys_id);
    $.ajax({

        url:'information.php',
        type:"POST",    
        data:"name="+vname+"&type=vname",
        cache: false,
        success: function(response){
            var getResponse = $.parseJSON(response);
            console.log(getResponse);
            $('#rno').val(getResponse.rno); 
            $('#rperiod').val(getResponse.rperiod);
            $('#retdate').val(getResponse.retdate);
        }
    });
}

Information.php

<?php
session_start ();
include_once 'utils/DB.php';
include_once 'utils/config.php';
$name=($_POST['name']);
$type=($_POST['type']);

if($type=='vname')
{ 
    $name=($_POST['name']);
    $sql = "select rno, rperiod, single_cal2 from vatReturn 
            where vname='{$name}' ORDER BY vId DESC LIMIT 1";
    $stmt = $conn->prepare ( $sql );
    $result = $stmt->execute();
    $r = $stmt->fetch(PDO::FETCH_ASSOC);
    echo json_encode($r);
} 
?>

Now I want if previous value is 1 then $('#rno').val(getResponse.rno); return 2, if previous value is 2 then $('#rno').val(getResponse.rno); return 3 and so on

Ahmed Numaan
  • 1,034
  • 1
  • 10
  • 26
richa
  • 1
  • 5
  • 2
    What's your question? Is this not working as you expect? – WillardSolutions Sep 04 '18 at 16:36
  • 3
    Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Sep 04 '18 at 16:39
  • If I understand there are a number of ways `$('#rno').val(getResponse.rno+1);` or maybe `$r = $stmt->fetch(PDO::FETCH_ASSOC); $r.rno++;` – RiggsFolly Sep 04 '18 at 16:42
  • If I'm not mistaken, you can parse the variable to an integer in PHP with intval() and then add 1 to it. Use an if statement. – ihodonald Sep 04 '18 at 16:48
  • @RiggsFolly Its not working I already try the same – richa Sep 04 '18 at 16:53
  • @ihodonald can you please tell me the code – richa Sep 04 '18 at 16:55
  • @richa you want to increment $('#rno').val(getResponse.rno) by 1? what does getResponse.rno return? – 95faf8e76605e973 Sep 04 '18 at 17:02
  • 1
    I don't see a column named return_no and I haven't used `FETCH_ASSOC` in so long that I don't know how that's even working. Just try `$('#rno').val((parseInt(getResponse.rno) + 1));` instead of `$('#rno').val(getResponse.rno);` If that doesn't work, then I'm sorry. I haven't used `FETCH_ASSOC` in a long time and I don't completely understand the problem. – ihodonald Sep 04 '18 at 17:02
  • "I am selecting the value in the input field on the basis of last entry in the database."...please tell me you are not using this technique in place of having an actual auto-increment field in your database? It sounds suspiciously like you're trying to generate a brand new label number as the current maximum ID plus one? Hopefully if you consider the case when two users load this page at the same time then you'll see the obvious flaw in this plan. – ADyson Sep 04 '18 at 17:05
  • @ihodonald thanks It's working. Now I also want to add the current year at the end of the no like 1-2018, 2-2018. Can you help me in this – richa Sep 04 '18 at 17:18
  • In JavaScript: `var date = new Date()` then where you want the year, enter `date.getFullYear()`. Docs: https://www.w3schools.com/js/js_dates.asp Working example: https://www.w3schools.com/js/tryit.asp?filename=tryjs_date_getfullyear I'm not sure if you want that as the value or not. If so, just plug that variable right into your success function and concatenate it to the value with `+`. – ihodonald Sep 04 '18 at 17:22
  • Did you read my earlier comment? I think you are at serious risk of ending up with duplicated label numbers using this approach. Don't try to use your application to generate unique identifiers - only by the database using an auto-increment column can guarantee uniqueness. – ADyson Sep 05 '18 at 09:53

1 Answers1

1

Looks to me like you are wanting to create a 'Next' type button (if that guess is wrong, then please correct...)

Two ways for you to get the 'higher than previous' value:

  1. (as @lhodonald suggested in the comments) do it with javascript in the response.

    $('#rno').val((parseInt(getResponse.rno) + 1));
    
  2. do it in the mysql request

     $sql = "select rno +1, rperiod, single_cal2 from vatReturn 
        where vname='{$name}' ORDER BY vId DESC LIMIT 1";
    

with a change to the input as well.....

<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Client Name*</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<?php
$sql = "select vname from vendor";
 // NEW LINE (selects the number that was sent in the form)     
$sql += !empty($_POST['rno']) && is_numeric($_POST['rno'])?" and rno={$_POST['rno']}":'';
$stmt = $conn->prepare ( $sql );
 $result = $stmt->execute();
 ?>
<select class="select2_single form-control" tabindex="-1" name="vname" id="vname" onChange="getinfo(this.value)" required>
<option value=""></option>
<?php
foreach($stmt as $row){
 ?>
<option value="<?php echo $row['vname'];?>"><?php echo $row['vname'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
     <label class="control-label col-md-3 col-sm-3 col-xs-12">Return No</label>
     <div class="col-md-9 col-sm-9 col-xs-12">
     <input type="number" class="form-control" placeholder="Return No" name="rno" id = "rno" required="required">
</div>
Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28