Database management php mysql - php

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

Related

For each loop - setting session variables

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"];

Get the Item id when creating dynamic pages on wordpress

I've been searching for several days now trying to find how to retrieve the item id of a select item from a mySQL database on a wordpress website.
I have a database of several hundreds of recipes. They are all displayed in a list on a certain page. I then want to be able to click on one of the recipes in the link and be sent to a unique recipe.php page.
The list of all recipes is on main-page.php. On this page, I have created the following:
echo '<td class="recipe"><a href=recipe?id='.$id.'><div class="recipe-container"><img src="'.$img.'" id="recipe-image"/>';
echo '<div class=recipe_name>'.$nam.'</a></div>';
As you can see, clicking on a recipe with the id of 4 would send you to www.example.com/recipe?id=4.
My question is, how do I tell the code to recognize that we are on the page for recipe #4, so that I can display its unique details? In particular, what do I write in recipe.php so that I can display the unique content for the selected recipe?
create an onload() event on the recipe.php, then check for $_SERVER['QUERY_STRING'] to retrieve id value and retrieve data from database based on the value
Found the awnser. All I had to do was:
$id = $_GET['id']
Had no idea it would be that easy.

1 php page, different contents based on an identifier

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']}";
// ...
}
?>

how to get table updated only once after page reloaded

i made an application where user have some points(like stackoverflow reputation), if someone want to see other answer then his 2 point will be deducted.
so what actually did i passed que_id and uniq_id into url or link, and on the basis of that value i updated his POINTS with -2.
see my url is:
<a href='marks.php?que_id =$que_id && uniq_id = $uniq_val' target='_blank' onclick='p_alert()'>Answers</a>
and update table as:
$n_points = $o_points - $cut_points;
$res1 = "update $tab set points = '$n_points' where uniq = '$uniq_val'";
its working fine but problem occurs when page get reloaded and the points cutting simultaneously with each refresh.
so what i do for overcome this(expect header() redirection), any idea suggestion would be highly appreciated.
Thank you
As you've rightly said, you should do a redirect at that point to take the URL away from there. So
header('Location: www.yoursite.com/new-page');
exit();
You can also track the previous clicks by IP or if a user is logged in when clicking by their unique id before performing the update query. To do this you would need to create a separate table with a user ID and content ID for tracking the rep assignment. Then you could do a SELECT to see if content has been rated based on a content id and user id and if not, UPDATE the rep, and INSERT a record into the new tracking table so the next time they go to the URL, it won't add the rep
You have to persist that the user has payed to see that answer.
So you just deducted if the user did not see the answer yet.
Create a user_answer_view with the userId and the answer.
You can do what you think like that:
header('Location: marks.com/page.php');
But it is unsafe. Because the user can set the url with ids and the problem will persist.

php search results

I've seen alot of tutorials on search with php and mysql, but im having trouble with generating a link with the search result. For example say i have an item called "item1" in my db and the user searhes for item , item1 should be returned as a link so the user can click the link to get more information about that product. Does anyone have any scripts , or snippets of code for how to acheive this?Thanks
You probably need to create an item page say item.php that accepts an id which will then search teh database for that item and display the item information.
Your search results will then have to display the name of the item in a link that also includes the id of the item.
<?php echo $itemname"?>
This would of course be in a loop that goes through the list of items one at a time.
When the user clicks the link it will take them to item.php and send id as a parameter.
that's a simply example with no check and no control by datatype or query results. just start from it and do what you need...
<?php
//your db connection
//col1 is where the id is saved
//col2 is the url
$qry="SELECT col1, col2 FROM table WHERE col1='".$_GET['var']."'";
$result=mysql_fetch_array(mysql_query($sql));
echo 'LINK';
?>
edit:
if you want a direct redirect do this instead of echoing
header('Location: '.$result['col2']);

Categories