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.
Related
Get the last record id inserted to db and pass to redirect url
my url is employeesview.php?showdetail=&id=
so id mus go after &id=
thanks
Question could be improved (e.g. what DB to be used, why do you think you have to provide the ID as HTTP GET argument, etc.)
But the answer could be easy. Just guessing you are using a mysql db (because php beginners often use mysql).. in this case please check auto_increment attribute for your ID column in sql. It allows that PHP automatically uses the next ID for your new records, and you don't need to provide the ID for inserting a new row.
See https://www.w3schools.com/sql/sql_autoincrement.asp
I developing a chat app, in that i need to connect between two users in my database and the point is i dont want to connect users that already connected earlier. So i store a column to store the already connected users which will be in the format: id1|id2|id3|.....|id50.
First i thought of selecting a user and get his id and then fetch the user's connected column and then by php check if id exists in his column, thus denying that chat. But it makes lot more complicated.
Can anyone recommend a strategy to make this simple by using only MySQL Queries, i am not advanced in writing nested or join queries and the don't know all pre-defined functions that exist in MySQL.
Can anyone make a suggestion?
SELECT * FROM table WHERE column IN (this,that,these,those,em)
if statement returns not empty value, sure, user exists.
I wrote example where you can check many users at once. Just implode your id-user-list and quire em in parenthesise..
Thus, u'll need to parse returned array to see who succeeded in selecting, who not. (compare two arrays)
Happy coding..
When a few users try to insert a row into the table, this error appears:
(325 is the ID of the row. there is no auto increasment because I'm doing it myself because I need it.)
I remember that in asp they have the lock() method that actually prevents this kind of errors.
Is there something similiar in PHP?
Code will be added if asked although I dont think its required.
Thank you very much!
Now you see why you shouldn't assign IDs manually :)
Change your table to start assigning IDs automatically:
ALTER TABLE your_table AUTO_INCREMENT=last_assigned_id+1
If you insist on implementing a lock, you can use sem_acquire
In a customer CMS the customer can update their personal information, like change their address and first/last name. I use a mysql UPDATE query for that.
Problem with working like this is, is that people can change their information. E.g. change their name from john doe to asdfdas. I would like to SAVE their old information.
What are my options? What is the best way to do this?
Assuming the user has a unique ID you could have an old_user_information table and when you do an update also do a new entry into that table. The table would have an autogenerated ID as well as the unique user ID and the rest of that users past information.
A user could have multiple rows in this table but only one row in the real Users table.
Edit: If I were you I would write a stored procedure that does both of these things so that it is easier to manage if things change.
You can make table that contains something like this
`yourTableID, field, value, date`
and update this with a trigger. You write an update trigger that adds the old value if it is changed. Look at the manual here to find out more about triggers.
If you don't want to use triggers you could obviously do the same in your logic: just update the history table with the old value. But this needs some trickery to find out if you need to update it, but nothing to complicated.
For easy retrieval what happened you might want to add something like "oldvalue" AND "newvalue", but the latter isn't really needed, as it is either in the next update as 'old' value, or it is the current value.
Create new columns for the data being updated column would be prior_to_update_column
On update, move the old info into prior_to_update_column
If the user updates again, append to the prior_to_update_column seperated by , (to look like an array).
This should keep all the previous info the user updates
add an additional field name version
and use Insert instead of update
あの答えのとうりに、やってない。
UPDATE table name SET column=column+'new value' WHERE condition
So basically what I am trying to do is when a user of my site creates a new account on our register page, I'd like the primary key from the newly created row on the User table (basic info table, email, password, etc.) to be inserted into a new row on the Profile table (more descriptive info, about me, display name, etc.)
I'd like to do this in PHP and any help would be appreciated.
if you are using mysqli look at:
http://www.php.net/manual/en/mysqli.insert-id.php
Get the id after your first insert and then use this in your next insert.
If doing it "in php" isn't really a requirement, then you can use MySQL's built in Trigger mechanism to do this update.
Triggers cause something to happen AFTER or BEFORE an event(INSERT, UPDATE,DELETE)
So your trigger would be:
CREATE TRIGGER thistrigger AFTER INSERT
ON User FOR EACH ROW
UPDATE PROFILE SET "whatever"
On Triggers: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
I think there isn't a really elegant way in MySQL, basically because INSERT doesn't return anything. PostgreSQL does allow for an INSERT ... RETURNING clause, but that's an extension.
That said, if you're using the mysql_* functions in PHP, you can use mysql_insert_id, which might suffice for your needs (i.e. if your primary key is an AUTO INCREMENT integer).
If you are using a mysql database, you could alternatively do another query call from php with the following query:
"SELECT LAST_INSERT_ID();"
More info about it here: http://www.jpgtutorials.com/mysql-last_insert_id-function
It is connection specific. Concurrent inserts from different connections won't affect the current connection.