Draw a link from the database and add it to the HTML - php

I want to make a page where the element will redirect the user to a random page from the database.
I made the HTML code, but I can not deal with PHP and MySQL - I was able to connect to the database, but any attempt to connect the MySQL code with the tag ended in a loss.
Can I count on help writing a PHP script and linking code with ?
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat+Subrayada" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">
<title>Random it</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header></header>
<div class="random">
Random
</div>
<?php
require_once "connect.php";
$conn = #new mysqli($host, $db_user, $db_password, $db_name);
$sql = "SELECT link FROM randomit ORDER BY RAND()";
?>
<footer></footer>
</body>
</html>

Let's try this:
Put this PHP part at the top of the page;
<?php
// set a default link in case something goes wrong
$DEFAULT_LINK = "default.html";
// connect to DB
require_once "connect.php";
$conn = new mysqli($host, $db_user, $db_password, $db_name);
// set query -> get 1 result only with LIMIT 1
$sql = "SELECT link FROM randomit ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $sql);
if ($result !== false)
$row = mysqli_fetch_assoc($result);
else
$row = false;
// if we have a valid result => use it
// else => use the default
if ($row && isset($row["link"]))
$RANDOM_LINK = $row["link"];
else
$RANDOM_LINK = $DEFAULT_LINK;
?>
And now the HTML part:
<!DOCTYPE html>
<html>
<!-- head part... -->
<body>
<header></header>
<div class="random">
Random
</div>
<footer></footer>
<!-- etc... -->

Related

Can't call dynamic page with $_GET

I'm trying to use $_GET to call a dynamic page, but it throws "Error". Do I need to call page 1 from page 2 or why can't they talk to each other?
Thanks in advance for your help, supposingly there's an easy solution.
/Jimmy
This is page 1:
<!DOCTYPE html>
<html>
<head>
<title>Players</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php"); ?>
<?php
$servername = "localhost";
$username = "jim";
$password = "pass";
$dbname = "jim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL search
$sql = "SELECT PlayerID, Person FROM People where PlayerID is not null ORDER BY Person";
$result = $conn->query($sql);
// Condition
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Player</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>"."<a href='spelarfakta.php?=id{$row['PlayerID']}'>{$row['PlayerID']}</a>"."</td>"."<td>".$row["Person"]."</td>";
}
echo "</table>";
} else {
echo "No hits";
}
$conn->close();
?>
</body>
</html>
This is page 2:
<!DOCTYPE html>
<html>
<head>
<title>Player Stats</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php");
if (isset($_GET['id'])) {
echo '<p>'. $_GET['id'].'</p>';}
else {echo 'Error';
}
?>
</body>
</html>
Correct solution as a comment:
=id should be id=

Getting data from SQL to PHP

I have a data-table in mySQL and I need help accessing the information to display on an HTML Page.
Here are some details.
Host: 127.0.0.1:8889
username: root
password: root
database-name: gibsonek
table-name: events
Here is my code:
<html>
<head>
<meta charset="UTF-8">
<title>Gibson Ek Schedule</title>
<!--JQuery Add-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!--My JS Add-->
<script src="script.js"></script>
<!--Normalize CSS Add-->
<link rel="stylesheet" href="css/normalize.css">
<!--Google Font - Open Sans - Add -->
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600'>
<!--Bootstrap Add-->
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
<!--My CSS Add-->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="navbar">
<span>Gibson Ek Schedule</span>
</div>
<div class="header">
<div class="color-overlay">
<div class="day-number"></div>
<div class="date-right">
<div class="day-name"></div>
<div class="month"></div>
</div>
</div>
</div>
<div class="timeline">
<ul id = "l">
<?php
$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');
$query = "SELECT * FROM `events` WHERE 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<p>SQL DATA WILL GO IN HERE</p>";
}
mysql_close(); //Make sure to close out the database connection
?>
</ul>
</div>
</div>
</body>
</html>
Please use PDO (it's more flexible and safe)
$servername = "127.0.0.1:8889";
$username = "root";
$password = "root";
$dbname = "gibsonek";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `events` WHERE id='1'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
var_dump($result); // or as you like
There is a good article.
There is a best guide.
<div class="timeline">
<ul id = "l">
<?php
$host = "127.0.0.1:8889";
$username = "root";
$password = "root";
$db_name = "gibsonek";
$mysqli = new mysqli($host, $username, $password, $db_name);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '.$mysqli->connect_error);
}
$query = "SELECT * FROM `events` WHERE id='1'"; // Here if 1 means id
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
?>
<!-- Here You can add any HTML or css -->
<li><?php echo $row["column"]; ?> </li> // write your column name what you want to show
<?php
}
?>
</ul>
</div>
I think that would be clear for you, leave a comment if any query.
Thanks
I think you missed something
here is correct code:
<?php
$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');
$query = "SELECT * FROM `events` WHERE id(or any column as u want)='1'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
print_r($row);
}
mysql_close(); //Make sure to close out the database connection
?>

PHP Basic Random Quote Generator. How do I prevent quotes that have appeared recently?

