What is the best way to create member profiles in PHP? For instance, some websites read in the address bar: profile.php?id=11233 and then profile.php?id=13563. How is this actually done? As of now, I am saving such types of URL's in MySQL, but where will I write the actual code? Will I have to write the code in profile.php? Or will I have to make separate files for each id? How does it work?
Will I have to write the code in
profile.php?
Yes. Visitors will be visiting profile.php and there will be a default variable set named $_GET['id'] that has the value in the URL, like 11233 or 13563. You can then query the database for the user with that id and display the proper information.
If there is more than one variable, like profile.php?id=123&type=cake, then you will have two variables: $_GET['id'] = 123, $_GET['type'] = 'cake'. It is stored in $_GET because GET is the method used to access the page. Find out more at http://php.net/get
There is also another common method, called POST. This is used when forms are submitted with method=POST. In that case, the information will be stored in the $_POST array.
Yes, in those example URLs, the database id is being sent as a parameter which would be parsed by PHP.
The basic steps would be
get database id (from what's called the $_GET superglobal in this case). Make sure it's an integer, using intval() or (int)
set up your database connection (I use PDO)
build a query, something like select * from profile where id=?.
execute the SQL query
check your results
if you have valid results, print out the information in the profile
The details of how to do each of those steps is the meat of the matter, of course. I'm sure folks around here would be more than happy to answer questions about how to connect to and set up a database, and use the results in PHP.
A simple example database would be set up something like
CREATE TABLE profile(
id int NOT NULL AUTO_INCREMENT,
user_name varchar(32),
bio varchar(1024),
favorite_flavor enum('chocolate','vanilla','fruity'),
image_id int default null,
primary key(id)
);
Related
In my app, any user can create a post object which contains a title, description, and ID. These post objects are stored in a database within a server. However, I need a way to assign unique ID's to the posts, so I can assign the user to their post (only the ID of a post will be stored in the core data of the poster's phone).
Right now I'm using integers as the ID. For example, when a user submits a post, I have it so the database is queried and I find the ID of the last post submitted and then make the ID of the current post being submitted one more than the ID of the last one. Then the post is uploaded to the database.
I feel like this is bad practice because if 2 people coincidentally submit their post at the same time, their respective posts could be given the same ID.
So, what I'm asking is how should I assign some sort of ID to an object in the database to guarantee it to be unique? Should I assign it in a php script and then somehow return that ID back to Xcode? Sorry, I'm new to this.
You can simply use an auto increment id for your posts, so the database does that job for you.
CREATE TABLE post(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
description VARCHAR(255),
PRIMARY KEY (id)
);
Simply check Auto increment example in the mysql documentation
after adding data to the table you can fetch the last id of the inserted post with the last_insert_id command.
so if you use PHP as programming language and you are using mysqli for the database access, you can use mysqli_insert_id to fetch the last id (see the example in the php.net documentation for better understanding)
I don't know how to program webpages very well so I have the following question. I have a MySQL database which a user can manage. I have two tables: persons, videos.
I want my page for the user to select a person from the persons table (by name for example) and then associate this person with a video link.
Selecting the person from database is easy,
SELECT personID, name
FROM persons
WHERE name LIKE '%John%'
So now the user has to insert a video link to the videos table which has a foreign key the personID.
I could store the primary key of the person after the "select" in a hidden html input form but I assume this is not very secure as they can edit it?
My question is: Where would you store such a key value in between a select and an insert call?
Is it safer to store all this data in a session instead?
This sort of thing happens quite a lot, and it's completely fine as long as you don't mind people seeing your primary keys (if they're numbers, it shouldn't be anything to worry about).
Let's say you're retrieving a list of rows from a table, and displaying them in HTML. If you want each table row to have a 'delete' button for deleting that row, both from the DOM and in the Database, say via an XMLHttpRequest, it's typical for each row to have an "id" attribute, with the id being the primary key value of the row you wish to delete.
Typically, your primary key will be:
The id field in your table
The field with AUTO_INCREMENT set
In short, give each dropdown a data-id attribute, which you access in your JavaScript via the .data selector. That's good enough. Just make sure you're also protecting yourself from SQL Injection and using PDO and prepared statements, and you'll be fine.
For extra protection, and to make sure nobody has altered the data-id attribute client-side before submit, check that the id corresponds to the name field in the database, and you're golden.
I think storing this type of data in the session is secure and a good choice. After all, it's session-scoped, so there's no reason to output it.
It would be more secure to store it in the session and, ofcourse, easier to access and manage.
As a part of a script I'm writing I would like to add a templating system where the variables are derived from a database table like this:
id int auto_increment primary key,
name varchar(200) not null,
value float(6,2) not null
My problems are:
1- I am not able to find a way that makes it user-friendly to display those variables when editing a page & I don't know how many variables there would be so I cannot add all-in-one form.
2- I don't know how the variable should be saved to the database (I'm looking to save a reference of the variable not its value so when it's updated on the table, all pages that uses it will have the new value) ... but using the variable number doesn't seem to be clear on what it holds when someone first looks into the page while editing.
3- Should I use something like preg_replace to replace those variables or is there a better method to do that?
If i understand your question correctly, you are seeking a way to extract column definitions from your database to build a web form regarding to the fields defined in a database table? if this is the case, you can obtain the structure easily with
SHOW COLUMNS
FROM database-table
Even better would be to use the information_schema database (available since MySQL 5.x), because you wont have to parse anything from it:
SELECT *
FROM information_schema.columns
WHERE table_name = "database-table"
I have a website which generates each visitor a referral link (ex. http://mysite.com/?ref=12345678). The actual referral id (12345678) is a unique 8 character ID (using uniqid() ).
I then just add the ID to the end of http://example.com/?ref=....
I am trying to find a script which can connect to my MySQL database, check if the id exists, and if it doesn't, enter it into the table.
If it does exist it shouldn't do anything.
I am guessing that I need to implement a cookie to check if the id exists, so I don't really need help with that. I'm just confused to how to make the script I mentioned above.
I'm trying to make the table look like this:
Unique ID
---------
3af456yT
Sa32xs21
9af456yT
8a78Fs21
1wsd4Fav
7f3Xv5Bd
Here is a great place to start: http://php.net/manual/en/book.mysql.php. The documentation will provide the information you need to connect to a MySQL database via PHP and to insert data. Specifically, you will need to use the mysql_query() function.
You can use the "UPDATE INTO" syntax to ensure that you do not create duplicate rows. Please see http://dev.mysql.com/doc/refman/5.0/en/update.html for more information.
I'm working on a project to add statistics for pageviews. I'm having trouble looking up and then storing in MySQL from PHP.
I have users stored in the database as:
ID Email_Address Visits
1 email#address.com NULL
2 email#address.com NULL
A variable is passed into the page called $ID which refers to which user has been visited.
Firstly, should the Visits column be a default of 0 rather than NULL?
Secondly, What query can I then use to go something like;
Lookup $ID, add 1 to Visits in that row. ie: Visits = Visits + 1?
Also if $ID is blank, it shouldn't do anything.
Cheers
Yes, it should be default of 0.
UPDATE `your-table`
SET `Visits` = `Visits` + 1
WHERE `ID` = :id
If the id is blank, don't run the query.
Your views column should probably default to zero.
Secondly, this is the query you should use:
update tablename set visits=visits+1 where id=:id
Replace tablename with the name of your table and bind the value of :id to the ID. (You are using prepared statements, right?) Also, if the ID is empty, just don't issue the query.
1) It really depends what you are trying to do. But, from what it looks like, I would default to 0 since it looks like a numeric field.
2) I don't want to write code for you, but I'll give you some links:
First you need to Select. Then add use the built in mysql PHP functions to add to views, then Update. Or you can do it all in one query. But, for beginners, I always advocate doing it in steps.
It also looks like ID is your primary key, which you shouldn't leave blank. Read about primary keys here.