Can't call dynamic page with $_GET - php

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=

Related

why PHP search page show blank box with no result?

I'm trying to do a search bar here.
I have done my database, filled it with data, connected it to my php file, did the search bar code everything is fine but when i tried to search for a word its showing me blank box with zero result
This is my db connect code
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "product_list";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is search page code
<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
?>
</style>
<?php ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>
<div class="result-container">
<?php
if (isset($_POST['Search'])) {
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM product_list WHERE a_Name LIKE '%$search%' OR Pharmacy LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryresult = mysqli_num_rows($result);
echo 'There are' . $queryresult . 'results!';
if ($queryresult > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='result-box'>
<h3>" . $row['a_Name'] . "</h3>
<p>" . $row['Price'] . "</p>
<p>" . $row['Pharmacy'] . "</p>
</div>";
}
} else {
echo "Sorry No Result :(!";
}
}
?>
</div>
this is my form page
<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Screen</title>
</head>
<body>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
?>
</style>
<?php ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>
<form action="search.php" method="POST">
<div id="searchbar">
<input type="text" name="search" placeholder="Search">
<button id='sbutton'>Search</button>
</div>
</form>
<h2 class="allresult">All Result</h2>
<div class="result-container">
<?php
$sql = "SELECT * FROM Product";
$result = mysqli_query($conn, $sql);
$queryresults = mysqli_num_rows($result);
if ($queryresults > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='result-box'>
<h3>" . $row['a_Name'] . "</h3>
<p>" . $row['Price'] . "</p>
<p>" . $row['Pharmacy'] . "</p>
</div>";
}
}
?>
</div>
</body>
</html>
I'm new to PHP
Your search field name is search in lowercase but you are checking for Search in $_POST which is capitalized. Make sure they match and it will fix the issue

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

Length of the values which is fetched from database

I need to find how many character each first name has and arrange from higher to lower. This code will print the id first name and last name using data table, I want to show the number of character each has in separate table with the corresponding first name.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/dataTables.jqueryui.min.js"></script>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "name";
$conn = new mysqli($servername, $username, $password, $dbname);
if(mysqli_connect_errno($conn))
{
echo 'Failed to connect to database: '.mysqli_connect_error();
}
else{}
$sql="SELECT * FROM nametable";
$result=mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
echo "<table width=50% class='dataTable'><thead><tr><th>id</th><th>Firstname</th><th>Lastname</th></tr></thead><tbody>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["firstName"]."</td><td>".$row["lastName"]."</td></tr>";
}
echo "</tbody></table>";
}
?>
<script>
$(document).ready(function() {
$(".dataTable").DataTable({
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"pagingType": "numbers"
} );
} );
</script>
<?php
$sql1="SELECT firstName, LEN(firstName) as LengthOfFirstName FROM nametable";
$result2=mysqli_query($conn, $sql1);
if ($result2->num_rows > 0){
while ($row2 = $result2->fetch_assoc()){
echo $row2 ."<br>";
}
}
?>
</body>
</html>
You seem to be looking for strlen, which returns the length of the input string:
strlen($row['firstName']);

MySQL and PHP. My table doesnt exist

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

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