EDIT: Thank you for correcting the formatting... did not notice that mistake
I have completed this basic quote generator as a first project, but have noticed that it frequently displays the same quote upon refresh. Granted, there are only five at the moment, but this problem might still be apparent as I fill the database. I am currently learning JQuery AJAX for a more advanced one - but am only starting. To my knowledge, this is not a duplicate question. Displays database query upon reload. Thank you!
Here is the code:
connect.php:
<?php
$connection = new mysqli('localhost', 'root', '', 'random_quotes');
if ($connection->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ')'
. $mysqli->connect_error);
}
?>
functions.php:
<?php
require ('connect.php');
$query = "SELECT id, quote, author FROM quotes ORDER BY RAND() LIMIT 1";
$getQuote = $connection->query($query);
?>
HTML:
<!doctype html>
<html lang="en">
<?php
require ('includes/connect.php');
require ('includes/functions.php');
?>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Candal' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container-fluid text-center">
<h1>Random Quote Generator</h1>
<p>Some of my favourite all around quotes!</p>
<br/>
<button class="btn btn-default" onclick="newQuote()" type="submit">New Quote
</button>
<div class="quote_wrap text-center">
<span class="quote">
<?php
while($row = $getQuote->fetch_assoc()){
$stringID = $row['id'];
$stringQuote = $row['quote'];
$stringAuthor = $row['author'];
echo $stringQuote;
}
?>
</span>
</br>
<div class="author text-center"><?php echo $stringAuthor?></div>
<div class="quoteid" id="<?php echo $stringID?>"></div>
</div>
</div>
<script src="https: /ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript">
function newQuote() {
location.reload();}
</script>
</body>
</html>
Generate a random number and then access quote by that id. May work better..
$query = "SELECT COUNT(*) as noQuotes FROM quotes ";
$result = $connection->query($query);
$row = $result->fetch_assoc();
$noQuotes = $row["noQuotes"];
srand(time());
//echo(rand(1,$noQuotes));
$randNo = rand(1,$noQuotes);
$query = "SELECT id, quote, author FROM quotes WHERE id = $randNo ";
$getQuote = $connection->query($query);

Trying to get a PHP button to delete MySQL rows

EDIT: Added the whole code for viewProblems, with the actual credential information just ***.
I've been trying to do this all day and I can't figure it out. What I want is a button at the end of each row (the button shows up correctly) that allows me to delete that row from the MySQL database and the page (doesn't delete anything though). The code I have is as follows:
<!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">
<meta name="description" content="">
<meta name="author" content="">
<title>View Problems</title>
<!-- Bootstrap core CSS -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="well">
<div class="container">
<div class="page-header">
<h1>Jimmy's Laundry</h1>
</div>
<ol class="breadcrumb">
<li>Home</li>
<li>Login</li>
<li>Admin page</li>
</ol>
</div>
</div>
<?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 problem_id, machine_id, description FROM tbl_problem";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo '<table class="table table-hover"><tr><th>Problem ID</th><th>Machine Number</th><th>Problem Description</th><th> </th></tr>';
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row['problem_id']. "</td><td>" . $row['machine_id']. "</td><td>" . $row['description']. "</td><td><form action='deleteProblem.php?name=" . $row['problem_id']."' method= 'post'><input type='hidden' name='id' value=".$row['problem_id']."><input class ='btn btn-danger' type='submit' name='submit' value='Resolved?'></form></td></tr>";
}
echo "</table>";
}
else
{
echo "There are no problems! :)";
}
?>
</table>
</body>
For my main page, viewProblems.php. My deleteProblem.php page is as follows:
<?php
$query= "DELETE FROM tbl_problem WHERE problem_id={$_POST['id']}";
mysql_query ($query);
if (mysql_affected_rows() == 1)
{
echo "<strong>Row has been deleted</strong>"
}
else
{
echo "<strong>Deletion Failed</strong>"
}?>
I've been browsing this site and Google, and I'm trying to get it to work, but it just won't. The page loads the table correctly, but when I click the button, it takes me from website/viewProblems.php to website/deleteProblem.php?name=9(or 10, 11, 12, 13, depending on which button I press) but the page is just white space and the database doesn't get updated.
Any help would be appreciated.
P.S. I know that mySQL_ methods are dated, but we have to use them.
The script that deletes the row is a separate script that is executed in a separate request. Therefor, it is completely isolated from the request that generated the page and you will have to make a new database connection if you want to query or delete data.
In your current situation, you don't make a connection, so that's why the delete statement fails.

how to display the data from database for multiple images

html
<img src="getdesserticecreamimage.php?itemId=oepd1007" alt="image" id="img1"></li>
samcheckdb.php
<?php
$hostname="localhost";
$username="root";
$password="tiger";
$itemId=intval(\filter_input(\INPUT_GET,'itemId'));
* #var $dbhandle type */
$dbhandle = \mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
/* #var $select type */
$select= \mysqli_select_db($dbhandle,"sample")
or mysqli_error($dbhandle);
$sql="select subtitle,descript from dessert where itemId='oepd1007'";
$result=mysqli_query($dbhandle,$sql);
$row= mysqli_fetch_array($result);
$subtitle=$row['subtitle'];
$descript=$row['descript'];
mysqli_close($dbhandle);
?>
<html>
<head>
<title>Customer menu card</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" href="dessert.css">
</head>
<body>
<form id='custdisp' name='custdisp' method='post' action="samcheckdb.php" enctype="multipart/form-data">
<div id="d"><?php echo $descript ?></div>
<div id="s"><?php echo $subtitle?></div>
</form>
</body>
</html>
I'm able to retrieve the corresponding data from the database with this code.But im having n number of images,it is not possible to create that many number of pages and i dont think it is optimal.How can i do this for multiple images???
You can put an onclick like
<img src="getimage.php?itemId=oepsv1007" alt="image" id="img1" onclick="get_detail('oepsv1007');">
then do a ajax call to get the detail using this id.
javascript function
function get_detail(id) {
// ajax call
}

Categories