I am trying to get my PHP to populate my page elements but the page will not output the results based on anything I trying in the where clause of my SQL (even though it works on my server. I don't know why nothing happens when this page is run with a where parameter (the posted variable is being captured). Let me know if you need more information.
<?php
//start output buffering
ob_start('ob_gzhandler');
session_name('shipshapeLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();
define('INCLUDE_CHECK',true);
require('connect.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Organization Profile</title>
<link rel="stylesheet" type="text/css" href="profile.css" media="screen" />
</head>
<body>
<?php
$businessName = $_POST['companies'];
echo $businessName;
//check if username or email is already registered
$query = "SELECT * FROM Businesses_profiles WHERE business_name = :businessName";
//now lets update what :user should be
$query_params = array(
':businessName' => $businessName,
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
}
//fetching all the rows from the query
$profileRow = $stmt->fetch();
while ($profileRow = $stmt->fetch())
{
?>
<h1>Name</h1>
<p><?php echo $profileRow['business_name'];?></p>
<h1>Description</h1>
<p><?php echo $profileRow['description'];?></p>
<h1>Address</h1>
<p><?php echo $profileRow['address'];?></p>
<h1>Phone</h1>
<p><?php echo $profileRow['contact_phone'];?></p>
<h1>Email</h1>
<p><?php echo $profileRow['contact_email'];?></p>
<?php
}
?>
</body>
</html>
<?php
//end output buffering
ob_end_flush();
?>
I had to remove the $profileRow above the while loop.
/fetching all the rows from the query
$profileRow = $stmt->fetch();**
while ($profileRow = $stmt->fetch())
{
?>
Related
I'm new to php and backend, I tried making a login and I wanted to buttons to change the background colour. Instead of using javascript, I use php to program it, to create sessions.
Below is the code. The buttons work in the url bar, but nothing is happening.
What am I missing or overseeing?
<?php
// Initialize the session
session_start();
require_once "config.php";
session_start();
require_once "config.php";
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
$sql = "SELECT background FROM users ORDER BY id DESC";
$backgroundColour = [];
$colour = [];
$result = $link->query($sql);
$data = [];
while ($row = $result -> fetch_object()) {
$data[] = $row;
}
$backgroundColour['backgroundColour'] = $data;
$_SESSION['colour'] = $colour;
if(isset($_GET['colour'])){
$colour = $_GET['colour'];
$_SESSION['colour'] = $colour;
}
$colour_session = $_SESSION['colour'];
$sql = "INSERT INTO users (background) VALUES ('$colour_session')";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<h1>Choose background color</h1>
Hvid
Sort
</div>
<title>Welcome</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: 14px sans-serif; text-align: center; }
</style>
</head>
<body>
<h1 class="my-5">Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>
<p>
Reset Your Password
Sign Out of Your Account
</p>
</body>
</html>
At least you need to tell the client side (the browser) that the background color is set as $_SESSION["color"] (e.g. black)
So, one of the ways is to add the following line to the end of your script (your script already has <body></body>) :
<?php
echo '<script>document.body.style.backgroundColor = "'. $_SESSION['colour'] . '";</script>';
?>
So, omitting the db part, the code (tested, fully working) will be
<?php
session_start();
if(isset($_GET['colour'])){
$colour = $_GET['colour'];
$_SESSION['colour'] = $colour;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<h1>Choose background color</h1>
Hvid
Sort
</div>
</body>
<?php
echo '<script>document.body.style.backgroundColor = "'. $_SESSION['colour'] . '";</script>';
?>
For the db part:
You may need to re-write the part on the db queries. For example, apart from the need to use parameterized prepared statement in your query (to avoid SQL injection attacks). Consider using "update user set background=? where id=?" instead of an insert query, For a multi-user system, it will be something like:
$sql = "UPDATE users SET background=? WHERE id=?";
$stmt= $link->prepare($sql);
$stmt->bind_param("si", $_SESSION['colour'], $_SESSION["id"]);
$stmt->execute();
It is because each user should have only a single record in the db table (so logically it is an update instead of an insert).
I was trying to make a website. So this is the index.php page.
When 'more info' of any of the form is clicked, the user is redirected to a payment.php page, where the user must make the payment. Once the payment is done, the user is redirected to success.php page, which is supposed to show these 3 lines for two seconds and then redirect the user to details.php page. However, for some reason, instead of redirecting to details.php, both details.php and index.php come up simultaneously like this. How can I avoid the index file from being there too? I just want to show the details file.
Here is the code of the success page:
<?php
include 'index.php';
if(!empty($_GET['tid'] && !empty($_GET['product']))) {
$GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
$tid = $GET['tid'];
$product = $GET['product'];
} else {
header('Location: payment.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Thank You</title>
</head>
<body>
<div class="container mt-4">
<h2>Thank you for purchasing <?php echo $product; ?></h2>
<hr>
<p>Your transaction ID is <?php echo $tid; ?></p>
<p>Check your email for more info</p>
<?php header('Refresh: 2; URL=details.php?id='.$customer['id']);?>
</div>
</body>
</html>
I feel that this is the most important part of the success.php code:
<?php header('Refresh: 2; URL=details.php?id='.$customer['id']);?>
here's the details page:
<?php
include 'config/db_connect.php';
include 'config/db.php';
include 'index.php';
if (isset($_POST['delete'])) {
$id_to_delete = mysqli_real_escape_string($conn, $_POST['id_to_delete']);
$sql = "DELETE FROM customers WHERE id = $id_to_delete";
if (mysqli_query($conn, $sql)) {
header('Location: index.php');
} else {
echo 'query error: ' . mysqli_error($conn);
}
}
// check GET request id param
if (isset($_GET['id'])) {
// escape sql chars
$id = mysqli_real_escape_string($conn, $_GET['id']);
// make sql
$sql = "SELECT * FROM customers WHERE id = $id";
// get the query result
$result = mysqli_query($conn, $sql);
// fetch result in array format
$customer = mysqli_fetch_assoc($result);
mysqli_free_result($result);
//mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<?php include 'templates/header.php'; ?>
<div class="container center grey-text">
<?php if ($customer) : ?>
<h4><?php echo $customer['Job_Type']; ?></h4>
<p>Contact Number of loan enquirer: <?php echo $customer['Telephone']; ?></p>
<p>Annual income: <?php echo 12 * $customer['Monthly_salary']; ?></p>
<p>Existing loan amount: <?php echo $customer['Existing_loan_amount']; ?></p>
<p>Residential_Type: <?php echo $customer['Residential_Type']; ?></p>
<p>Job: <?php echo $customer['Job']; ?></p>
<p>Form submission time: <?php echo date($customer['Form_Submission_Time']); ?></p>
<!-- DELETE FORM -->
<form action="details.php" method="POST">
<input type="hidden" name="id_to_delete" value="<?php echo $customer['id']; ?>">
<input type="submit" name="delete" value="Delete" class="btn brand z-depth-0">
</form>
<?php else : ?>
<h5>No such customer exists.</h5>
<?php endif ?>
</div>
<?php include 'templates/footer.php'; ?>
</html>
Your details page starts with these three line:
include 'config/db_connect.php';
include 'config/db.php';
include 'index.php';
As you can see, in the third line, you include index.php. My best guess is that that is the reason you see it in the details page.
So i made a database in my phyMyAdmin and the time i encoded it with my html file and open it in chrome...it says that
Table 'forum.form_tabl' doesn't exist
what happened and what should i do?
this is the code
<?php
session_start();
require"db_connect.php";
$sql= "SELECT forum_id,forum_name FROM form_tabl";
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div id="container">
<table>
<?php if($query->num_rows !==0);
while($row = $query->fetch()):
?>
<tr><td><?php echo $f_name;?>
</td></tr>
<?php endwhile;T_ENDIF;
?>
</table>
</div>
</body>
</html>[enter image description here][1]
//THE OTHER .PHP file is db_connect.php where i put the actual one
<?php
$db = mysqli_connect("localhost","root","","forum") or die("ERROR! With Connection")
?>
enter image description here
$con = mysqli_connect("HostName","UserName","password","DBName") or die("Some error occurred during connection " . mysqli_error($con));
// Write query
$strSQL = "SELECT username FROM MyTable";
// Execute the query.
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["username"]."
";
}
// Close the connection
mysqli_close($con);
?>
My knowledge of php is very limited, although I am aware that mysql functions are depreciated but it doesn't matter for the purpose of this project.
I have a table (enisatquestion) with training questions, and file paths to 14 videos stored on my pc which I want to display using localhost.
My table structure and an example of one of the rows in my table are as follows:
Columns are:
eNISATID (Auto-increment)
eNISATQuestion (Training question)
eNISATVideo (File path to video)
An example of a Row:
1
Can you login?
http://localhost\Tna\eNISAT\LoginTutorial.wmv
Here is my code, I am getting an error;
mysql_fetch_assoc() expects at least 1 parameter, 0 given in C:\wamp\www\Tna\eNISATVids.php on line 20
Can anyone please help me with this, I have researched a lot of ways to display this, but I am struggling with it as I have never worked with videos in php before. The videos are also wmv format. Or can anyone give me a more suitable example
Many Thanks
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
//maintain SESSION user_id
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
//Select video name and question
$query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion";
$result = mysql_query($query);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$enisatquestion = "<table >";
while ( $row = mysql_fetch_assoc($result) ) {
$enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";
}
$enisatquestion .= "</table>";
echo $enisatquestion;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>NHSCT eNISAT Tutorials</label>
</div>
<div id="right">
<div id="content">
Welcome <?php echo $userRow['forename']; ?> Sign Out
</div>
</div>
</div>
</body>
</html>
The answer is simple:
$query = "SELECT eNISATName, eNISATVideo FROM enisatquestion";
$result = mysql_query($query);
$enisatquestion = "<table>";
while ( $r = mysql_fetch_assoc($result) ) {
$enisatquestion .= "<tr><td><a href='{$r['eNISATVideo']}'>{$r['eNISATName']}</a></td></tr>";
}
$enisatquestion .= "</table>";
echo $enisatquestion;
?>
Notice the new $result variable that is passed to mysql_fetch_assoc.
So here's an example code I use to show all the categories that are linked to a certain postID.
For example post 1 has the categories: Apple, Green and Yellow linked to it.
But I can't seem to fetch the data correctly since it has already been fetched once at the top i can't do a proper while loop at the Categories: part of my code where I try to do a while loop. The while loop works and fetches all the categories except the First one and also when I place the while loop the
$row['postTitle']
and
$row['postCont']
won't appear anymore because it's being skipped. How would I fix something like this? Thanks.
<?php require('includes/config.php');
$stmt = $db->prepare(" SELECT *
FROM blog_posts
LEFT JOIN blog_posts_categories ON blog_posts.postID=blog_posts_categories.postID
INNER JOIN blog_categories ON blog_posts_categories.catID=blog_categories.catID
WHERE blog_posts.postID = :postID");
$stmt->execute(array(':postID' => $_GET['id']));
$row = $stmt->fetch();
//if post does not exists redirect user to homepage.
if($row['postID'] == ''){
header('Location: ./');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $row['postTitle'];?> | Website</title>
<link rel="stylesheet" href="style/normalize.css">
<link rel="stylesheet" href="style/main.css">
</head>
<body>
<div id="wrapper">
<h1>Single Post Page</h1>
<hr />
<p>Home |
Categories:
<?php while($row = $stmt->fetch())
{
//the first category is being skipped? How to fix?
echo $row['catName'];
} ?>
</p>
<div>
<?php
//these won't appear because of the while loop. It's being skipped.
echo '<h1>'.$row['postTitle'].'</h1>';
echo '<p>'.$row['postCont'].'</p>';
?>
</div>
</div>
</body>
</html>
Replace:
while($row = $stmt->fetch())
{
echo $row['catName'];
}
With:
do {
echo $row['catName'];
} while($row = $stmt->fetch());
As for the other items - put them inside the loop obviously instead of afterwards.