Logic issue with changing a variable in PHP - php

I'm a total noob when it comes to PHP, as I'm sure you'll discover once you read this question.
I'm messing around and trying to learn, and I've made a chat room using PHP. I'm trying to add functionality so that a user can change which room they are in by using a drop-down box with some pre-defined rooms. I have a login page which asks for a username, password, and which room (the dropdown box). The login page then directs to the index.php page which is where the rest of the chat code is at. The index page has a section that checks to see if the user has changed rooms. If they have changed, it displays a message in the OLD room to say that they've left, and a welcome message in the NEW room.
The issue that I've run into is that when I log in, the room variable gets broken somehow. It puts me in a "blank" room. If I use the drop down box after that to change rooms, everything works fine. I'm just having issues with the initial login.
I tried removing the change room "routine" completely, and I'm able to log in just fine (but I can't switch rooms, obviously) -- so that leads me to believe that there's a logic error somewhere here.
Sorry for the long-winded description - I'm new here and I don't know how much info to provide.
Here's the code that I have that checks/changes rooms -- anyone have any recommendations?
//check for room change
if(!($_SESSION['room'] == $_POST[r])){
$query = "INSERT INTO roomdata (message, room) VALUES (' * $_SESSION[username] ($flags$ip$flags) has joined the room. Welcome!!', '$_POST[r]'), ('* $_SESSION[username] has left the room.','$_SESSION[room]')";
$result = mysql_query($query);
$_SESSION['room'] = $_POST['r'];
}
Again, I'm new to PHP and I'm sure my code is pretty crappy. I'm also aware that mysql is depreciated, but I'm just doing this for a hobby and I'll learn mysqli once I've got this sorted out.
Thank you in advance to anyone who has time to help!

Related

User database structure

Im not a webdev, Im learning all this stuff on the go and I stuck with this stage with my project.
I have a website people can register to use it, so I can store users already. I have a table with different items (item_id, item_name, item_owned). My problem is, every users should have the same list of item, but they can select if they have this item or not on thier own profile.
I could do it make the db and the items are selectable, the db shows 0 if you dont have it or 1 if you have it. But every user sees the same item_owned data and I need it to be user specific.
How can achive this? What structure should I use with the database? Do I need any special libraies beside HTML, PHP, SQL?
Thank you!

Creating x amount of pages equal to x ID's in database?

The title may be a little vague, but I'll try to explain the concept here.
I have a site in which daily new items ( sports games ), get published, now what I would love is that my users would be able to click on a certain game and get redirected to a new page with more detailed information about that one game.
The thing is, creating every php page seperate for every game each day is a lot of work because also a lot details in the page need to be changed.
Now I was wondering if it is possible in some kind of way that there's a script that reads, OK you have 5 games today, page 1 = id number, title of the page is the matches name, extra info is the info that stands with that id.
I don't know how else to explain this so I hope this was good enough.
Thanks for your time and reading this :)
You can use requests to select the page (the best way is to use $_GET as suggested by gbestard) to select the ID. Then you can have some static content (like url or short article or link to screen shot or something similar) stored in your database under the same ID as the game, and upon clicking on the link to the game, the page is reloaded and you populate it with the new content, that was stored in the database.
You can even store entire pages with static html (as in article or short game description in your case) content and etc in your database and simply call them to populate a div.
If you have something like a custom made admin section of your site, you can add a page to edits it with a js plugin like ckeditor and the creation/editing of articles will be a piece of cake.
It's pretty much a standard case of a CMS(Content management system).

Refresh values in a database

I am really new to database management. I installed wamp and have set up a database in phpmyadmin. Now I need to refresh the value of a particular row which contains some data. How do I do this. Are there any good tutorials for doing this available on the net. Can you please point me in the right direction as I don't know what to do now.
It's called "UPDATE".
mysql_query("UPDATE table_name SET field_name = new_value");
If you add a condition to the end, you can decide which record get's updated.. Assuming you have an "id" field (which is unique to each record), you could change the record with ID 50 by appending:
where id = 50
to the query.
I think you are referring to the visual thing more than what is happening on the background. If you hit the button, lets say, update, the page will go to the server and come back with the new, updated results of what you are trying to update. Before digging into using complicated techniques to make this happen nicely, try to understand the page lifecycle. Then start applying javascript(and AJAX) if you want to make this happen nicely. Hope this helps,

php report button

I am working on a search script and on each search result I want a report link button. I am not sure how to make one. I have looked on google for answers but cannot come up with anything. Can anyone give me an idea of what to do? or does anyone know where there is an example of this? I plan on using the link id and making a new table on mysql to send reports to. I am just looking for a basic button to send reports to mysql I am just not sure what would be the best way to do it. I have the data for the link id's I just need to be able to report it to a new table I am assuming. Any suggestions or examples are very appreciated. Thanks.
Chris,
First you will want to create that new table in your database to capture this information. Lets assume to you will have the following fields (very basic, you may want to add more): ReportId, LinkId, DateReported. ReportId is our primary key, LinkId is the ID you reference in your question and the DateReported is the server time we logged the transaction.
With this table created you are going to want to create a new php page, lets call it report-link.php. You are going to want to make this page accept a querystring variable called linkid (accessible in the $_GET[] collection). In the code of this page you will want to write a SQL query that inserts the value of the linkid parameter into the new link report table in the database (along with the date()).
On your search page you will be able to have users report an entry by clicking a link with the href of /path/to/report-link.php?linkid=<?php echo $link_id; ?>
Please note this example is very simplistic in nature, and offers no security for spamming, pleasant end user experience after they click the link, but this is the process you will want to follow in setting this feature up. After you have it working you can spruce up the experience for your users.

Let users choice modules they want

I'm making a social gaming network, for learning but mostly because php is alot of fun!
Anyway I want this future..
I wanna let my users chose which "modules" they want activated, like a message board on their profile, polls, buddy list, contact box. Some modules will have to be purchased with points.
Im thinking on something like this:
id
username
passwd
points
msgboard_activated
poll_activated
buddylist_activated
contactbox_activated
Just check if msgboard_activated equals to 1, then include that module on members profile
Some modules (feel free to come with suggestions of modules):
Message board (lets users leave messages on a specific users page)
Buddy list
Poll
I pretty much got the idea from (awesome site!):
http://www.fpsbanana.com/modules
Anyway, Let me hear your thought and idea of this!
Thanks alot and have a good day!
ps.(im BR so expect spelling errors etc)
Oie, Agamemon!
I would encourage you to try a different structure. For instance, three tables, as opposed to a single table.
First, your users table which represents everybody on your site. Second, a modules table which represents all possible modules. This way you can add more in the future without having to add columns to the user table, etc. And lastly we bring them together into a relationship with the usersmodule table, which shows what modules belong to each particular user.
I hope this gives you some direction.
table:users
userid
username
...
table:modules
moduleid
modulename
...
table:usermodules
userid
moduleid
...

Categories