Display data from database in HTML page [duplicate] - php

This question already has an answer here:
Show data pulled from database, based on html form input and display in html page
(1 answer)
Closed 6 years ago.
I would like to display data from my database on page load, but I don't know how and I didn't found any functional way. Inserting works fine.
Here is my HTML code for data inserting:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="snapname">Name:</label>
<input type="text" name="snapname">
</p>
<p>
<label for="age">Age:</label>
<input type="text" name="age">
</p>
<input type="submit" value="odeslat">
</form>
</body>
</html>
PHP for connect and insert data to database:
<?php
$servername = "localhost";
$username = "admin";
$password = "***";
$dbname = "db1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Could not connect to server: " . $conn->connect_error);
}
$first_name = mysqli_real_escape_string($conn, $_POST['snapname']);
$last_name = mysqli_real_escape_string($conn, $_POST['age']);
$Jmeno = $_POST['snapname'];
$Vek = $_POST['age'];
$sql = "INSERT INTO snapy (ID, username, age, date)
VALUES (0, '$Jmeno', '$Vek', CURRENT_TIMESTAMP)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Can someone help me with displaying data in HTML file?

You need to run a SELECT-request. Since you are using MySQLi you want to use something like:
$sql = "SELECT * FROM snapy";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["username"]. " " . $row["age"]. " yo<br>";
}
} else {
echo "0 results";
}
Found here: http://www.w3schools.com/php/php_mysql_select.asp

Related

How to update multiple rows in mysql and php with a different values using variables and different files?

