show random entries from SQL databases fade in out with out refreshing - php

I need to have a website that can echo out random names from a database, then fade them in and out smoothly. the code that i am using currently is refreshing the whole page and it makes the page flicker and the name change is not smooth.
<!doctype html>
<html><head><meta http-equiv=REFRESH CONTENT=15;url=index.php>
<title>Student Display</title></head>
<body>
<img src="U" width="499" height="128" alt=""/>
<center>
<span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'DejaVu Sans', Verdana, sans-serif; font-size: 72px; color: #013b6a;">
<p>
<p>
<p>
<?php
//delayed refresh
$db_host = "localhost:/test";
$db_user = "test";
$db_pwd = "password";
$database = "test";
$table = "Sheet1";
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table} ORDER BY RAND() LIMIT 0,1");
if (!$result) {
die("Query to show fields from table failed");
}
// Display the name and date
while($row = mysql_fetch_assoc($result))
{
echo $row['Firstname'] . " ";
echo " " . $row['Surname'] . " ";
echo "<br>";
echo " " . $row['Year'] . " ";
}
?>
</span>
</center>
</body></html>

You'll need to split the code that queries the database with the code that renders the page. You can use AJAX techniques to request a new name from the script that queries the database without a page reload, then use jQuery animation techniques to fade out the old value and fade in the new value.

Related

Putting items from a database into an HTML page and formatting them to be side by side

I have made a database and connected it properly. I am trying to display the database contents to be side by side. I have tried to create a div but everything is jumbled up. The items are floating left but are not showing up side by side.
<html>
<head>
<style>
.menu{
float:left;
}
p{
text-align:left;
float:left;
inline-size:300px;
overflow:hidden;
}
</style>
</head>
<body>
<main>
<section>
<h3>Inventory</h3>
<?php include 'db.php';
$ID = $_GET['ID'];
$query = "SELECT * FROM inventory ORDER BY ID";
/* Try to query the database */
if ($result = $mysqli->query($query)) {
// Don't do anything if successful
}
else
{
echo "Sorry, an item with the ID of $ID cannot be found " . mysql_error()."<br>";
}
while ($result_ar = mysqli_fetch_assoc($result)) {
//Display food image and image source
$image = $result_ar['Image'];
echo "<div class = 'menu'>" . $result_ar['Image_Source'] . "<br>".
"<IMG src= 'images/$image' style = height:200px; width:300px;>". "</div>";
//Display name and description
echo "<p>".$result_ar['Name'] . "<br>"
. $result_ar['Description'] ."</p>";
}
$mysqli->close();
?>
</section>
</main>
</body>
</html>

