Page 1 of 1
MySQL: Add one to an integer in DB
Posted: Sun Sep 06, 2009 5:34 pm
by azylka
Hi, I found this code but, I'm new to MySQL and I don't know how to implement this into my code. Could you label where the DB name, and table names go? I feel stupid, but I don't have enough time to waste trying to figure this out. And what's all this $pageid and $link mumbo jumbo? I just want to be able to add one to a field in the DB called: "suz", and a table called "account_f".
Cheers,
Alex
Code: Select all
<?php
$sql_query = mysql_query("SELECT `number` FROM `numbers` WHERE `pageid` = '$pageid'", $link);
while($rs = mysql_fetch_array($sql_query)) {
$num = $rs[0] + 1;
}
$sql_query = "UPDATE `numbers` SET `number` = '$num' WHERE `pageid` = '$pageid'";
if(mysql_query($sql_query)) {
echo "Record updated successfully";
}
?>
Re: MySQL: Add one to an integer in DB
Posted: Sun Sep 06, 2009 5:41 pm
by Darhazer
`number` is a fieldname, `numbers` is the table name
the $pageid is to identify to which row to add 1
$link holds the DB connection handle... you have to connect to the database before you can update anything in it.
The same operation however can be implemented with just one query:
[sql]UPDATE account_f SET suz = suz + 1[/sql]
Without WHERE clause however, this will update all records in the table.
Re: MySQL: Add one to an integer in DB
Posted: Sun Sep 06, 2009 5:51 pm
by azylka
Ok. Here's what I need. I have a file hosting company. i want to store the number of files uploaded by a user and display them on "account.php". How would I do this with MySQL. (After they upload a file, using the GET method of PHP, I want to be able to direct them to a page that takes their username and somehow adds one to the amount of files uploaded for that username) How would I do this with MySQL and PHP?
Re: MySQL: Add one to an integer in DB
Posted: Mon Sep 07, 2009 8:39 am
by azylka
Ok. So I've found this code. But I don't know what the "id" on the last couple of lines means. Can anybody explain what the "id" is for, and how to use it?
Code: Select all
<?php
include('connect.php');
$user = $_GET['u'];
UPDATE
files
SET
$user = $user + 1
WHERE
id = row_id
?>
Cheers,
Alex
Re: MySQL: Add one to an integer in DB
Posted: Mon Sep 07, 2009 8:59 am
by jayshields
The id fields are generally primary keys by which you can identify individual records in the table.
The code you have there won't work as it's just an SQL query put straight into a block of PHP code.
Re: MySQL: Add one to an integer in DB
Posted: Mon Sep 07, 2009 9:03 am
by onion2k
azylka wrote:Ok. Here's what I need. I have a file hosting company. i want to store the number of files uploaded by a user and display them on "account.php". How would I do this with MySQL. (After they upload a file, using the GET method of PHP, I want to be able to direct them to a page that takes their username and somehow adds one to the amount of files uploaded for that username) How would I do this with MySQL and PHP?
Presumably you have a table in the database with records of all the uploaded files, right?
[sql]SELECT COUNT(`file_id`) AS total_files WHERE `user_id` = 1[/sql]
That'll give you the total number of files that have a user_id value of 1. You could maintain a number in the user record but I wouldn't recommend it... you'll need to add to it when the upload, reduce it if they delete something, make sure it never gets out of sync with their actual uploads... that'll be annoying. It's much easier just to count the number of records that are associated with their id.
Re: MySQL: Add one to an integer in DB
Posted: Mon Sep 07, 2009 11:46 am
by azylka
onion2k wrote:azylka wrote:Ok. Here's what I need. I have a file hosting company. i want to store the number of files uploaded by a user and display them on "account.php". How would I do this with MySQL. (After they upload a file, using the GET method of PHP, I want to be able to direct them to a page that takes their username and somehow adds one to the amount of files uploaded for that username) How would I do this with MySQL and PHP?
Presumably you have a table in the database with records of all the uploaded files, right?
[sql]SELECT COUNT(`file_id`) AS total_files WHERE `user_id` = 1[/sql]
That'll give you the total number of files that have a user_id value of 1. You could maintain a number in the user record but I wouldn't recommend it... you'll need to add to it when the upload, reduce it if they delete something, make sure it never gets out of sync with their actual uploads... that'll be annoying. It's much easier just to count the number of records that are associated with their id.
So, I create a table for each user when they register, but what do I do when they upload a file? Can you give me a rundown on how to do this?
Alex
Re: MySQL: Add one to an integer in DB
Posted: Tue Sep 08, 2009 3:26 am
by onion2k
azylka wrote:So, I create a table for each user when they register
Err.. no. Just one table containing all the data about everyone's uploads.
, but what do I do when they upload a file?
Add a record to the database table that stores all the information about the upload and the user's id. Then when you want a list of all the user's uploads you can get it with a simple SELECT. If you want to know how many things they've uploaded you can use the query I posted earlier.
Have you ever read a book about using databases? I recommend that you do. Having a good grasp of how to store, retrieve and update data is actually more important to writing a good website than even knowing how to write code. If your data isn't stored properly your website
will suck.
Re: MySQL: Add one to an integer in DB
Posted: Tue Sep 08, 2009 6:23 pm
by azylka
No, no, I mean I have a database of the users and passwords, and was wondering if I could somehow use that for this type of thing. I'm new to MySQL, and didn't want to have to go about reading books and all that crap like when I learned HTML and PHP. Plus, I don't have too much access to that, BECAUSE I'M ONLY 13!
Re: MySQL: Add one to an integer in DB
Posted: Wed Sep 09, 2009 10:27 am
by onion2k
azylka wrote:No, no, I mean I have a database of the users and passwords, and was wondering if I could somehow use that for this type of thing. I'm new to MySQL, and didn't want to have to go about reading books and all that crap like when I learned HTML and PHP.
If you want to learn stuff you'll have to read (either proper books or stuff online). Learning from people on forums like this is great, but it'll be waaaaaay slower than the pace you want to go at.
As it is, yes, you can use your existing users table.
azylka wrote:Plus, I don't have too much access to that, BECAUSE I'M ONLY 13!
http://books.google.com/books?q=mysql&btnG=Search+Books