missing data from mysql database - php

I have a mysql database that is used by a php script to display data... the problem i have is, at what appears to be random occurrences it misses results and I can't see a pattern to establish why it might be doing it...
all the data appears to be fine when i check the database.
here is my initial search page
<?php
include 'connect.php';
//set variable
$option = '';
// Get the county names from database - no duplicates - Order A-Z
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";
// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";
}
echo "<html xmlns='http://www.w3.org/1999/xhtml'>";
echo "<title>HSB - Latest Offers</title>";
echo "<style type='text/css'>;
body {
background-color: #FFF;
}
#wrapper {
background-color: #FFF;
height: auto;
width: 1000px;
margin-right: auto;
margin-left: auto;
font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<div id='wrapper'>
<p><img src='images/header.jpg' width='400' height='100' alt='header' /></p>
<HR/>
Select an area from the menu below to view any offers in that area.
<form id='filter' name='filter' method='post' action='resultssimple.php'>
<p><label>County</label></p>
<select name='result' id='result'>' . $option . '</select>
<input name='' type='submit' />
</form>
</div>
</body>
</html>";
?>
and here is my results page
<?
include 'connect.php';
//Get the details from previous page
$SelectedCounty = $_POST["result"];
// Select offers linked to selected county from form
$result = mysqli_query($con,"SELECT * FROM offers WHERE tradingCounty ='" . $SelectedCounty . "'ORDER BY categoryIdName ASC;");
// PREVIOUS ATTEMPTS - ALL WRONG - GGGGRRRRRRRRRRRR !!!!!!!!
//------------------------------------------------------------
//$result = mysqli_query($con,"SELECT * FROM offers WHERE tradingCounty LIKE" . $SelectedCounty);
//$result = mysql_query("SELECT * FROM pdetails WHERE uid='" . $inputname . "';");
//"SELECT * FROM `offers` WHERE `tradingCounty` LIKE
//$result = mysqli_query($con,"SELECT * FROM offers;");
//$result = mysql_query("SELECT * FROM pdetails WHERE uid='" . $inputname . "';");
//$result = mysqli_query("SELECT * FROM offers WHERE tradingCounty=" . $SelectedCounty);
//check to see if results is set - error if not.
if(!$result)
{
die("<p>Error in listing tables: ". mysql_error()."</p>");
}
//Show all records for selected county
echo ("<p><h2>Showing Latest Offers In : " . $SelectedCounty . "</h2></p>");
echo ("<p><a href='offers.php' target='_self'>back to search menu</a></p>");
/*
echo ("<table border='1'>");
echo ("<tr>");
echo ("<td>ID</td><td>Category</td><td>Business Name</td><td>Business Address</td><td>Address2</td><td>Address3</td><td>Town</td><td>County</td><td>Post Code</td><td>Telephone</td><td>URL</td><td>Email</td><td>Discount / Special Offer</td><td>valid from</td>");
*/
while($row = mysqli_fetch_row($result))
{
echo ("<div style=' background-color: #EFF5FF; color: #06C; padding: 5px; float: left; border: 1px dotted #06C; margin: 10px; width: 300px; height: 300px; text-align: center; >");
// echo ("" . $row[0] . "");
// echo ("</br>");
echo ("<strong>" . $row[1] . "</strong>");
echo ("<hr/>");
// echo ("</br>");
echo ("" . $row[2] . "");
echo ("</br>");
echo ("" . $row[3] . "");
echo ("</br>");
// echo ("" . $row[4] . "");
// echo ("</br>");
// echo ("" . $row[5] . "");
// echo ("</br>");
echo ("" . $row[6] . "");
echo ("</br>");
echo ("" . $row[7] . "");
echo ("</br>");
echo ("" . $row[8] . "");
echo ("</br>");
echo ("" . $row[9] . "");
echo ("</br>");
// echo ("" . $row[10] . "");
// echo ("</br>");
echo ("" . $row[11] . "");
echo ("</br>");
echo ("<hr/>");
echo ("<strong>" . $row[12] . "</strong>");
echo ("</br>");
echo ("</div>");
/* echo("<tr>");
echo("<td>" . $row[0] . "</td>" . "<td>" . $row[1] . "</td>" . "<td>" . $row[2] . "</td>" . "<td>" . $row[3] . "</td>" . "<td>" . $row[4] . "</td>" . "<td>" . $row[5] . "</td>" . "<td>" . $row[6] . "</td>" . "<td>" . $row[7] . "</td>" . "<td>" . $row[8] . "</td>" . "<td>" . $row[9] . "</td>" . "<td>" . $row[10] . "</td>" . "<td>" . $row[11] . "</td>" . "<td>" . $row[12] . "</td>" . "<td>" . $row[13] . "</td>");
echo("</tr>");
*/
}
// echo("</table>");
?>
what I'm getting can be seen here

