Information not showing up my MySQL Database - php

I'm trying to open a new php page from the sNumber and display the data from the student table on student profile page from the sNumber. But I can't retrieve the data, it goes right to the error. Any help will be appreciated. Thanks
studentlist.php
<div class="memtable">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>Last Name</th> <th>First Name</th> <th>School</th> <th>Snumber</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
$lastName = "<a href = 'studentprofile.php?id= " .mysql_result($result, $i, 'sNumber'). "'>" . mysql_result($result, $i, 'lastName') . "</a>";
echo "<tr " . $cls . ">";
echo '<td>' . $lastName . '</td>';
echo '<td>' . mysql_result($result, $i, 'firstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'school') . '</td>';
echo '<td>' . mysql_result($result, $i, 'sNumber') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
studentprofile.php
<?php
include('phpdocs/connect.inc.php');
include('header.php');
if ( isset( $_GET[ "sNumber" ] ) )
$student_sNumber = $_GET['sNumber'];
$getStudentInfo = " SELECT sNumber FROM student WHERE student.sNumber = " . $student_sNumber;
?>
<!DOCTYPE html>
<html>
<head>
<title>Student</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="transoverlay">
<?php
if ($result = mysql_query($getStudentInfo)) {
/* fetch associative array */
while ($row = mysql_fetch_assoc($result)) {
echo "<h1 class='tv'>" . $row["sNumber"]. ", ". $row['firstName']."</h1>";
}
mysql_free_result($result);
}else{
echo "<div class='tv'>Student Data could not be listed. </div>";
}
?>
<hr color="#1a1a1a">
</div>
</body>
<?php include('footer.php');?>
</html>

the url uses id but you checking for sNumber change one of those
you need to quote student number in the query as its a string
$getStudentInfo = "SELECT sNumber FROM student WHERE student.sNumber ='". $student_sNumber."'";

Related

php search script in mysql database dosen't work

I am trying to develop a program that search in mysql database and return the search results but in when I hit the submit it displays all the results I don't know why this is happening, so any help will be appreciated
<?php
include('connect2.php');
?>
<?php
echo "<center>";
echo "<table border = '3'>";
echo "<thead>";
echo "<tr>";
echo "<th><u>id</u></th>";
echo "<th><u>name</u></th>";
echo "<th><u>countrycode</u></th>";
echo "<th><u>district</u></th>";
echo "<th><u>population</u></th>";
echo "</center>";
echo "</tr>";
echo "</thead>";
$name = isset($_POST['search']) ? $_POST['search'] : '';
$result = mysqli_query($conn, "SELECT id, name, district, population, countrycode FROM city WHERE concat(id, name, district, population, countrycode) LIKE '%$name%' ");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td><td>" . $row['countrycode'] . "</td><td>" . $row['district'] . "</td><td>" . $row['population'] . "</td></th>";
}
}
else {
header( "Location:error page.html" ); die;
}
echo "</table>";
mysqli_close($conm);
?>
the search page search.php
<?php
include('connect2.php') ;
?>
<!DOCTYPE html>
<head>
<title>city results</title>
<link href="style-table.css" rel="stylesheet">
<link href="animate.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script>
function validateform() {
var x = document.forms["myform"]["input"].value;
if (x == "") {
alert("input must be filled out");
return false;
}
}
</script>
</head>
<body>
<form method="POST" action="display-results-city.php" name="myform" onsubmit="return validateform()">
<input type="text" name="input" placeholder="search.........">
<button name="submit" type="submit">go</button>
</form>
<?php
echo '<div class="animated fadeInUp">';
echo '<i class="fa fa-arrow-left fa-lg" aria-hidden="true" ></i>';
echo "<div id='records'>";
echo "<table border = '0'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>name</th>";
echo "<th>countrycode</th>";
echo "<th>district</th>";
echo "<th>population</th>";
echo "</tr>";
echo "</div>";
echo '</div>';
$sql = "select * from city limit 50";
$result = $conn->query($sql);
if( $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['ID']. "</td><td>" . $row['Name']. "</td><td>" . $row['CountryCode'] . "</td><td>" . $row['District'] . "</td><td>" . $row['Population'] . "</td></tr>";
}
} else {
echo " 0 results";
}
echo '</table>';
$conn->close();
?>
</body>
</html>
Try running this query..
$result = mysqli_query($conn, "SELECT id, name, district, population, countrycode FROM city WHERE UPPER(concat(id, name, district, population, countrycode)) LIKE UPPER('%$name%') ");
It will match both "Name" and "name".

