Currently I use SESSIONS to call for my varibles from a different page,where multiple data are retrieved from mysql database using a while() statement, onto another page. The many retrieved rows has a specific id which is retrieved from the table. Each row also has a link that when clicked takes you to the page 2 to show details of the person marching that id.
For instance on page1, these are the data shown
id | name
1 | Kamena
2 | aman
3 | Forfie
sample of my code on page 1
session_start();
$stmt="select id,name from table1";
$result=$db->query($stmt);
while($row=mysqli_fetch_assoc($result)){
$id=$row['id'];
$name=$row['name'];
$_SESSION['id']=$id;
echo "<table>
<tr>
<td><a href='page2.php'>id</a></td>
<td>name</td>
</tr>
</table>";
}
On page 2, this is a sample of my codes
session_start();
if (isset($_SESSION['id']))
$id=$_SESSION['id'];
$stmt="select id,name from table1 where id=$id";
$result=$db->query($stmt);
$row= mysqli_fetch_assoc($result);
$perID=$row['perID'];
$name=$row['name'];
The problem here is when the link, id which is 1 with Kamena been the name, is clicked to page 2 the id returns 3 on the page 2 mean while it suppose to return id which is equal to 1. Please is there is something I am doing wrong?
Yes you are writing to SESSION['id'] and overwriting it each time, thus storing the last value only. You need to do this...
$_SESSION['id'][$id] = $id;
This way you have access to it, and can access it as a key/value.
So on your next page if the id you want to access is 7, you would access it as such...
echo($_SESSION['id']['7']);
But a problem I see, is that you don't know which ID you clicked to be able to access on the next page. In your link you need to somehow reference it, then check if its in the session. Which kinda defeats the point of the SESSION variable anyways. Because then you could just pass it as a GET request
Like so,...
<a href='page2.php?id=7'>PAGE 2</a>
And on page two...
if(isset($_GET)){
$id = $_GET['id'];
}
$stmt="select id,name from table1 where id=$id";
$result=$db->query($stmt);
$row= mysqli_fetch_assoc($result);
This is completely unrecommended though, just highlighting the fact that you need to somehow pass it to the next page, and SESSION isnt the way since you need a way to reference that session somehow anyways. If you know what Im trying to say.
Related
I have a listing page (mysite.com/listing) in PHP that uses a foreach loop. It outputs a user ID e.g. user1, user2 and a link to a detail page eg: mysite.com/detail
Output:
1. User ID = user1 Detail Page
2. User ID = user2 Detail Page
...
10. User ID = user10 Detail Page
The detail page (mysite.com/detail) needs to receive the corresponding user ID from the listing page e.g if user clicks item 1 "user1" is set in a Session variable and passed from the listings page to the detail page.
In the detail page then I want to just output "The users id is user1".
I am wondering how Session variable work in a foreach loop or is there a better solution?
If I use something like:
$_SESSION['user_id'] = (string)$user->primary_id;
echo $_SESSION['user_id'];
..in a foreach loop in the listings page it will capture the last User ID in the loop e.g. user10
and
echo $_SESSION['user_id'];
...in the detail page will output 'user10' instead of 'user1'.
Is it ok to even use session variables in a foreach loop?
Thanks
"user1" is set in a Session variable and passed from the listings page to the detail page...there is no need to do that. Sessions are not the best way to achieve this (not least because that model of transferring data breaks if you have the site open in more than one tab in your browser).
Just put a query parameter in the hyperlink URL and retrieve it using $_GET.
e.g.
User 1 Detail Page
User 2 Detail Page
...etc.
and in the "detail" page:
$userID = $_GET["user"];
I have two tables (blog_members and blog_posts) which are related 1 to many, 1 member to many posts. Therefore, in order to be able to relate the two I had to make one field in blog_posts named memberID and set up the relation between this field and the one from blog_members.
Now, I'm trying to make a script to add posts into the database. The thing is, now I have the field memberID in blog_posts which needs to be the same with the one from blog_members in order to be related. So, I'm trying to get the current logged in memberID from the blog_members so I can introduce it into the blog_posts.
I know this can be done with an input where you can type your ID but it doesn't feel right, I want this to be in the back, not to be seen.
Short story:
$memberID = get current logged in memberID from blog_members;
//insert into database
$sql="INSERT INTO blog_posts (memberID, postTitle,postDesc,postCont,postDate) VALUES('$memberID',$postTitle','$postDesc','$postCont','$postDate')";
$result=mysqli_query($link,$sql);
if($result){
echo '<p>Added successfully!</p>';
}else {
echo "Error: ".$sql."<br>".mysqli_error($link);
}
Normally you would store the ID of the logged in user in a session:
$_SESSION['login_userid'] = // THE USER ID OBTAINED FROM LOGIN
Now it will be stored in the browser as $_SESSION['login_userid'] and you can just put this to the top of your code:
$memberID = $_SESSION['login_userid'];
On every page where you use sessions you must run session_start() before the first line of HTML code. If you are not sure how to create your own login system, have a look at this tutorial.
Note: memberID is not unique in the blog_posts table, as one user can create many posts. You should probably create a primary key blogpostID as well.
I want to make a PHP page that will changing it's content based on some sort of an ID.
The idea is: the index page will have 4 squares, the content of those 4 squares will be the top 4 records in the database, they obviously have their own IDs in the DB.
What I want to happen is when I click on one of the squares it will pass the ID to another PHP page that will get all the details about it in the page.
To be more clear:
Lets say it is a cars website, the 4 squares would be an image of top 4 cars in the database with the IDs 1,2,3 and 4 respectively, when I click on the car's image (lets say 1) I will be directed to a PHP page called CarInfo.php
what i want to happen here is for the ID of the square to be passed to this page (maybe page will appear something like carInfo.php?id=1) and the page will load all the information from the database where the ID will match the recieved ID (in this case the record with ID = 1).
The problem is I don't even know how to start doing it... How can I pass the ID? How can the other page receive it? And can I use a variable to pass it to so I can use it in the query carteria? If so how?
Note: there will be 1 PHP page that displays the information ONLY (aside from index), all content will be dynamic.
very easy. exactly as you have said. you will use address with parameter, like
carInfo.php?id=1
and on carinfo page you can use php get variable http://php.net/manual/en/reserved.variables.get.php
$_GET["id"]
it will give you selected id, and you can then use is in database
In the first page you have to do something like this for each car:
Click Here
Click Here
Click Here
Click Here
Then, in show.php you need to write a code like this to recive information form the provided ID if the database you're using is MySQL:
<?php
$db = mysqli_connect('localhost','USERNAME', 'PASSWORD','DATABASE');
$result = mysqli_query($db, "SELECT * FROM YOUR-TABLE WHERE id = '{$_GET["id"]}'");
while($row = mysqli_fetch_assoc($result)){
echo "Color : {$row['color']}";
// ...
}
?>
I am a student still new to php and mysql,i am developing a website which is database driven. It has two pages i.e index.php and subpage.php.
On the index page there appears 14 links namely about_us,projects,services,partners and among others. And for the case of the subpage.php, it has "a blank body",the header, navigation and the footer.
My goal is to not to create pages for every link that appears on the index.php. So i want to use only the "blank body" in the subpage.php to display the data for every link that is on the index.php whenever it is clicked on.
In my struggle to achieve this, i have created a database with 14 tables so that each should cater for every link on the index.php.
So I would like you guys to help me how i can RETRIEVE DATA FROM THE DATABASE FROM DIFFERENT TABLES ON TO THE SUBPAGE.PHP
Forexample;
If am am on the index.php and i click about_us, it should ONLY retrieve data from the table called about us in the database.
And if i click another link it should on retrieve data specifically for that link i have clicked on.
Here is the sample php codes that am using to retrieve data for only projects_table onto the subpage.php
<?php
require_once("connection.php");
?>
<table>
<?php
mysql_select_db("cognative_db",$sql);
$sql="SELECT * FROM projects_table ";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['project_title'];?></td>
<tr>
<td><?php echo $row['project_details'];?>
<?php echo " ";?>
</td>
</tr>
<?php
}
?>
<?php
mysql_close();
?>
</table>
Any help will be highly appreciated
I can't agree with your database design concepts. But to problem of having one page for all links , one way can be - you can create links like this
http://yoursite/subpage.php?page=content
http://yoursite/subpage.php?page=aboutus
On your subpage.php
$page= $_GET['page'];
depending upon the value of $page you can choose which query to run and what to display in the page.
To add to all these, you don't need to have 14 tables to cater 14 links. Perhaps you can start looking at Joins
simply create simple table structure for your website,like below one instead of assigning one menu one table
|id | menu | content |
|1 | about_us | asds.... |
|2 | services | asds.... |
I don't know why for every link you made the table. But just for displaying multiple table data, you can pass table name in query string as:
Click
// and on subpage.php retrieve it by get method as:
$table=$_GET['q'];
//and make your sql query
$sql="SELECT * FROM $table";
I started learning Php scripting today only,i want to create an auction website for my college. I have a bid table having the following columns:
Userid
Itemid
Bidamount
When a user logs in ,and goes to "Items.php" page and when he will click on the bid option on an item (each item has an itemid)he will be redirected to "bid.php" page.
My doubt is: I will get the useid by $_SESSION['userid'], will i similarly also get the
itemid automatically by the $_SESSION['itemid'], if not how do i pass the itemid to the bid.php page.
another way is to to use an href tag to pass a variable to your bid.php and fetch it using get.
i.e
Bid now!
in your bid.php
$id = $_GET['id'];
you would create a from and use POST or GET information that is passed by the form. you would not get this from session
here's a tutorial: http://www.tizag.com/phpT/postget.php