Are they missing or maybe they are obscured by unescaped html characters. Check the View Source option of your browser to see if they are actually there. I would especially watch for characters in the data like the Less Than character that the browser may be mistaking for an HTML open character.
You may need to escape your output so the browser does not try to render it:
echo ("" . htmlspecialchars($row[2]) . "");
Also, I would suggest you never take input directly from a user and put it into a SQL query without escaping it first. You are opening yourself up for SQL Injection attacks.
See the following:
http://php.net/manual/en/mysqli.real-escape-string.php

Don't know if it'll help but in this line:
$result = mysqli_query($con,"SELECT * FROM offers WHERE tradingCounty ='" . $SelectedCounty . "'ORDER BY categoryIdName ASC;");
it looks like you have an extra semi-colon (;) right before the last double quote. I don't think that should be there.
You could also store everything returned in an array and echo everything as you iterate through it to see what it returns. Then, if something's missing, go to your database and look at that row.
$tempArray = array();
while($row = mysqli_fetch_row($result)) {
$tempArray = $row;
}
foreach($tempArray as $value) {
echo $value . '<br>';
}

Related

Form submits multiple times with one button click, running the related action="" script multiple times

Here's what I'm trying to do:
I'm working on an inventory management system for my work, and the idea is that when we need to order something it's status is set to 'order' in the mySQL database which contains all of our inventory items. We have an 'Order Queue' page that displays all of the things that need ordered. The twist is that we need two different tables for each supplier: one for Purchase Orders and one for Request for Quotes (e.g. buying raw material isn't always a constant price, so a PO and RFQ need a different format). Then for each supplier table there is a submit button that says "Push supplier name PO/RFQ".
Pressing this button takes you to a second page which creates the PO and sends the email to the supplier. I know this isn't the issue here because regardless of what is in this script it will run three times when the button is pressed.
Here's some code:
$suppTable = mysqli_query($conn,"SELECT * FROM `suppliers` WHERE (`user` = '" . $user_login . "')");
This creates the array storing all the supplier info, and is all correct.
<div class="trackheader" style="border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;">
<h3 style="color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;">Display:</h3>
</div>
<div class="trackcont3" style="border-radius:0px 0px 5px 5px; border: 2px solid #CC3333; margin-bottom:20px;">
<form action="" method="POST" id="rec">
<?php
while($row1 = mysqli_fetch_assoc($suppTable)){
echo "<input type='checkbox' id='" . $row1['name'] . "' name='" . $row1['name'] . "' value='" . $row1['name'] . "' onchange='this.form.submit()'";
if(isset($_POST['' . $row1['name'] . ''])){
echo "checked='checked'";
}
echo ">";
echo "<label for='" . $row1['name'] . "'>" . $row1['name'] . "</label>";
}
?>
</form>
</div>
Please ignore the inline CSS - this will get added to the stylesheet once this actually works...
This is a pretty straightforward snippet: it creates display toggles for each supplier in the database so that they don't all show up at once.
Now for the fun stuff:
<?php
$loopTable = mysqli_query($conn,"SELECT * FROM `suppliers` WHERE (`user` = '" . $user_login . "')"); //this creates another array with each supplier tied to the user account
while($row2 = mysqli_fetch_assoc($loopTable)){ //this while loop creates two tables for each supplier: one with items that need to be ordered in a PO and another table for RFQ
if(isset($_POST['' . $row2['name'] . ''])){ //controls display from the above form
$display['' . $row2['name'] . ''] = "block";
} else {
$display['' . $row2['name'] . ''] = "none";
}
echo "<div style='display:" . $display['' . $row2['name'] . ''] . " ;'>";
echo "<div class='trackheader' style='border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;'>
<h3 style='color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;'>" . $row2['name'] . " PO</h3>
</div>";
echo "<div class='trackcont2'><table style='width:100%; !important'><tbody style='width:100%; !important'>
<tr style='width:100% !important;'>
<th style='width:16.6%;'><h3 style='color:grey !important;'>ALEX ID</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Description</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Supplier P/N</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Order QTY</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Price</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Total</th>
</tr>";
$i = 0;
$listTablePO = mysqli_query($conn,"SELECT * FROM `inventory` WHERE (`user` = '" . $user_login . "') AND (`supplier` = '" . $row2['name'] . "') AND (`status` = 'order') AND (`method` = 'PO')"); //this creates an array with all database items from the appropriate supplier which need ordered (and go in the PO table)
$total = 0; //tallies a running total of the price
while($row3 = mysqli_fetch_assoc($listTablePO)){ //this while loop creates the table of all parts that need ordered and their necessary info
if($i % 2 == 0){
echo "<tr class='odd' style='text-align: center;'>";
} else {
echo "<tr class='even' style='text-align: center;'>";
}
echo "<td>" . $row3['part_name'] . "</td>";
echo "<td>" . $row3['description'] . "</td>";
if($row3['order_link'] == null){
echo "<td>" . $row3['supplier_part_no'] . "</td>";
} else {
echo "<td><a href='" . $row3['order_link'] . "' target='_blank'>" . $row3['supplier_part_no'] . "</a></td>";
}
if($row['order_override'] == 0){
echo "<td>" . $row3['order_qty'] . "</td>";
$qty = $row3['order_qty'];
} else {
echo "<td>" . $row3['order_override'] . "</td>";
$qty = $row3['order_override'];
}
echo "<td>$" . sprintf('%.2lf', $row3['price']) . "</td>";
$total = ($total + ($qty * $row3['price']));
echo "<td>$" . sprintf('%.2lf', $total) . "</td>";
echo "</tr>";
$i++;
}
echo "</tbody></table></div>
<form action='/po-mailing/' method='POST' name='" . $row2['name'] ."PO'>
<input type='hidden' value='" . $row2['name'] . "' name='PO' id='PO'></input>
<button type='submit' class='addbutton' style='margin-top: -20px !important; margin-bottom: 20px;'>Push " . $row2['name'] . " PO</button>
</form>
</div>"; //the above snippet handles creating the appropriate form and submit button. All this needs to do is route to the appropriate page (/po-mailing/) and pass the correct supplier name.
//From there I can get all the necessary info from the SQL database and don't need to pass any other info through the form.
//I suspect this is where my issue lies, but all I need is an individual Push PO button for each supplier that directs to /po-mailing/ and passes the supplier info to the page. The problem here is it is doing it three times...
The next code is basically the same as above but for the RFQ table. Skip this part
// the same code is essentially repeated for RFQ for each supplier with minor format changes (it is also irrelevant as I have only been testing with the PO side)
echo "<div style='display:" . $display['' . $row2['name'] . ''] . " ;'>";
echo "<div class='trackheader' style='border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;'>
<h3 style='color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;'>" . $row2['name'] . " RFQ</h3>
</div>";
echo "<div class='trackcont2'><table style='width:100% !important;'><tbody style='width:100%; !important'>
<tr style='width:100% !important;'>
<th style='width:16.6%;'><h3 style='color:grey !important;'>ALEX ID</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Description</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Supplier P/N</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Order QTY</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Last Price</th>
<th style='width:16.6%;'><h3 style='color:grey !important;'>Estimate</th>
</tr>";
$i = 0;
$listTableRFQ = mysqli_query($conn,"SELECT * FROM `inventory` WHERE (`user` = '" . $user_login . "') AND (`supplier` = '" . $row2['name'] . "') AND (`status` = 'order') AND (`method` = 'RFQ')");
while($row5 = mysqli_fetch_assoc($listTableRFQ)){
if($i % 2 == 0){
echo "<tr class='odd' style='text-align: center;'>";
} else {
echo "<tr class='even' style='text-align: center;'>";
}
echo "<td>" . $row5['part_name'] . "</td>";
echo "<td>" . $row5['description'] . "</td>";
if($row5['order_link'] == null){
echo "<td>" . $row5['supplier_part_no'] . "</td>";
} else {
echo "<td><a href='" . $row5['order_link'] . "' target='_blank'>" . $row5['supplier_part_no'] . "</a></td>";
}
if($row['order_override'] == 0){
echo "<td>" . $row5['order_qty'] . "</td>";
$qty = $row5['order_qty'];
} else {
echo "<td>" . $row5['order_override'] . "</td>";
$qty = $row5['order_override'];
}
echo "<td>$" . sprintf('%.2lf', $row5['price']) . "</td>";
$total = ($total + ($qty * $row5['last_price']));
echo "<td>$" . sprintf('%.2lf', $total) . "</td>";
echo "</tr>";
$i++;
}
echo "</tbody></table></div>
<form action='/rfq-mailing/' method='POST' id='" . $row2['name'] ."RFQ'>
<input type='hidden' value='" . $row2['name'] . "' name='" . $row2['name'] . "' id='" . $row2['name'] . "'></input>
<button type='submit' class='addbutton' style='margin-top: -20px !important; margin-bottom: 20px;'>Push " . $row2['name'] . " RFQ</button>
</form>
</div>";
}
?>
Hopefully someone can spot where I'm screwing up.. FWIW I'm also using Wordpress as a backend for the system (just an easy way to handle accounts, create pages, etc)
I found the solution:
My wordpress theme appears to load the head of each page multiple times, so the issue wasn't with the code itself. On each load it was submitting the info.
Encasing necessary info in if(isset($_POST['submitbuttonexample'])){} fixed the issue.

PHP, trying to make row ID's work

So I basically been coding this quick stat shower for my game server, but I want it to ID numbers.
<html>
<head>
<meta charset="utf-8">
<title>ExileMod Stats</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
$con=mysqli_connect("******","******","******","******"); //server address, username, password, dbname
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//check order ascending or descending
if (isset($_GET["order"])) {
$sort = $_GET["order"];
if ($_GET["order"] == "asc"){
$sort = "desc";
$order = "asc";
}
else{
$sort = "asc";
$order = "desc";
}
}
//check filter
if (isset($_GET["name"])) {
$name = $_GET["name"];
}
else {
$name = "name";
}
$list=array('name', 'money', 'score', 'kills', 'deaths', 'uniform', 'vest', 'last_updated_at');
if (in_array($name,$list))
{
//variable ok
}
else
{
$name = "name";
}
$query = mysqli_query($con,"SELECT * FROM account a INNER JOIN player p ON a.uid = p.account_uid ORDER BY a.$name $order");
?><!--//echo "<table border='1'>-->
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<table class="table">
<tr>
<?php echo "<th>Player Name</th>";?>
<?php echo "<th>Money</th>";?>
<?php echo "<th>Score</th>";?>
<?php echo "<th>Kills</th>";?>
<?php echo "<th>Deaths</th>";?>
<?php echo "<th>Uniform</th>";?>
<?php echo "<th>Vest</th>";?>
<?php echo "<th>Last Updated</th>";?>
</tr>
<!--//";-->
<?php
while($row = mysqli_fetch_array($query))
{
?><tr class="danger"><?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
© HeroesOfGaming 2016 - 2017
</div>
</body>
</html>
So what I am trying to do is put numbers so it counts 1 2 3 4 5 6 7 to however many players are selected from the database? Hopefully this makes sense.
You can do like this
<?php
$i = 0; // <--- Added this
while($row = mysqli_fetch_array($query))
{
$i++; // <--- Added this
?><tr class="danger"><?php
echo "<td>". $i . "</td>"; // <--- Added this
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

PHP-Formatting table header

This code reads a CSV file for creating a table. It works fine.
How do I make the 1st row to be formatted in header style (from the table CSS)?
<table id="myTable" class="tablesorter animated fadeInDown"> <!-- cellspacing='0' is important, must stay -->
<tbody>
<?php
$lines = file('graphdata/IndicForTableVsOthers.csv');
foreach ($lines as $lineNum => $line) {
if($lineNum == 0) {
print " <tr id=\"tr" . $lineNum . "\">";
}
print " <tr id=\"tr" . $lineNum . "\">";
$tokens = str_getcsv($line);
print "<td style=\"width: 300px;\">" . trim($tokens[0]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[1]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[2]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[3]) . "</td>";
print "<td style=\"width: 100px;\">" . trim($tokens[4]) . "</td>";
print "</script>\n";
}
?>
</tbody>
This should work, I also changed some other parts who I think would have generated wrong marup.
<table id="myTable" class="tablesorter animated fadeInDown">
<?php
$lines = file('graphdata/IndicForTableVsOthers.csv');
foreach ($lines as $lineNum => $line) {
$cellType = ($lineNum == 0 ? "th" : "td");
$tokens = str_getcsv($line);
if ($lineNum == 0) echo "<thead>";
if ($lineNum == 1) echo "<tbody>";
echo "<tr id=\"tr" . $lineNum . "\">";
echo "<" . $cellType . " style=\"width: 300px;\">" . trim($tokens[0]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[1]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[2]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[3]) . "</" . $cellType . ">";
echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[4]) . "</" . $cellType . ">";
echo "</tr>";
if ($lineNum == 0) echo "</thead>";
}
if (count($lines) > 1) echo "</tbody>";
?>
</table>
If the first row is printed but it looks light and you want it Bold and Pretty so the user knows it's the header, add a class for convenience.
table.cs-contents tr:first-child td {
font-weight: bold;
text-align: center;
border-bottom: 2px solid #eaeaea;
}

Sort sql query & ID reset and start in numerical order every time I make a query or sort from <th> tag

I have a database called forms1 and a table named demo
The fields in the table are ID, Autore, Titolo, cit
I wish I had a first line that gives me the ability to sort alphanumeric values ​​that are retrieved by the query, such as
How can i modify my code?
This is cerca2.php:
<style>
br {margin-bottom:-10px;}
</style>
<form action="cerca2.php" method="post">
<b>Nome</b> <input type="text" name="Nome">
<b>Numero </b><input type="text" name="Numero">
<b>city </b><input type="text" name="city">
<input type="Submit">
</form>
<style>
tr:nth-of-type(odd) { background-color: AZURE; }
tr:nth-of-type(even) { background-color: CYAN; }
</style>
<style>
tr:hover{background-color<img src="images/smilies/biggrin.gif" border="0" alt="">EEPSKYBLUE;}
</style>
<?php
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='auto' bgcolor=”#7FFFD4″> <i>ID<i/></td>";
echo "<td width='auto' > <i>Nome<i/></td>";
echo "<td width='auto' ></td>";
echo "<td ></td>";
echo "</tr>";
define('DB_NAME', 'forms1');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can't use ' . DB_NAME . ': ' . mysql_error());
}
$Nome = str_replace(' ', '%', $_POST['Nome']);
$Numero = str_replace(' ', '%', $_POST['Numero']);
$city = str_replace(' ', '%', $_POST['city']);
$arNome = str_split($Nome);
$arNumero = str_split($Numero);
$arcity = str_split($city);
$Nome='';
foreach ($arNome as $value)
{
if ($value=='%') continue;
$Nome.=$value.'%';
}
$Numero='';
foreach ($arNumero as $value)
{
if ($value=='%') continue;
$Numero.=$value.'%';
}
$city='';
foreach ($arcity as $value)
{
if ($value=='%') continue;
$city.=$value.'%';
}
$sql = mysql_query("SELECT * FROM demo WHERE Autore LIKE '%$Nome%' AND Titolo LIKE '%$Numero%' AND cit LIKE '%$city%' ORDER BY Autore") or die(mysql_error());
while($row=mysql_fetch_array($sql)){
echo "<tr>";
echo "<td width='auto' bgcolor=”#FF0000 ″>" . " ". "<b>" . $row[0] . " ". "<b/>". "</td>";
echo "<td width='auto'>" . " " . $row[1] . " " . "</td>";
echo "<td width='auto'>". "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[2] . "<i/>". " " . "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[3] . "<i/>". " " . "</td>";
echo "</tr>";
}
mysql_close();
?>
I would click on Nome and other th (except ID) to reorder the values ​​alphanumeric AZ or ZA viceversa when i click. At the moment this is my result page (look code above) when i call a query:
I would, too - but I do not know how to do - that the ID value always start from 1 and continue in numerical order according to the number of x values ​​retrieved every time I make a query or sort from th tag. In my case, instead, is always dependent on the value it represents.
You see it from the picture that the numbers appear in random order.
Here, I do not want this.
Try this code-part:
$i = 0; while($row=mysql_fetch_array($sql)){
$i++;
echo "<tr>";
echo "<td width='auto' bgcolor=”#FF0000 ″>" . " ". "<b>" . $i . " ". "<b/>". "</td>";
echo "<td width='auto'>" . " " . $row[1] . " " . "</td>";
echo "<td width='auto'>". "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[2] . "<i/>". " " . "</td>";
echo "<td width='auto'>" . " ". "<i>" . $row[3] . "<i/>". " " . "</td>";
echo "</tr>";
}