Dynamic table with php pdo and two foreach

So, I'm connected to a database and I'm pulling data from two different tables within same database. I'm running foreach to generate rows, but the table renders not the way I want. I want the 2nd foreach to create a 2nd column to the right of the first one and all those dynamic rows to go downward.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/db.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row text-center">
<h3>Schedule of Ms. Kim & Ms. Morly</h3>
</div>
<?php
include 'database.php';
$pdo = Database::connect();
//Start of table Kim - ID=10
$sql = 'SELECT * FROM kim ORDER BY id';
$q = $pdo->prepare($sql);
$q->execute(array($schd_tut_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$sql2 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=10 ';
$q2 = $pdo->prepare($sql2);
$q2->execute(array($tut_profile_id));
$data2 = $q2->fetch(PDO::FETCH_ASSOC);
$imgkim = $data2['tut_img'];
//End of Table Kim
//Start of table Morly - ID=16
$sql3 = 'SELECT * FROM morly ORDER BY id';
$q3 = $pdo->prepare($sql3);
$q3->execute(array($schd_tut_id));
$data3 = $q3->fetch(PDO::FETCH_ASSOC);
$sql4 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=16 ';
$q4 = $pdo->prepare($sql4);
$q4->execute(array($tut_profile_id));
$data4 = $q4->fetch(PDO::FETCH_ASSOC);
$imgmorly = $data4['tut_img'];
//End of table Morly
?>
<table class="table table-striped table-bordered">
<thead>
<th style="width:20px;">Time</th>
<th><img class="img-responsive" src="<?php echo $imgkim ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
</thead>
<tbody>
<?php
//tutor kim
foreach ($pdo->query($sql) as $row ) {
echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
}
//echo '</tr>';
//tutor morly
foreach ($pdo->query($sql3) as $row ) {
//echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
Something like this should work, might need a bit of fiddeling.
Basically just store the values in an array and then print the array.
<?php
$row1 = [];
$row2 = [];
//table 1
foreach ($pdo->query($sql) as $row ) {
$row1[] = $row['status'];
}
//table 2
foreach ($pdo->query($sql2) as $row ) {
$row2[] = $row['status'];
}
echo "<table>";
foreach ($row1 as $key => $row) {
echo "<tr><td>" . $row[$key] . "</td><td>" . $row2[$key] . "</td></tr>";
}
echo "</table>";
Database::disconnect();
?>

Adding products to shopping cart results in error

My issue is that when I try to access my cart after adding products to it I keep getting the same error,
"mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
given"
with this line of code:
$itemnumrows = mysqli_num_rows($itemsresult);
<?php
function showcart()
{
require('connect.php');
if (isset($_SESSION['SESS_ORDERNUM'])) {
if (isset($_SESSION['SESS_LOGGEDIN'])) {
$custquery = "SELECT id, status from orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2;";
$custresult = mysqli_query($con, $custquery);
$custrow = mysqli_fetch_assoc($custresult);
$itemsquery = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id =products.id AND order_id = " . $custrow['id'];
$itemsresult = mysqli_query($con, $itemsquery);
$itemnumrows = mysqli_num_rows($itemsresult);
} else {
$custquery = "SELECT id, status from orders WHERE session = '" . session_id() . "' AND status < 2;";
$custresult = mysqli_query($con, $custquery);
$custrow = mysqli_fetch_assoc($custresult);
$itemsquery = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id = products.id AND order_id = " . $custrow['id'];
$itemsresult = mysqli_query($con, $itemsquery);
$itemnumrows = mysqli_num_rows($itemsresult);
}
} else {
$itemnumrows = 0;
}
if ($itemnumrows == 0) {
echo "You have not added anything to your shopping cart yet.";
} else {
echo "<table cellpadding='10'>";
echo "<tr>";
echo "<td></td>";
echo "<td><strong>Item</strong></td>";
echo "<td><strong>Quantity</strong></td>";
echo "<td><strong>Unit Price</strong></td>";
echo "<td><strong>Total Price</strong></td>";
echo "<td></td>";
echo "</tr>";
while ($itemsrow = mysqli_fetch_assoc($itemsresult)) {
$quantitytotal = $itemsrow['price'] * $itemsrow['quantity'];
echo "<tr>";
if (empty($itemsrow['image'])) {
echo "<td><img src='productimages/dummy.jpg' width='50' alt='" . $itemsrow['name'] . "'></td>";
} else {
echo "<td><img src='productimages/" . $itemsrow['image'] . "' width='50' alt='" . $itemsrow['name'] . "'></td>";
}
echo "<td>" . $itemsrow['name'] . "</td>";
echo "<td>" . $itemsrow['quantity'] . "</td>";
echo "<td><strong>£" . sprintf('%.2f', $itemsrow['price']) . "</strong></td>";
echo "<td><strong>£" . sprintf('%.2f', $quantitytotal) . "</strong></td>";
echo "<td>[<a href='delete.php?id=" . $itemsrow['itemid'] . "'>X</a>]</td>";
echo "</tr>";
#$total = $total + $quantitytotal;
$totalquery = "UPDATE orders SET total = " . $total . " WHERE id = " . $_SESSION['SESS_ORDERNUM'];
$totalresult = mysqli_query($con, $totalquery);
}
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>TOTAL</td>";
echo "<td><strong>£" . sprintf('%.2f', $total) . "</strong></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
echo "<p><a href='checkout.php'>Go to the checkout</a></p>";
}
}
?>
Heres my showcart page, also getting the same error with this line:
$numrows = mysqli_num_rows($result);
<?php
session_start();
require("header.php");
require("functions.php");
echo "<h1>Your shopping cart</h1>";
showcart();
if (isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
$query = "SELECT * FROM orderitems WHERE order_id = " . $_SESSION['SESS_ORDERNUM'] . ";";
$result = mysqli_query($con, $query);
$numrows = mysqli_num_rows($result);
if ($numrows >= 1) {
echo "<h2><a href='checkout.php'>Go to the checkout</a></h2>";
}
}
require("footer.php");
?>
My header page as well, when I click the viewcart link it outputs same errors as above blocks of code..
<?php
if (!isset($_SESSION)) {
session_start();
}
if (isset($_SESSION['SESS_CHANGEID']) == TRUE) {
session_unset();
session_regenerate_id();
}
include("connect.php");
?>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="header">
<h1 style="color:#ffffff"> <?php echo $site; ?> </h1>
</div>
<div id="menu">
Home
View Basket/Checkout</div>
<div id="container">
<div id="bar">
<?php
include("bar.php");
echo "<hr />";
if (isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
echo "Logged in as <strong>'" . $_SESSION['SESS_USERNAME'] . "'</strong>[<a href='" . $access . "logout.php'>logout</a>]";
} else {
echo "<a href='" . $access . "login.php'>Login</a>";
} ?></div>
<div id="main">

Populate my drop down list with tables and not rows in php?

Hello i am able to use a drop down list to show rows from a table but what i am wanting to is to populate the drop down list with the 2 tables i have in my database.
Example: i am currently listing members from the table members
bob
jake
chris
but what i am wanting to do is to list the tables
members
cars
<?php
// check for errors
ini_set('display_errors', 1);
//calls connection
require_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1> View Table(s) </h1>
<?php
// calls get results method to list members
$ResultSet = getResults("members");
echo "<table border='1' cellpadding='6'>";
echo "<tr> <th>Id</th> <th>First Name</th> <th>Second Name</th> <th>Age</th> <th>Email</th>";
foreach ($ResultSet as $row) {
echo "<tr>";
echo "<td>" . $row ['id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['second_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "<table>";
?>
<br/>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p>Choose a member to view:</p>
<select name='members' id="ddmembers">
<?php
$results = getResults('members');
if ($results) {
foreach ($results as $row) {
echo '<option value="' . $row['id'] . '">' . $row['first_name'] . '</option>';
}
}
else
echo '<option value="0"0"> No Data</option>';
?>
</select>
<input type="submit" id="submit" value="Submit"/>
<br/>
<br/>
</form>
<?php
//Message for what has been selected
if (isset($_POST['members'])) {
echo 'You have selected Member ' . $_POST['members'];
}
?>
</body>
</html>
Try using information_schema database
See the following post for more information
Get table names using SELECT statement in MySQL
EDIT :
Without using information_schema, you can do something like
<?php
$dbname = "DATABASE_NAME";
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
$tableNames= array();
while ($row = mysql_fetch_row($result)) {
$tableNames[] = $row[0];
}
echo '<select name="tables" id="tables">';
foreach ($tableNames as $name){
echo '<option value="' . $name . '">' . $name . '</option>';
}
echo '</select>';
?>
2 times action :
Save all names into array
Iterate this array and print options
Of course, if you prefer, you can do this with 1 time action

The Next iteration

I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.

Categories