PHP Code for selecting data from mySQL Database for Amazon web services [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
Currently, I am trying to work on adding a webpage on Amazon Web Services. The web page contains PHP and HTML code intended to display a particular table from my local MySQL database. I have already created an EC2 instance and a AWS RDS instance, however, there is a problem that is happening consistently which is bothering me. On my computer's local host through MySQL, the code to display all records of a table was this:
SELECT * from Students, StudentProfile, Job
WHERE Students.StudentID=StudentProfile.Students_StudentID and Students.StudentID=Job.Students_StudentID;
Based from the above code, this executes perfectly and I did confirmed this by doing the same procedure at the MySQL Workbench. However, upon uploading my file called "projectview.php" (the .php file to view all records of a table) onto AWS, it doesn't work. I was aware that I knew I had to change parts of my code for connecting it to AWS's RDS instance. As a result, I followed the tutorial example in creating a sample page based from a tutorial in AWS. Below of this contains a sample .php file that contains my current work for a website to display.
<HTML>
<HEAD>
<TITLE>List Students</title>
<link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet">
<style>
body {font-family: 'Varela Round', sans-serif;
background-color: #ffddcc;
color: #e64d00;
border: 2px solid #ff7733;
border-radius: 10px;
}
h1 {font-family: Georgia, serif;}
input {color:#e64d00;}
</style>
</head>
<BODY>
<H1>List Students</h1>
<?php include '../inc/dbinfo.inc';
$conn = #mysqli_connect("DB_SERVER","DB_USERNAME","DB_PASSWORD","DB_DATABASE" );
if($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
else
{
echo "Status:Connected.";
echo "<br>";
}
$sql = "SELECT * from Students, StudentProfile, Job
where Students.StudentID=StudentProfile.Students_StudentID and Students.StudentID=Job.Students_StudentID";
mysql_select_db("project2");
$result = mysqli_query($conn, $sql);
if(!$result) {
die('Could not get data: ' .mysql_error());
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<br>";
echo "StudentID: " . $row["studentID"]. "<br>";
echo "First Name: " . $row["firstName"]. "<br>";
echo "Last Name: " . $row["lastName"]. "<br>";
echo "Major: " . $row["Major"]. "<br>";
echo "Level: " . $row["Level"]. "<br>";
echo "Address: " . $row["Address"]. "<br>";
echo "City: " . $row["City"]. "<br>";
echo "Country: " . $row["Country"]. "<br>";
echo "Zipcode: " . $row["Zipcode"]. "<br>";
echo "Phone: " . $row["Phone"]. "<br>";
echo "Job Company: " . $row["Job_Company"]. "<br>";
echo "Job Title: " . $row["Job_Title"]. "<br>";
}
echo "Fetched data successfully\n";
//$result = $conn->query($query);
$conn->close();
?>
<br>
<FORM METHOD="LINK" ACTION="project2mainmenu.html">
<INPUT TYPE="submit" VALUE="Return to Main Menu">
</form>
</body>
</html>
Furthermore, I noticed that when I check at my web page after running the code, it shows an image like this:
****Note: I already uploaded all my related files for the particular web pages. Also, this web page is not affiliated with any other files. It is a stand-alone file.**
Apply this code and set your query it's working fine.
i have create connection from new mysqli.
it's result like this http://prntscr.com/envt1l
<HTML>
<HEAD>
<TITLE>List Students</title>
<link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet">
<style>
body {font-family: 'Varela Round', sans-serif;
background-color: #ffddcc;
color: #e64d00;
border: 2px solid #ff7733;
border-radius: 10px;
}
h1 {font-family: Georgia, serif;}
input {color:#e64d00;}
</style>
</head>
<BODY>
<H1>List Students</h1>
<?php //include '../inc/dbinfo.inc';
$conn = new mysqli('localhost','root','','wordpress1');
if($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
else
{
echo "Status:Connected.";
echo "<br>";
}
$sql = "SELECT * from wp_terms";
//mysql_select_db("project2");
$result = mysqli_query($conn, $sql);
if(!$result) {
die('Could not get data: ' .mysql_error());
}
foreach ($result as $value) {
echo "<br>";
echo "term_id: " . $value["term_id"]. "<br>";
echo "name: " . $value["name"]. "<br>";
echo "slug: " . $value["slug"]. "<br>";
}
echo "Fetched data successfully\n";
//$result = $conn->query($query);
$conn->close();
?>
<br>
<FORM METHOD="LINK" ACTION="project2mainmenu.html">
<INPUT TYPE="submit" VALUE="Return to Main Menu">
</form>
</body>
</html>

PHP printf; Css only works on first row from database

Need Help. I tried to show reader comments on my page. The comments are saved in a database (MySQL). I used printf(String, args1,args2) and put some css style on it. Strangely, the css works on the first comment only.
if ($conn = mysqli_connect("$servername", "$username", "$password", "$dbname")){
$sql = "SELECT * FROM kommentare ORDER BY datum DESC";
$ergebnis = mysqli_query($conn, $sql);
while ($zeile = mysqli_fetch_object($ergebnis)){
**$format = "<p><span id='ueberschrift'>%s<span/> <span id='writer'><a href='mailto:%s'>%s<a/> schrieb am:%s<span/><p/>
<p id='kommentare'>%s<p/><hr/>";
printf($format,
htmlspecialchars($zeile->ueberschrift),
urlencode($zeile->email),
htmlspecialchars($zeile->name),
htmlspecialchars(date("d.m.Y, H:i", $zeile->datum)),
nl2br(htmlspecialchars($zeile->kommentar))
);**
}
} else {
echo "Fehler: ". mysqli_connect_error(). "!";
}
#writer{
font-weight: normal;
float:right;
}
#ueberschrift {
font-weight: bold;
font-size: 14px;
}
float:right only works on the first comment (last row from database)
what have I done wrong? Here's an image of the page.
Try using
<span class ='ueberschrift'
<span class ='writer'
NOT
<span id ='ueberschrift'
<span id='writer'
Each id should be unique and only appear once per html document. Class can appear multiple times.
CSS would be:
.writer{
font-weight: normal;
float:right;
}
.ueberschrift {
font-weight: bold;
font-size: 14px;
}
https://css-tricks.com/the-difference-between-id-and-class/
Check your html closing tag, all your html closing tag is wrong.
Slash / is should be put after < so its like </tag>, except self-closing tag like <br /> , <hr /> , etc.
<span/> should be </span>
<a/> should be </a>
<p/> sholud be </p>
And here the completed of your code
if ($conn = mysqli_connect("$servername", "$username", "$password", "$dbname")){
$sql = "SELECT * FROM kommentare ORDER BY datum DESC";
$ergebnis = mysqli_query($conn, $sql);
while ($zeile = mysqli_fetch_object($ergebnis)){
**$format = "<p><span id='ueberschrift'>%s</span> <span id='writer'><a href='mailto:%s'>%s</a> schrieb am:%s</span></p>
<p id='kommentare'>%s</p><hr />";
printf($format,
htmlspecialchars($zeile->ueberschrift),
urlencode($zeile->email),
htmlspecialchars($zeile->name),
htmlspecialchars(date("d.m.Y, H:i", $zeile->datum)),
nl2br(htmlspecialchars($zeile->kommentar))
);**
}
} else {
echo "Fehler: ". mysqli_connect_error(). "!";
}
it might help you https://www.w3.org/TR/html5/syntax.html#end-tags

No database selected: login php code

<?php
session_start();
$con=mysqli_connect("localhost","xxx","xxxxxx","xxx");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$eadd = $_POST['eadd'];
$pass = $_POST['pass'];
$eadd = htmlspecialchars(stripslashes(strip_tags($eadd)));
$pass = htmlspecialchars(stripslashes(strip_tags($pass)));
if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
if(!$sql){
die('There was an error in query '. mysql_error());
}
$count = mysql_numrows($sql) or die(mysql_error());
if ($count<=0)
{
echo "
<html>
<style>
body{
background-color:#cccccc;
}
#error{
position:relative;
margin:auto;
top:20px;
width:320px;
height:55px;
background-color:#63a8d7;
border:1px solid #2a262a;
}
#errorC{
position:absolute;
top:20px;
left:20px;
font: 14px arial, tahoma;
}
</style>
<body>
<div id=error>
<div id=errorC>
Incorrect Email Address and Password! <a href=index.php>GO BACK</a>
</div>
</div>
</body>
</html>
";
}
else
{
//have them logged in
$_SESSION['account'] = $eadd;
header('location:home.php');
}
mysqli_close($con);
} else {
echo "
<html>
<style>
body{
background-color:#cccccc;
}
#error{
position:relative;
margin:auto;
top:20px;
width:320px;
height:55px;
background-color:#63a8d7;
border:1px solid #2a262a;
}
#errorC{
position:absolute;
top:20px;
left:20px;
font: 14px arial, tahoma;
}
</style>
<body>
<div id=error>
<div id=errorC>
Invalid Email Address! <a href=index.php>GO BACK</a>
</div>
</div>
</body>
</html>
";
}
?>
Why there is an error "No database selected"? My mysqli_connect is correct. I have another register php code, using which I can register some email address using that connection.But here in login php code, I can't login with the user email address.
From the above code its look like you are using mysqli_connect for database connection and mysql_query for query execution. Use mysqli_query instead of mysql_query . Like this
mysqli_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'");
Actually, you need to select a database after authentication.
We need to follow these steps while making a database connection.
Create a connection
Select the database
Fire the query
Use query result
Close the connection
Sample Code is here
Firstly, You need to add this line in your code after login
$db_select = mysql_select_db("myDatabase", $con);
Secondly, you need to pass $con as second parameter after selecting your database So your query statement becomes like this
$sql = mysql_query("SELECT * FROM accounts WHERE Emailadd = '$eadd' AND Password = '$pass'", $con);

