aristeidhsP aristeidhsP - 1 month ago 7
HTML Question

Return back the n inserted rows in a database

I am trying to echo last 5 inserted rows of db one by one and then print it through jquery plugin . For example into the input area to be inserted last row then print then last row -1 then print etc..
in my html i have these inputs in my 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>


then in my php i am inserting multiple values depending of the value of input 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);


in my js file i have this

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);


is there any way to print to the printer one by one last inserted rows?

Answer

Add the following code after $stmt->close();, according to your question here I simply build a query and execute it fetching the last $printmultiple rows, that the user inserts.

Then it passes an entry called added to the response that you can handle in your jQuery response .done function.

$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;

The data sent back to jQuery is encoded in JSON format.

Finally the JS part would be as follows:

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

Comments