MySQL and PHP. My table doesnt exist - php

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);
?>

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=

MySqli joint function with PHP to show products rating

I am stuck in middle of creating product rating function with PHP.
Here is the scenario....
1) Table articles_ratings has following fields.
id, pd_id, rating
2) Table products has following fields
pd_id, pd_title, pd_short_desc, pd_image, pd_price, pd_desc and there are more fields...
This is my article page to show all items listed on page.
<?php
include 'connect.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$sql = "SELECT products.pd_id, products.pd_title, AVG(articles_ratings.rating) AS rating
FROM products
LEFT JOIN articles_ratings
ON products.pd_id=articles_ratings.pd_id
GROUP BY products.pd_id";
$result = mysqli_query($conn, $sql);
while ($row=mysqli_fetch_array($result)) {
echo "<a href='article-detail.php?pd_id=".$row[0]."'><h1>$row[1]</h1></a>";
echo "<div class='article-rating'>";
echo "Rating ".round($row[2])."/5";
echo "</div>";
}
?>
</body>
</html>
This is articel_detail page to show info when select specific item
<?php
include 'connect.php';
$id = $_GET['pd_id'];
//echo $id;
$sql = "SELECT products.pd_id, products.pd_title,
AVG(articles_ratings.rating) AS rating
FROM products
LEFT JOIN articles_ratings
ON products.pd_id=articles_ratings.pd_id
where products.pd_id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$title = $row['pd_title'];
echo "<h3>".$title."</h3>";
//echo "<p>".$row['pd_price']."</p>";
echo "Average ".round($row[2])."/5<br>";
echo "Rate this article";
}
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style media="screen">
i.fa.fa-star:hover {
color: coral;
}
</style>
</head>
<body>
<?php foreach (range(1, 5) as $rating): ?>
<?php //echo $rating; ?><i class='fa fa-star'></i>
<?php endforeach; ?>
<?php
if (isset($_GET['msg'])) {
if ($_GET['msg']="success") {
echo "<p>Thank you for your vote</p>";
}
}
?>
</body>
</html>
This page does the work to process the rating.
<?php
include 'connect.php';
if (isset($_GET['pd_id'], $_GET['rating'])) {
$pd_id = (int)$_GET['pd_id'];
$rating = (int)$_GET['rating'];
$sql = "INSERT INTO articles_ratings (pd_id, rating) VALUES ('.$pd_id.', '.$rating.')";
if (mysqli_query($conn, $sql)) {
header('Location: article-detail.php?pd_id=' . $pd_id.'&msg=success');
}
}
?>
Issue here I am facing is that when fetch info from products table the rating auto updates to 0/5.
Kindly help with this.

Select from database and echo on html page

Im trying to echo the information that is inserted to my database. As im pretty new to coding php im not really sure how i could manage to do this. My database name is "nyheter" and the table is "post". So i want to selecte the data from db and echo it on the page.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "nyheter");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//get results from database
$result = mysqli_query($connection,"SELECT * FROM post");
$all_property = array(); //declare an array for saving property
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<link rel="stylesheet" href="nyheter.css" type="text/css" />
<link rel="stylesheet" href="read.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.dynamicmaxheight.js"></script>
</head>
<body>
<div class="allt-2">
<div class="content">
<img src="http://cdn4.iconfinder.com/data/icons/socialmediaicons_v120/48/google.png"/ alt="" >
<h3><?php echo $Name?></h3>
</div>​
<section class="section js-dynamic-height" data-maxheight="150" >
<p class="dynamic-height-wrap"> Hej
</p>
<button class="js-dynamic-show-hide button" title="Läs mer" data-replace-text="Läs mindre">Läs mer</button>
</section>
<img class="ny-img" src="http://placehold.it/500x320"/>
</div>
</body>
<script>
$(document).ready(function(){
$('.js-dynamic-height').dynamicMaxHeight();
});
</script>
</body>
</html>
You're selecting everything from the database. So you've to use a loop to display the content you've selected from the database.
Write something similar to this:
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "name: " . $row["name"]."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn); ?>
Hope this was helpful.

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

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... -->

Simple PHP Not Populating Page

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())
{
?>

Categories