【问题标题】:Return back the n inserted rows in a database返回数据库中插入的 n 行
【发布时间】:2017-02-28 02:41:34
【问题描述】:

我正在尝试一一回显插入的最后 5 行 db,然后通过 jquery plugin 打印出来。例如到要插入最后一行的输入区域然后打印然后最后一行-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);

有没有办法将最后插入的行一一打印到打印机上?

【问题讨论】:

  • 您可以在插入操作之后查询表并获取最后5行
  • 没那么难
  • 我不是想将结果回显到表格中,而是要插入到 pritablearea div 中以逐个打印到打印机
  • 你说:我正在尝试一一回显插入的最后 5 行 db,然后通过 jquery 插件打印出来
  • 是的 jquery 插件将 div 的内容打印到打印机

标签: php jquery html mysql json


【解决方案1】:

$stmt-&gt;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部分如下:

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
       
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

  • TypeError: 'in' 操作数 a 无效
  • 类型错误:项目为空
  • 做一个 print_r($jqueryData);在php中并在这里发布
  • 在 $data[..] 之后的 php 中
  • undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多