i am having a problem with the get method in php, i try to get a variable($profile_id) from one php page to another, the variable is working in this page
<?php
$follow="";
$loggedinid=$_SESSION['userid'];
$sqll = "SELECT id FROM Follow WHERE user_one='$loggedinid' AND user_two='$profile_id'";
if($profile_id != $_SESSION["userid"]){
$check= mysqli_query($db_conx, $sqll);
if(mysqli_num_rows($check) == 1){
$follow='Unfollow';//This is where i try to put the variable, so i can call it with the get method on followaction.php
}else{
$follow='Follow';
}
}
?>
but then in the followaction.php when i call the profid, It returns $profile_id(sting) instead of the number it should be representing
<?php
include_once("php_includes/check_login_status.php");
$followaction=$_GET['followaction'];
$profileid = $_GET['profid'];
$loggedinid = $_SESSION['userid'];
$loggedinusername = $_SESSION['username'];
if($followaction == 'follow'){
mysqli_query($db_conx, "INSERT INTO Follow VALUES('','$loggedinid','$profileid')");
}
if($followaction == 'unfollow'){
$sql = "DELETE FROM Follow WHERE user_one='$loggedinid' AND user_two='$profileid'";
mysqli_query($db_conx, $sql);
}
?>
How can i fix this, everything is working but i cant transfer the profile_id to this page....
This does not work
$follow='Unfollow';
If you do
echo $follow;
You will get something like this (Notice $profile_id has not been replaced)
Unfollow
You need to use double quotes if you want variable replacement
$follow="Unfollow";
Related
I am trying to insert rating in database and calling average of that given rating from database
<?PHP
$connection = mysqli_connect("localhost", "akdnaklnd", "lfnlfns","faknfns");
$game = $_POST['game'];
$post_rating = $_POST['rating'];
$find_data = mysqli_query( "SELECT * FROM rates WHERE game='$game'");
while($row = mysqli_fetch_assoc($find_data)){
$id=$row['id'];
$current_rating = $row['rating'];
$current_hits=$row['hits'];
}
$new_hits = $current_hits + 1;
$update_hits= mysqli_query("UPDATE rates SET hits = '$new_hits' WHERE id='$id'");
$pre_rating= $current_rating + $post_rating;
$new_rating = $pre_rating / $new_hits;
$update_rating = mysqli_query("UPDATE rates SET rating ='$new_rating' WHERE id='$id'");
header("location : average.php");
?>
try to debug it by removing all the MySQL stuffs from code and keep redirection code with no space ie .
header("location:average.php");
if it works then there must be an error on your MySQL query. try adding error check after execution each MySQL query operations. Hope that will help you finding the breakpoint where your execution terminates before redirection statement.
Add $connection param to mysqli_query like this:
$find_data = mysqli_query($connection, "SELECT * FROM rates WHERE game='$game'");
Delete space symbol after "location" before colon: header("location: average.php");
Firstly you need not to select data you can directly write your update query like this.
Please make sure you pass variable instead of string for better practise use double and single quotes and dott too.
Most important pass connection variable.
$update_hits= mysqli_query($connection,"UPDATE rates SET hits = hits+1 WHERE id='".$id."'");
Try echo $update_hits if it give one then query successful else there is issue.
For that you can use error log
if (!$result) {
$errorQuery = $update_hits;
throw new Exception(mysqli_error($connection));
$errorMessage = mysqli_real_escape_string($connection,$e->getMessage());
echo $errorMessage;
exit();
}
else{
echo "Success";
}
Before setting as duplicate, I've spent 4 hours on researching about my problem, but I had no luck.
I am trying to make a signup/login system for my website. The main point that doesn't seem to work is that when I am signing up on my website, the session doesn't seem to start. The reason that I can see it is because, on my navbar, I have set it to change from signup to log out. Here is the piece of code for that:
<ul>
<li class="list1">Home</li>
<li class="list2">About</li>
<li class="list3">Portfolio</li>
<li class="list4">Blog</li>
<li class="list4">Contact</li>
<?php
if (isset($_SESSION['id'])){
echo "<li><a href='#'>SIGN OUT</a></li>";
}
else{
echo "<li><a onclick='signup(event)' href='#'>SIGN UP</a></li>";
}
?>
</ul>
To make that I have created three files. One is the mane page, one is the signup file itself, code below:
<?php
session_start();
include "../dbh.php";
$first = $_POST["first"];
$last = $_POST["last"];
$uid = $_POST["uid"];
$email = $_POST["email"];
$pwd = $_POST["pwd"];
$sql = "INSERT INTO users (first,last,uid,email,pwd) VALUES ('$first','$last','$uid','$email','$pwd')";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$_SESSION['id'] = $row['id'];
header("Location: ../index.php");
exit();
and the last one is the file which connects PHP to the database code below:
$conn = mysqli_connect("XXX","XXX","XXX","XXX");
if (!$conn){
die("Connection failed: ".mysqli_connect_error());
}
I believe that the session doesn't start because the main page reloads after the user hits signup on the form, but I have started the session on all of my files (except the database connection file where it's not needed). I used session start on all of my page and I placed it on the beginning of all pages with opening and closing PHP tags.
Any suggestions? I appreciate your answers and comments!
Sorry for the bad English but it's not my first language.
This:
$sql = "INSERT INTO users (first,last,uid,email,pwd) VALUES ('$first','$last','$uid','$email','$pwd')";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
^^^^^^^^^^^^^^^^^^
$_SESSION['id'] = $row['id'];
Insert queries do NOT return a result set, and you can NOT fetch() from them. That means mysqli_fetch_assoc() is failing, and returning a boolean FALSE. You then use that boolean false as if it was an array, and are basically doing the equivalent of
$_SESSION['id'] = null;
Note this:
php > $foo = false;
php > $id = $foo['id'];
php > var_dump($id);
NULL
You want
$_SESSION['id'] = mysqli_insert_id($conn);
instead.
It is an error with you SQL query.
$sql = "INSERT INTO users (first,last,uid,email,pwd) VALUES ('$first','$last','$uid','$email','$pwd')";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
The first line of the code is an INSERT command. The second line executes this command by sending it to the server. If query is properly processed then MySQL server doesn't return you anything, so $result will equal to true. It wil not contain any data from the database. So you can't fetch it, what you try to do in the third line. Need to make a separate query for data.
I have this PHP function that I want to use to retrieve a column, "username", from a table called "members". I have used the below function before, and had no problems. But now when I try to use it on a different project it won't work.
Here is the "functions.php" page code:
<?php
include 'connection.php';
function getusername(){
$query = "SELECT `username` FROM `members` WHERE `ID`=`".$_SESSION['user_id']."`";
if($result = mysqli_query($con, $query)){
while($row = mysqli_fetch_assoc($result)){
return $row['username'];
}
}
mysqli_free_result($result);
}
?>
On my login.php page I have a session variable that stores the user ID from the table in "$_SESSION['user_id']". I have echoed out the user ID so I know that the user ID is set.
On the page that I want the username echoed to, I have this snippet of code:
<?php echo getusername();?>
I also have the functions.php page "included" on the page where I want the username echoed to.
Your $con isn't set inside your function, so mysqli won't work. Additionally you're using backticks around the value you're searching for $_SESSION['user_id'] backticks are for column names, you should use ' around values.
try it like this:
<?php
include 'connection.php';
function getusername($con){
$query = "SELECT `username` FROM `members` WHERE `ID`='".$_SESSION['user_id']."'";
if($result = mysqli_query($con, $query)){
$row = mysqli_fetch_assoc($result);
mysqli_free_result($result);
return $row['username'];
}
}
?>
Then call it passing $con to the function:
$username=getusername($con);
On a side note your mysqli_free_result doesn't work if there are results as the return will stop the function. I updated the function a bit for it to work as you expect.
I really got a problem now. I tried for decades and I can't find why it is not working. I want to get the user that is logged in to see their email on a specific page. I tried a new code now and i get this error: Notice: Undefined variable: row in
The code I use is:
<?
$username = $_SESSION['username'];
$sql = "select * from users where username=" . $username . "";
echo $sql;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
}
?>
AND
<?php echo $row['email']; ?>
<?php
$username = $_SESSION['username'];
$query = mysql_query("select * from users where username='".$username."'");
while ($row = mysql_fetch_array($query)) {
$email=$row["email"];
}
echo $email;
?>
try this.
don't use mysql_* functions
I think... Problem is in SQL query. I propose your column "username" is something like VARCHAR(50). So you have to add quote.
$sql = "select * from users where username='" . $username . "'";
I see a bug, and a design problem.
You've designed your script so that you're printing whatever was last assigned to $row in the condition of your while loop.
You're getting the error because the query is not returning anything and the loop is not running. Therefore, $row is never assigned. That being said, you probably don't want to use a while-loop if all you're trying to do is display the value of the "email" column in the first record returned. If you did want to, then stop it.
Call mysql_fetch_assoc() on your $result (doesn't return as much data), and check that it doesn't return FALSE (one or more records weren't found).
if((row = mysql_fetch_assoc($result)) === false)
die("Error.");
?>
Email:
since PHP can't be used in Spotify apps directly I am using it on my server only, but I am having problems getting the sessions to work, is it possible to use sessions even?
I want to use it to see if a user is registered in my database or not so I am doing it like this:
session_start();
$query="SELECT * FROM user WHERE facebookID = {$fbid}";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
if ($num_results > 0)
{
echo"THERE IS A USER!";
$row['facebookID'] = $_SESSION[fid];
}
else
{
echo"THERE IS NO USER!";
}
the authentication to the server works fine but the session does not seem to be working, on another PHP page witch is being called after the above code has been called with some ajax code I simply wanted to try this:
session_start();
if(isset($_SESSION['fid']))
{
echo"SESSION EXIST";
}
else
{
echo"NO SESSION";
}
anyways is there a special way to do this in Spotify apps or do I have to do this in some other way?
Where do you set $_SESSION[fid] to be the FacebookID?
Maybe you should do this:
session_start();
$query="SELECT * FROM user WHERE facebookID = {$fbid}";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if ($row)
{
echo "THERE IS A USER!";
$_SESSION['fid'] = $row['facebookID']; // SET THE FACEBOOK ID IN THE SESSION
} else {
echo"THERE IS NO USER!";
}
BTW always put quotes around the key names when you access arrays in PHP, like this: $_SESSION['fid'] not $_SESSION[fid], otherwise PHP will first check for constant named "fid" then cast to string - its slower and more important - uglier and bad practice.