i have the following information displayed
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0)
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
?>
as you can see i have created a game_details page which will display that specific Game_id when the image is clicked
im having trouble understanding how to pull the data out from that game_id in sql on the other page.
here is my attempt on the game_details page
<?php
if (!isset($_GET['$game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id={$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['$game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
This attempt is giving me the "invalid category ID" error
Would appreciate help
There are a few issues with your code.
Let's start from the top.
['$game_id'] you need to remove the dollar sign from it in $_GET['$game_id']
Then, $row['$game_id'] same thing; remove the dollar sign.
Then, game_id={$game_id}' will throw a syntax error.
In your first body of code; you should also use proper bracing for all your conditional statements.
This one has none if (mysqli_num_rows($result) > 0) and will cause potential havoc.
Rewrites:
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0){
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
}
?>
Sidenote for WHERE game_id='{$game_id}' in below. If that doesn't work, remove the quotes from it.
WHERE game_id={$game_id}
2nd body:
<?php
if (!isset($_GET['game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id='{$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
Use error checking tools at your disposal during testing:
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php
You want to be using $_GET['gameid'] as that's the parameter you passed.
You are calling for game_id when the link to go to game_details.php has the variable gameid. Either change the parameter in the link to game_id or call for gameid in your $_GET['$game_id'].
Also, as Fred -ii- said, take out the dollar sign in $_GET['$game_id']
Related
I have looked everywhere and cannot seem to find out the answer to fix my problem.
I have an online messaging system and i would like to implement Online and Offline status to the users. But no matter what i do with this code... I cannot seem to get it working. Any help would be greatly appreciated. Keep in mind I dont want to alter any of my existing code other than the ones that have to do with $online and the q2 variables.
my code is:
<?php
//show all the users expect me
$q = mysqli_query($con, "SELECT * FROM `users` WHERE id!='$user_id'");
$q2 = mysqli_query($con, "SELECT status FROM `users`");
//display all the results
while($row = mysqli_fetch_assoc($q2)){
$online = $row['status'];
if($online == "online"){
$tcolor = "green";
}
elseif($online=="offline"){
$tcolor = "red";
}
elseif($online ==""){
$tcolor= "gray";
}
}
while($row = mysqli_fetch_assoc($q)){
echo "<a href='/users/pm?id={$row['id']}'><li style='border-left:{$tcolor} 15px solid;'><img src='{$row['pic']}'> {$row['name']}</li></a>";
}
?>
Your logic for setting $online is reversed. It should be: $online = $row['status'];
EDIT BELOW:
Your original $q2 was unnecessary as it was just retrieving the status column and nothing was linked to the users. Also, you have all the data you need in $q.
<?php
//show all the users expect me
$q = mysqli_query($con, "SELECT * FROM `users` WHERE id!='$user_id'");
//display all the results
while($row = mysqli_fetch_assoc($q)){
$online = $row['status'];
if($online == "online"){
$tcolor = "green";
}
elseif($online=="offline"){
$tcolor = "red";
}
elseif($online ==""){
$tcolor= "gray";
}
echo "<a href='/users/pm?id={$row['id']}'><li style='border-left:{$tcolor} 15px solid;'><img src='{$row['pic']}'> {$row['name']}</li></a>";
}
?>
I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id
<?php
include 'config.php'; //connect to db
if(isset($_REQUEST["pwd"]) && isset($_REQUEST["name"])) {
$password = $_REQUEST['pwd']; //pass from previous page
$name = $_REQUEST['name']; //pass from previous page
$checkUserPass = mysql_query("SELECT * FROM validPersonnel WHERE Passkey = '$password' and Name = '$name'", $conn); //check if the user exist
if(mysql_num_rows($checkUserPass) == 1) {
$personnelId = mysql_query("SELECT PersonnelID FROM validPersonnel WHERE Passkey = '$password' and Name = '$name'", $conn); //query user id
while($row = mysql_fetch_assoc($personnelId)) {
echo $row['PersonnelD']; // print user id
}
mysql_close($conn);
//echo "<br/><br/>";
//echo "<script>alert('Logged In.')</script>";
//header("Refresh: 1; url=profile/profile.php?id="'.$id.');
//header('Refresh: 1; url=test.php?id=$personnelId');
} else {
echo "<br/><br/>";
echo "<script>alert('Wrong Password.')</script>";
header('Refresh: 1; url=personnelselect.php');
}
}
?>
i cannot echo the $row['PersonnelD'] the page shows blank. i cannot understand where did i go wrong. this page quesion have been solved
Looks like you have mistake in code:
echo $row['PersonnelD'];
shouldn't it be following?
echo $row['PersonnelID'];
check the mysql_fetch_assoc() function may be its parameter is empty so it can't enter the while loop
Try to debug and check the values came in the variables using var_dump() function. Ex: var_dump($row); in while loop.
In both your querys, you have
"SELECT * FROM validPersonnel WHERE Passkey = '$password' and Name = '$name'"
It should be:
"SELECT * FROM validPersonnel WHERE Passkey = '".$password."' and Name = '".$name."';"
PHP doesn't recognize the $var unless you close the quotes. The period adds the $var to the string.
The search function works like a charm, but I'm not sure how to add certain "extras" to it. When there are results to display, I would also like it to say:
"Your search returned x results."
Followed by the results.
When there are no results to display, I would like it to say:
"Your search returned no results. Please try again."
And when the user does not input anything into the search form, I would like it to say:
"You did not enter a search term."
I'm a PHP beginner, and I'm not sure how to implement this into my current code; I've tried a bunch of different ways, and it either gives me errors or returns a blank page when there are no results.
Any direction or help would be great. Thank you.
Here is my current code:
<?php
//STEP 1 Connect To Database
$connect = mysql_connect("localhost","tarb89_admin","leccums");
if (!$connect)
{
die("MySQL could not connect!");
}
$DB = mysql_select_db('tarb89_characters');
if(!$DB)
{
die("MySQL could not select Database!");
}
//STEP 2 Check Valid Information
if(isset($_GET['search']))
{
//STEP 3 Declair Variables
$Search = $_GET['search'];
$result = mysql_query("SELECT * FROM characters WHERE name LIKE '%$Search%' ");
while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$id = $row['id'];
$breed = $row['breed'];
$gender = $row['gender'];
$genetics = $row['genetics'];
$profile = $row['profile'];
$player = $row['player'];
$color = $row['color'];
$markings = $row['markings'];
$traits = $row['traits'];
$defects = $row['defects'];
$extras = $row['extras'];
echo " <h3>$name</h3>
<table width='700px' cellpadding='5' cellspacing='0'>
<tr>
<td><p>
<b>ID Number: </b>$id<br>
<b>Breed: </b>$breed<br>
<b>Gender: </b>$gender<br>
<b>Genetics: </b>$genetics<br>
<b>Profile:</b> <a href='$profile'>$profile</a><br>
<b>Player:</b> $player</p></td>
<td><p>
<b>Color:</b> $color<br>
<b>Markings:</b> $markings<br>
<b>Traits:</b> $traits<br>
<b>Defects:</b> $defects<br>
<b>Extras:</b> $extras</p></td>
</table>";
}
}
?>
Use mysql_num_rows() to return number of rows
if($search!="") {
// Your Query
$num = mysql_num_rows($result);
if($num >= 1)
{
echo "Your search returned $num results";
// your code
}
else
{
echo "Your search returned no results. Please try again";
}
} else {
echo "You did not enter a search term";
}
YOUR CODE
if(isset($_GET['search']))
{
//STEP 3 Declair Variables
$Search = $_GET['search'];
$result = mysql_query("SELECT * FROM characters WHERE name LIKE '%$Search%' ");
$num = mysql_num_rows($result);
if($num >= 1)
{
echo "Your search returned $num results";
while($row = mysql_fetch_assoc($result))
{
$name = $row['name'];
$id = $row['id'];
$breed = $row['breed'];
$gender = $row['gender'];
$genetics = $row['genetics'];
$profile = $row['profile'];
$player = $row['player'];
$color = $row['color'];
$markings = $row['markings'];
$traits = $row['traits'];
$defects = $row['defects'];
$extras = $row['extras'];
echo " <h3>$name</h3>
<table width='700px' cellpadding='5' cellspacing='0'>
<tr>
<td><p>
<b>ID Number: </b>$id<br>
<b>Breed: </b>$breed<br>
<b>Gender: </b>$gender<br>
<b>Genetics: </b>$genetics<br>
<b>Profile:</b> <a href='$profile'>$profile</a><br>
<b>Player:</b> $player</p></td>
<td><p>
<b>Color:</b> $color<br>
<b>Markings:</b> $markings<br>
<b>Traits:</b> $traits<br>
<b>Defects:</b> $defects<br>
<b>Extras:</b> $extras</p></td>
</table>";
}
}
else
{
echo "Your search returned no results. Please try again";
}
} else {
echo "You did not enter a search term";
}
Note: mysql_* functions are deprecated, Move on mysqli_* functions asap
I am trying to echo out all of the user rows in my database as a select in a form.
It is only showing a blank space. Nothing else.
Here is my code.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
First, please stop using mysql_ functions as they are being deprecated. Look into mysqli_ or PDO. Be aware that your script is vulnerable to SQL injection.
The reason your script is not working is because it appears you are calling mysql_fetch_assoc twice. When calling it the second time, there won't be any output if your query only returns a single row.
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
You will need to resubmit a query (something like below) and call that result separately to populate the drop down, or store the result in an array.
$qry=("SELECT `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
echo '<option>' . ucwords($row['uname']) . '</option>';
}
It looks like your while loop is set up badly, I think you need to change
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
to this
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
UPDATE:
I reread your question and it seems that you want to display all unames from your table.
The original query was to check if the user is an admin.
Here's how it will look like. But it's just a duplicate of njk's answer.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
// added these 2 lines
$qry=("SELECT `rank`, `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
Are you sure your database has more than 1 row? You are calling the fetch 1 time so the second time it will try to fetch the second row.
Try also running the query directly into mysql and see if you get the rows you're expecting back, may be its not returning what you expected