返回数据库中n个插入的行

时间:2016-10-18 17:43:32

标签: php jquery html mysql json

我试图逐个回显最后5个插入的db行,然后通过jquery插件打印它。例如进入要插入最后一行的输入区域然后打印然后最后一行-1然后打印等。 在我的HTML中,我在我的printablearea中有这些输入

<li><input type="number" min=1 name="printmultiple" value="1" id="printmultiple"/></li>
<div id="printableArea">
<div align="center">



<h4></h4>
<h4></h4>



<input type="text" name="barcode2"  id="barcode2" required readonly >
<input type="text" name="barcodecountry" id="barcodecountry"  >
<input type="text" name="barcodecountry" id="barcodecountry2"  >

<br>
 <div id="print">
</div>

<label id="idbar">id.:</label><input type="text" name="barcodesurname" id="barcodesurname"  required readonly>
<br>
<br>
<label id="pricebar">price:</label><input type="text" name="barcodename" id="barcodename"  required readonly>
<input type="text" name="barcodecountry" id="barcodecountry3">
</div> 
</div> 

然后在我的php我插入多个值取决于输入printmultiple的值

<?php
require('config.php');
date_default_timezone_set('Europe/Athens');
include("auth.php");


$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

// validate the variables ======================================================
    // if any of these variables don't exist, add an error to our $errors array
      if (empty($_POST['surname']))
        $errors['surname'] = 'Surname is required.';
      if (empty($_POST['name']))
        $errors['name'] = 'Name is required.';



// return a response ===========================================================

    // if there are any errors in our errors array, return a success boolean of false
    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {

$printmultiple = $_POST['printmultiple'];
$signintime = date("Y-m-d H:i:s");
$username = mysqli_real_escape_string($con,$_POST['username']);
$surname =mysqli_real_escape_string($con,$_REQUEST['surname']);
$name= mysqli_real_escape_string($con,$_REQUEST['name']);
$seat= mysqli_real_escape_string($con,$_POST['seat']);
$mail= mysqli_real_escape_string($con,$_POST['mail']);
$ticketprice=mysqli_real_escape_string($con,$_POST['ticketprice']);
$barcode=mysqli_real_escape_string($con,$_POST['barcode']);
$matchtype=mysqli_real_escape_string($con,$_POST['matchtype']);
$telephone= mysqli_real_escape_string($con,$_POST['telephone']);
$expdate = date('Y-m-d H:i:s', time()+36000);
$submittedby = mysqli_real_escape_string($con,$_SESSION["username"]);

$result = mysqli_query($con, "SELECT customid FROM `salonika` ORDER BY `customid` DESC LIMIT 1");
$row = mysqli_fetch_array($result);
$max=$row['customid'];
$stmt = $con->prepare("INSERT INTO salonika (surname, name, telephone,customid,seat,mail,ticketprice,barcode,expdate,submittedby,signintime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

for ($i=0; $i<$printmultiple; $i++) {
    $barcode=$barcode+1;
    $seat=$seat;
    $mail=$mail;
    $ticketprice=$ticketprice;
    $expdate=$expdate;
    $submittedby=$submittedby;
    $signintime=$signintime;
    $max=$max+1;
    $surname = $surname;
    $name = $name;
    $telephone = $telephone;
    $stmt->bind_param('sssississss', $surname, $name, $telephone, $max,$seat,$mail,$ticketprice,$barcode,$expdate,$submittedby,$signintime);

    $stmt->execute();


}

$stmt->close();


        // show a message of success and provide a true success variable
        $data['success'] = true;
        $data['message'] = 'Success!';



    }

    // return all our data to an AJAX call
    echo json_encode($data);

在我的js文件中我有这个

var formData = {
            'surname'       : $('input[name=surname]').val(),
            'name'      : $('input[name=name]').val(),

            'telephone' : $("#telephone").val(),
            'mail' : $("#mail").val(),
            'barcode' : $("#barcode").val(),
            'customid' : $("#customid").val(),
            'ticketprice' : $("#ticketprice").val(),
            'seat' : $("#seat").val()

        };
        // process the form
        $.ajax({
            type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url         : 'process.php', // the url where we want to POST
            data        : formData,// our data object
            dataType    : 'json', // what type of data do we expect back from the server
            encode      : true
        })
            // using the done promise callback
            .done(function(data) {


                // log data to the console so we can see
                console.log(data); 

                // here we will handle errors and validation messages
                if ( ! data.success) {
                        if (data.errors.surname) {
                        $('#surname-group').addClass('has-error'); // add the error class to show red input
                        $('#surname-group').append('<div class="help-block">' + data.errors.surname + '</div>'); // add the actual error message under our input
                        }
                        else if (data.errors.name) {
                        $('#name-group').addClass('has-error'); // add the error class to show red input
                        $('#name-group').append('<div class="help-block">' + data.errors.name + '</div>'); // add the actual error message under our input
                        }



                } else {
                    $('#printableArea').print();
                    // ALL GOOD! just show the success message!
                    $('form').append('<div class="alert alert-success">' + data.message + '</div>');
                     window.setTimeout(function(){location.reload()},3000);

有没有办法一个接一个地打印到打印机最后插入的行?

1 个答案:

答案 0 :(得分:1)

$stmt->close();之后添加以下代码,根据您在此处提出的问题,我只需构建一个查询并执行它,即可获取用户插入的最后$printmultiple行。

然后它将名为added的条目传递给您可以在jQuery响应.done函数中处理的响应。

$printmultiple = intval($printmultiple); //sanitize variable
$result = mysqli_query($con, "SELECT * FROM `salonika` ORDER BY `customid` DESC LIMIT ".$printmultiple);
$rows = mysqli_fetch_array($result);


// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
$data['added'] = $rows;

发送回jQuery的数据以JSON格式编码。

最后JS部分如下:

&#13;
&#13;
if ( data.success) {		
  //success
  $.each(data.added, function (i,it){
   alert(data.added[i]);
  });
  $('form').append('<div class="alert alert-success">' + data.message + '</div>'); 
  
  
}else{
  //code if error
       
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;