I have a form which I want to use to query a Mysql Table. The text area will accept a list of barcodes. Here is an example of the code form:
<form action="getbarcodes.php" method="post" id="calculate">
<div id="data">
<label>Barcode:</label>
<span class="error">*</span><input type="textarea" name="barcode" ><br />
<input type="submit" name="submit" value="View Codes" />
</div>
</form>
I am trying to figure a way to write the getbarcodes.php script, and have been doing a fair bit or research in how to achieve this. I have came across an example which I believe uses OOP to achieve the same result, however because I don't have details of the classes that were written, or PDO parameters etc, I can't figure the example out. Therefore I was wondering how I could translate the example into a basic mysqli query:
foreach(explode("
", Input::get('itemlist')) as $line) {
$item = preg_split('/(?<=\d) (?=[a-z])|(?<=[a-z])(?=\d)/i', $line);
if (isset($item[1])) {
echo 'Looking for ' . $item[1];
$itemObj = DB::table('items')->select('name', 'id')->where('name', '=', trim($item[1], ""))->first();
var_dump($itemObj);
}
}
I know how to look for a single barcode using . The table name is Product.
$result = mysqli_query($con, "SELECT barcode, name, cost, salesprice, vatcode FROM product where barcode = '$barcode' ");
So I presume that from the snippet I have provided, I would be changing 'itemlist' to 'barcode', $item as $barcode. So any help in translating this snippet so I can achieve the desired results using mysqli would be greatly appreciated.
Cheers
You may need to manipulate $_POST['barcode']
or $barcodes
a little more if it is not in the right format.
if ( ! empty($_POST['barcode']) ) {
$barcodes = explode("
", $_POST['barcode'])
foreach ( $barcodes as $barcode) {
$result[] = mysqli_query($con, "SELECT barcode, name, cost, salesprice, vatcode FROM product where barcode = '$barcode' ");
}
if (! empty($result)) var_dump($result);
else echo "No Product(s) Found";
}
if ( ! empty($_POST['submit']) && ! empty($_POST['barcode'])) {
$barcodes = "'" . str_replace("
", "','", $_POST['barcode']) . "'";
mysqli_query($con, "SELECT barcode, name, cost, salesprice, vatcode FROM product where barcode IN ($barcode)");
}
Use above code. Here I'm expecting barcodes separated by new line.