I am trying to make a discord bot in PHP (which works, i know not the best choice of language but that is what i am most comfortable with for my own needs for now) with a dashboard.
The idea was to be able to add and edit the commands on the website.
I am selecting my current commands using mysql for the form like this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$disable = $row['Disabled'];
echo "<input type='hidden' name='id[]' value='";
echo $row['Id'];
echo "'>";
echo '<tr>';
echo '<td class="tg-515c"><input type="text" name="command[]" value="';
echo $row['Command'];
echo '" class="form-control"></td>';
echo '<td class="tg-wp8o"><input type="text" name="text[]" value="';
echo $row['Text'];
echo '" class="form-control"></td>';
echo '<td class="tg-wp8o"><select class="form-control" name="disabled[]">';
if($disable == '0')
{
echo "<option value='1'>Yes</option><option value='0' selected>No</option>";
}
else
{
echo "<option value='1' selected>Yes</option><option value='0'>No</option>";
}
echo "</select>";
echo '</td>';
echo '</tr>';
}
and i have the form outside of my php syntax.
That seems to be working fine.
// Create connection
$command = $_POST['command'];
$text = $_POST['text'];
$id = $_POST['id'];
$disable = $_POST['disabled'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="UPDATE dashboard SET Command='$command[0]', Text='$text[0]', Disabled='$disable[0]' WHERE Id='$id[0]'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
header("Location: /");
$conn->close();
I have just copy/pasted this code few times and changed the array from [0] to [1], etc.
This now works, however, i have another file which adds more commands to database, so i need it to be automatized each time i add a new command and not manually type for each input.
Edited the code to updated version.
I was able to figure out a solution by doing this:
<?php
$servername = "xxxt";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$command = $_POST['command'];
$text = $_POST['text'];
$id = $_POST['id'];
$disable = $_POST['disabled'];
$count = count($id);
for ($x = 0; $x <= $count; $x++) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE dashboard SET Command='$command[$x]', Text='$text[$x]', Disabled='$disable[$x]' WHERE Id='$id[$x]'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
header("Location: /");
?>
So i decided to do a for loop and count how many inputs i have, so for each input, it automatically creates a new query updating it.
I made an example for you that I think works for you and it works now and I tried it
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "createde";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// When you press the change button, it will completely change the data
if(isset($_POST["sub"])){
$command = $_POST['command'];
$text = $_POST['text'];
$id = "1";
$disable = "0";
$sql="UPDATE deew SET Command='$command', Text='$text', Disabled='$disable' WHERE id='$id'";
$conn->query($sql);
}
$conn->close();
?>
The second page is where the values are entered
<?php
include("sql.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="#" method="POST">
<textarea name="command" id="command" cols="30" rows="10"></textarea>
<input type="text" name="text" >
<input type="submit" name="sub">
</form>
</body>
</html>
I know that your code calls all the data and determines through them what data you want to change

php-sql code displays all data in the database instead of one

i have this php form that search my database for a particular user using a tracking number that was assigned to each user ( each user different tracking) and the form is suppose to display the particular user that i am searching for but it end up displaying all the database
this is the php code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "class";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ctracking, cname, cemail, crport, cdport, clocation, cdestination FROM demo";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "ctracking: " . $row["ctracking"]. " - Name: " . $row["cname"]. " - Email: " . $row["cemail"]. " - Port: " . $row["crport"]. " - Dport: " . $row["cdport"]." - Location: " . $row["clocation"]. " - Destination: " . $row["cdestination"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
this is the html form code
<html>
<head>
<title> Static Page</title>
</head>
<body>
<form action="making.php" method="post" />
<h1>Britchi Tracking</h1>
<p>
Costumers Name (required) <br/>
<input type="text" name="search" placeholder="Emmanuel John"' . '" size="70"/>
</p>
<p><input type="submit" name="csearch" value="Search"></p>
</form>
<form action="http://localhost/wordpress">
<input type="submit" value="Go to Home Page">
</form
</body>
</html>
i want the code to display all the details of a particular user not all the database
You need to receive variable
$track_num=$_POST['search'];
And apply in your query .
$sql = "SELECT ctracking, cname, cemail, crport, cdport, clocation, cdestination FROM demo WHERE ctracking='$track_num'";
NOTE:your code is open to sql injection attack use prepared statement.

Wordpress PHP FIle not running

I have a custom search form on my website with this code:
<center>
<form action="orderstatus.php" method="POST">
<input type="text" name="OrderLineNumber" value="">
<br>
<input type="submit" value="Submit">
</form>
</center>
And although I call orderstatus.php to action . When I press the submit button I get this Url : blablabla.de/action_page.php?OrderLineNumber=+23563623
This means instead of running orderstatus.php it's running action_page.php
My orderstatus.php is trying to print One Select Row from database like this :
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "*******";
$username = "*******";
$password = "*******";
$dbname = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM OrderStatus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() & fetch_assoc() = $_POST["name"] ) {
echo "<br> Order Line Number: ". $row["OrderLineNumber"]. " - Date Started: ". $row["Date Started"]. " " . $row["Status"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

Send Form Data to mysqli dtbs using php fails (xampp environment)

Here is a my html form code: The problem is that i can't figure out how to succesfuly submit the form to mysql dtbs using xampp. (Data aren't sent to dtbs).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" value="Submit">
</body>
</html>
and now my php code:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Guests (firstname)
VALUES ('?')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Data is not sent in the mysql dtbs! i've been trying for 2 days solving this but nothing... please help!
Kind regards, Thanos
$sql = "INSERT INTO Guests (firstname) VALUES ('?')";
'?' is to substitute in an integer, string, double or blob value.
You placed the '?', but forgot to prepare it using bind_param. More importantly, you have to pass $firstname value into $stmt->bind_param("s", $firstname);
Updated Code
$firstname = $_POST['firstname'];
$sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
$sql->bind_param("s", $firstname);
if ($sql->execute() === TRUE) {
Read
Prepared Statements in MySQLi
how to insert into mysql using Prepared Statement with php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" name="submitForm" value="Submit">
</body>
</html>
**test.php file**
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitForm'])){
$firstname = $_POST['firstname'];
$sql = "INSERT INTO Guests (firstname)
VALUES ('{$firstname}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}else{
echo "Are you sure you enter a firstname and the name of your html submit is submitForm";
}
$conn->close();
?>

Using a form to conduct a database search

I'm trying to use a simple form to run a database search. I'm not getting a database connection error so I'm pretty certain that's not the problem. I checked my target page for the echo display, but I'm not receiving any out put.
<!DOCTYPE HTML>
<html lang = "en">
<head>
<title>Tech Order Department.html</title>
<meta charset = "UTF-8" />
</head>
<body>
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="Search.php" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$servername = "localhost";
$username = "XXXXXXXXX"; // removed
$password = "XXXXXXXXX"; // removed
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//-select the database to use
$mydb=mysql_select_db("oldga740_SeniorProject");
//-query the database table
$sql="SELECT ID, FirstName, LastName FROM Staff WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$ID=$row['ID'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$FirstName . " " . $LastName . "</li>\n";
echo "</ul>";
}
}
else{
echo "<p>Please enter a search query</p>";
}
}
}
?>
</body>
</html>

Categories