Mysql table not displayed as html table in cloud 9 - php

hi I am trying to execute the following code on cloud 9
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test | Products</title>
</head>
<body>
<?php
$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM veg");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
when i am executing this code, i am getting just the following php code as result instead of the table contents:
Name Price Image "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['price'] . ""; echo "" . $row['image'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
can someone tell me whre i am going wrong?

chang this command:
$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");
To like this:
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");

Table display once instead of multiple times
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test | Products</title>
</head>
<body>
<?php
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM veg");
//create table
$table = "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
//bind rows
$table .= "<tr>";
$table .= "<td>" . $row['name'] . "</td>";
$table .= "<td>" . $row['price'] . "</td>";
$table .= "<td>" . $row['image'] . "</td>";
$table .= "</tr>";
}
//close table
$table .= "</table>";
//display table
echo $table;
mysqli_close($con);
?>
</body>
</html>

Related

when i search my database instead of getting results i get error 500

i have a website that is supposed to search the database for a specific id, but i get an error 500 when the submit button is clicked
i am running php7 and the latest mysql, ive tried removing un-needed code, ive tried changing the method i use to get information from the database, no luck
"dbh.inc.php"
<?php
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "chromebook";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection Failed: ".mysqli_connect_error());
}
"search.inc.php(destination page, shows results)"
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
echo "<h2>Search Results</h2>
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query("SELECT * FROM chrome WHERE Teacher LIKE '%search%'");
echo "<table class="table" border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
i expect the page to load with the results asked from the database, instead i get an "http error 500" message from my browser
thanks for any help in advance
First parameter of the mysqli_query should be $conn
search.inc.php
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
echo "<h2>Search Results</h2>";
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'");
echo "<table class='table' border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
echo "<h2>Search Results</h2> is incomplete; add the closing ";
Unclosed if statement at if(isset($_POST['search'])) {
Unescaped double quotes on echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>"; and echo "<table class="table" border='1'>
Also, I would highly recommend adding some error checking to your code so these 500 errors aren't a total mystery.
This works great for on page error reporting:
error_reporting(E_ALL);
ini_set('display_errors', 1);
So in total, replace your existing code with this:
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
// added error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "<h2>Search Results</h2>"; //fixed
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a href=\"myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit\"</a>"; // escape quotes
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'"); // connection param added
echo "<table class=\"table\" border='1'> // escape quotes
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
} // closing bracket from main 'if' statement
?>

Connect HTML files to WAMP database on another machine

So I'm developing a Raspberry Pi project that needs access to a database, however the database is stored locally on my laptop through WAMP. I have the Pi as a hotspot so that i can exchange files over and back through SSH however I'm struggling to get it to connect to the WAMP database to display the information that I want it to, what is the best way to do this? I've added my PHP and HTML file below.
HTML & PHP:
<?php
//Step 1
$db = mysqli_connect('localhost', 'root', '', 'hospital') or die('Error connecting to MySQL server.');
?>
<html>
<head>
<meta http-equiv="refresh" content="20">
</head>
<body>
<h1>
PHP connect to MySQL
</h1>
<table>
<?php
//Step 2
$query = "SELECT * FROM patients";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
echo "<table border = '1'>
<tr>
<th>id</th>
<th>patient_name</th>
<th>check_in_date</th>
<th>room_number</th>
<th>bed_number</th>
<th>notes</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['patient_name'] . "</td>";
echo "<td>" . $row['check_in_date'] . "</td>";
echo "<td>" . $row['room_number'] . "</td>";
echo "<td>" . $row['bed_number'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
//while($row = mysqli_fetch_array($result)){
// echo $row['id'] . ' ' .</tr> $row['patient_name']. ' ' . $row['check_in_date'] . ' ' . $row['room_number'] . ' ' . $row['bed_number'] . ' ' . $row['notes'] . '<br />';
//}
?>
</table>
</body>
</html>

Tables not showing in HTML database

I am creating a database and when creating the HTML script the table is coming back with no boarders = Firstname Lastname ";
while($row = mysqli_fetch_array($result)) {
echo "";
echo "" . $row['Firstname'] . "";
echo "" . $row['Lastname'] . "";
echo "";
}
echo "";
mysqli_close($con); ?>
Here is the code I created, could someone look at it and tell me what I have done wrong?
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caller_info");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
th>Franchise</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] . "</td>";
echo "<td>" . $row['Franchise'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Look at the tag
<th>Franchise</th>
Please change your file extension to .php it is not rendering your script. Html file not render your php, and also correct your markup as suggested by #suhail.

If functions per $row in a table

I have a table that shows me data from a call flow.
I need the Data from this table to be manipulated per row in such a way that all the values of my table, which are being looked up from my DB, (which are now in code) will be translated into a text value. Let me show U and explain:
My Table:
<?php
include_once "Connect2Martijn1.php";
?>
<link rel="stylesheet" href="CSSMartijn1.css">
</link>
<head>
<meta charset="iso-8859-1">
<meta name="description"content="VoizXL ">
<meta name="keywords"content="VoizXL ">
<meta name="author"content="Kenn Lo-A-Tjong">
</meta>
<title>Call Flow</title>
</head>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Call Flow</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Call Flow</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","","voizxl_wachtrij");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Callflow");
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $row['statusAnswered'] . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Example of (how I want to be) Translating the Data:
<?php
if ($row['statusAnswered'] ="NULL")
{
echo "Not Answered!";
}
else
{
echo "Answered!";
}
?>
What I want to Achieve is for eg. that the value in this table from $row['statusAnswered'] will be displayed in text as "Answered or Not Answered" if the Value of this row in the DB is NULL or Not...
How do I do this?
Right now I can only achieve to have 1 echo under the table saying Answered :S
No Idea how to put it per $row.
while($row = mysqli_fetch_array($result))
{
if (!isset($row['statusAnswered']))
{
$answered = "Not Answered!";
}
else
{
$answered = "Answered!";
}
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $answered . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
if ($row['statusAnswered'] =="NULL"||$row['statusAnswered'] =="Null" || $row['statusAnswered'] =="null" || $row['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . ($row['statusAnswered']=='NULL'?'Not Answered!':'Answered!') . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
You could use;
$checkstatus = if($row['statusAnswered'] = NULL) { echo "Not Answered!"; } else {echo "Answered!";};
then call that by replacing;
echo "<td>" . $row['statusAnswered'] . "</td>";
with;
echo "<td>{$checkstatus}</td>"

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