$_POST variable is duplicating

I do not even know how to google this one...imagine it is something stupid...but any help would be great...
passing a variable when submitting a form...when echo the $_POST it is good...but when i put it into a php variable it is duplicated
<?
//list transactions by month
if ($_POST['m']=="yes"){
$table = $_POST['month'];
$_SESSION['table']=$_POST['month'];
$conn = mysql_connect("localhost", "mss_records", "3205") or die(mysql_error());
mysql_select_db('store_records', $conn) or die(mysql_error());
$result = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($result))
{
$id=$row['transaction'];
$date=$row['date'];
$time=$row['time'];
$paid=$row['payment'];
$total=$row['total'];
echo '<style type="text/css">
<!--
.list {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 12px;
color: #000;
padding: 2px;
border: 2px solid #009;
}
.view {
width: 100px;
}
-->
</style>
<div class="list">
<p><span style="color: #900">Transaction #</span>'.$id.'
<span style="color: #900">Date:</span>'.$date.'
<span style="color: #900">Time:</span>'.$time.'<span style="color: #900">
Paid By:</span>'.$paid.' <span style="color: #900">Total:</span>'
.number_format($total, 2).'
<form name="form1" method="post" action="find.php">
<label>
<input type="submit" name="view" id="view" value="'.$id.'">
</label>
</form>
</p>
</div>
<p></p>';
}
}
//view transaction after viewing by month
if (isset($_POST['view'])){
$conn = mysql_connect("localhost", "mss_records", "3205") or die(mysql_error());
mysql_select_db('store_records', $conn) or die(mysql_error());
$table = $_SESSION['table'];
echo "this is the number ".$_POST['view'];
$post=$_POST['view'];
echo "this is the post ".$post;
$result = mysql_query("SELECT * FROM $table WHERE transaction = '$post'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$items=$row['transaction'];
}
echo $items;
}
?>
after the user goes through the first selection and on the second window the output is...
this is the number 46this is the $post 4646
Your query is mysql_query("SELECT * FROM $table WHERE transaction = '$post'"). Therefore the value of $items=$row['transaction']; is also going to be 46. When you echo out everything without line breaks, it smashes everything together.
POST is not duplicating anything, you are just echoing $items directly after it.
Try this:
$table = $_SESSION['table'];
echo "this is the number ".$_POST['view']."<br /> \n";
$post=$_POST['view'];
echo "this is the post ".$post."<br /> \n";
$result = mysql_query("SELECT * FROM $table WHERE transaction = '$post'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$items=$row['transaction'];
}
echo $items;
}

Categories