I am new to using database with php, using 000webhost I don't know what to write to get data from specific table
connection.php
<?php
$servername = "localhost";
$username = "*****************";
$password = "***********";
$database = "*****************";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
getalldata.php
<?php
include 'connection.php';
//get data from Database
?>
Let suppose you are fetching records from studentrecord
require_once 'connection.php';
$roll = $_POST['roll'];
$query = "SELECT * from studentrecord where roll = '$roll'";
try{
$statement = $conn->query($query);
echo "<table border='1'>
<tr>
<td>Roll</td>
<td>Stream</td>
<td>Session</td>
<td>Name</td>
<td>Mother Name</td>
<td>Father Name</td>
<td>Total Mark's</td>
<td>Grade</td>
<td>Result</td>
</tr>";
//if you want to fetch records as an array indexed
while ($result = $statement->fetch()){
echo "<tr>";
echo "<td>" . $result['id'] . "</td>";
echo "<td>" . $result['stream'] . "</td>";
echo "<td>" . $result['session'] . "</td>";
echo "<td>" . $result['name'] . "</td>";
echo "<td>" . $result['mother_name'] . "</td>";
echo "<td>" . $result['father_name'] . "</td>";
echo "<td>" . $result['total_marks'] . "</td>";
echo "<td>" . $result['grade'] . "</td>";
echo "<td>" . $result['result'] . "</td>";
echo "</tr>";
}
//if you want to fetch records as an anonymous object
while ($result = $statement->fetchObject()){
echo "<tr>";
echo "<td>" . $result->id . "</td>";
echo "<td>" . $result->stream . "</td>";
echo "<td>" . $result->session . "</td>";
echo "<td>" . $result->name . "</td>";
echo "<td>" . $result->mother_name . "</td>";
echo "<td>" . $result->father_name . "</td>";
echo "<td>" . $result->total_marks . "</td>";
echo "<td>" . $result->grade . "</td>";
echo "<td>" . $result->result . "</td>";
echo "</tr>";
}
echo "</table>";
}
catch (PDOException $ex) {
echo "Failed to retrieve record".$ex->getMessage();
`enter code here`}
Related
I need to retrieve the color hex code stored in a database and display it as seen on screen within a table cell. i'm trying to find a way to convert the hex code the db query returns for that specific table cell into the actual color that the hex denotes. Here's my code...
<?php
// Include config file
require_once "config.php";
$pdo = new PDO($dsn, $username, $password, $options);
// Attempt select query execution
$sql = "SELECT * FROM sales";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>S/N</th>";
echo "<th>Transaction Date</th>";
echo "<th>Customer Name</th>";
echo "<th>Address</th>";
echo "<th>Phone Number</th>";
echo "<th>Vehicle Model</th>";
echo "<th>Vehicle Chassis Number</th>";
echo "<th>Vehicle Registration Number</th>";
echo "<th>Vehicle Color</th>";
echo "<th>Amount Paid</th>";
echo "<th>Advance</th>";
echo "<th>Balance</th>";
echo "<th>Balance Due Date</th>";
echo "<th>Paid To</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['transactiondate'] . "</td>";
echo "<td>" . $row['customername'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['vehiclemodel'] . "</td>";
echo "<td>" . $row['vehiclechassisnumber'] . "</td>";
echo "<td>" . $row['vehicleregistrationnumber'] . "</td>";
echo "<td>" . $row['vehiclecolor'] . "</td>";
echo "<td>" . $row['amountpaid'] . "</td>";
echo "<td>" . $row['advance'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>" . $row['balancedate'] . "</td>";
echo "<td>" . $row['paidto'] . "</td>";
echo "<td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
It is a bit unclear about where you wish to use the colour but I'm assuming it is here. And also my assumption is the retrieved value for vehiclecolor column is a HEX value
echo "<td>" . $row['vehiclecolor'] . "</td>";
You can use tag property 'background-color' for this.
$temp = $row['vehiclecolor'];
echo "<td style='background-color:" . $temp ."'></td>";
Pretty self-explanatory I guess. String concatenation.
I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down but I cannot seem to get all of the information to go into the table.
The formatting will only allow one row to be entered into the table and then the rest of the row go below in normal text format.
How would I go about getting all the information to sit in the table and automatically update when more information is added?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
$date = $rows['date'];
$region = $rows['region'];
$cameraman = $rows['cameraman'];
$livestream = $rows['livestream'];
$eventtitle = $rows['eventitle'];
$lecturer = $rows['lecturer'];
$time = $rows['time'];
$speaker = $rows['speaker'];
$icecontact = $rows['icecontact'];
$venuecontact = $rows['venueaddress'];
$venuecontact = $rows['venuecontact'];
$additionalcomments = $rows['additionalcomments'];
$onapp = $rows['onapp'];
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
echo "</table>";
endwhile;
?>
http://cpdonline.tv/spreadsheet/spreadsheet.php
You made two boo boo's in your code:
1: You are not using these variables, delete them
$date = $rows['date'];
$region = $rows['region'];
$cameraman = $rows['cameraman'];
$livestream = $rows['livestream'];
$eventtitle = $rows['eventitle'];
$lecturer = $rows['lecturer'];
$time = $rows['time'];
$speaker = $rows['speaker'];
$icecontact = $rows['icecontact'];
$venuecontact = $rows['venueaddress'];
$venuecontact = $rows['venuecontact'];
$additionalcomments = $rows['additionalcomments'];
$onapp = $rows['onapp'];
2: You are closing the table inside of the while loop
echo "</table>";
endwhile;
Should be:
endwhile;
echo "</table>";
I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down into the table. Unfortunately the table is adding an additional row to the bottom of the table making 4 rows when in fact there is only 3 rows displaying in the database.
Any idea as to why?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
endwhile;
echo "</table>";
?>
I have added the link to the page below.
http://cpdonline.tv/spreadsheet/spreadsheet.php
Update your while loop with the following:
WHILE($rows = mysql_fetch_array($query)):
$rows = array_filter($rows);
if (!empty($rows)) {
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
}
endwhile;
echo "</table>";
array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false.
It seems that you have empty data in your database, as many lines are empty
Check your database for empty records or run the query
DELETE FROM table_name WHERE column='';
This question already has an answer here:
How to extract only columns which has non zero values in mysql and php?
(1 answer)
Closed 7 years ago.
Above image is the entry of data in mysql database.
Above image was the output what i am getting is,
But i need to display only non zero fields as the output that means from "sample revived" column to "library qc" column only.
php script is
<?php
$userinput1 = $_POST['soid'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "status";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1' ") or die(mysqli_error
($conn));
echo "<p><font size= 4>SO_NUMBER:$userinput1";
echo "<table border='1'>
<tr>
<style>
th{
color: blue;
}
td{
color: black;
}
</style>
<th>Sample Recived</th>
<th>Mol-Bio Extraction</th>
<th>Extraction QC</th>
<th>Library Prep</th>
<th>Library QC</th>
<th>Sequencing</th>
<th>Data Check</th>
<th>RE Sequencing</th>
<th>QC Check</th>
<th>Analysis Started</th>
<th>Analysis Completed</th>
<th>Report</th>
<th>Outbound</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<br />";
echo "Department:".$row['dept'] ;
echo "<td>" . $row['samplerecived'] . "</td>";
echo "<td>" . $row['molbioextraction'] . "</td>";
echo "<td>" . $row['molbioextractionqc'] . "</td>";
echo "<td>" . $row['libraryprep'] . "</td>";
echo "<td>" . $row['libraryqc'] . "</td>";
echo "<td>" . $row['sequencing'] . "</td>";
echo "<td>" . $row['datacheck'] . "</td>";
echo "<td>" . $row['resequencing'] . "</td>";
echo "<td>" . $row['qccheck'] . "</td>";
echo "<td>" . $row['analysisstarted'] . "</td>";
echo "<td>" . $row['analysiscompleted'] . "</td>";
echo "<td>" . $row['report'] . "</td>";
echo "<td>" . $row['outbound'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
inside your while loop use for each field like this (use like this for all fields):
while($row = mysqli_fetch_assoc($result))
{
if($row['samplerecived']=="0000-00-00") $row['samplerecived']="";
echo "<tr>";
echo "<br />";
echo "Department:".$row['dept'] ;
echo "<td>" . $row['samplerecived'] . "</td>";
echo "<td>" . $row['molbioextraction'] . "</td>";
echo "<td>" . $row['molbioextractionqc'] . "</td>";
echo "<td>" . $row['libraryprep'] . "</td>";
echo "<td>" . $row['libraryqc'] . "</td>";
echo "<td>" . $row['sequencing'] . "</td>";
echo "<td>" . $row['datacheck'] . "</td>";
echo "<td>" . $row['resequencing'] . "</td>";
echo "<td>" . $row['qccheck'] . "</td>";
echo "<td>" . $row['analysisstarted'] . "</td>";
echo "<td>" . $row['analysiscompleted'] . "</td>";
echo "<td>" . $row['report'] . "</td>";
echo "<td>" . $row['outbound'] . "</td>";
echo "</tr>";
}
Use typecasting operator int() for the respective fields.
$var = (int) $var ? $var : '';
Explanation:
An empty date 0000-00-00 00:00:00 always return 0 which is blank/empty.
In above code, it will return 0 hence, it will not display blank/empty date.
Only dates with some non-zero values will be displayed.
Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. At present I have an order form which stores the order in my database and shows it on a table in my admin area. I would like to be able to delete the row from the table and my database when I have completed the order.
At present my code looks like this:
<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//deleterow.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: 87229feely.php");
?>
Any help? All greatly appreciated. Thanks
This answer does not meet security standards but should do the job:
<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>
Many ways to do this. Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it)
Create a form around the table
Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/">
Add some processing script before the table is being parsed
if (isset($_POST['submit'])) {
$sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
mysql_query($sql);
}
First of all ,you need to send ID of completed order to next form. You cam do this by adding:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
That will create a hidden field with value of ID.
If your form uses POST:
then:
if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}
If you are using GET method:
<form method="GET">
You could either use hidden field as mentioned above or you could parse ID of order in GET url:
<form action='action.php?id=".$row['id']."'>
and after submitting:
if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}
Maybe something like this with PDO
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </th>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$row = $conn->query('SELECT * FROM orderform');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>"
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "</tr>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>
<input type="submit" name="submit" value="submit" />
</form>
delete.php
<?php
$id
if(isset($_POST['submit']) {
include('connection.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
$stmt->exec($sql);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}