Hiding a table if a search result does not exist

Exactly what the title says. I want the table containing all of the search queries to be hidden, but I've tried a lot of things and none of them work. For example, if($myData!=null) {proceed with showing the table}, but that didn't work. isset() also didn't work. Any ideas?
<style>
ul
{
list-style-type: none;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Chemicals</title>
</head>
<p>
<body>
<h3>Chemical Information</h3>
<p>You may search by Catalog number, CASRN, or the chemical name.</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
error_reporting(0);
if (isset($_POST['submit'])) {
if (isset($_GET['go'])) {
if (preg_match("/^[a-zA-Z0-9]+/", $_POST['name'])) {
$name = $_POST['name'];
$conn = mysql_connect("localhost", "Blimeo", "password");
$db = mysql_connect("localhost", "-", "-") or die('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysql_select_db("chemicals");
//-query the database table
$sql = "SELECT * FROM products WHERE Catalog LIKE '%" . $name . "%' OR CASRN LIKE '%" . $name . "%' OR Chemical_Name LIKE '%" . $name . "%'";
$myData = mysql_query($sql, $conn);
echo "<table border=1>
<tr>
<th>Catalog</th>
<th>Image</th>
<th>CASRN</th>
<th>Chemical Name</th>
<th>Quantity 1</th>
<th>Price 1</th>
<th>Quantity 2</th>
<th>Price 2</th>
<th>Quantity 3</th>
<th>Price 3</th>
<th>Quantity 4</th>
<th>Price 4</th>
</tr>";
while ($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['Catalog'] . "</td>";
echo "<td><img src=\"./img/" . $record['Image'] . "\" alt=\"Chemical\"/></td>";
echo "<td>" . $record['CASRN'] . "</td>";
echo "<td>" . $record['Chemical_Name'] . "</td>";
echo "<td>" . $record['Quantity1'] . "</td>";
echo "<td>" . $record['Price1'] . "</td>";
echo "<td>" . $record['Quantity2'] . "</td>";
echo "<td>" . $record['Price2'] . "</td>";
echo "<td>" . $record['Quantity3'] . "</td>";
echo "<td>" . $record['Price3'] . "</td>";
echo "<td>" . $record['Quantity4'] . "</td>";
echo "<td>" . $record['Price4'] . "</td>";
echo "</tr>";
echo "</form>";
echo "<ul>\n";
echo "<li>" . "" . $Catalog . " " . $CASRN . " " . $Chemical_Name . "</li>\n";
echo "</ul>";
}
}
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
}
?>
</body>
</html>
</p>
You should add mysql_num_rows();
$myData = mysql_query($sql, $conn);
$exists = mysql_num_rows($myData);
if($exists) {
echo "<table border=1>";
//..................
echo "</table>";
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
Well, it seems you are echoing out your table regardless of the results of the search query. You should probably check the number of rows returned in teh result set and only echo out the table if the count is > 0.
Use <div visibility="hidden"> to hide the table and use Javascript to change the visibility based on the search queries or some other condition.

Categories