Automatically edit a php table - php

I have a coins table in phpMyAdmin and I need it to be automatically updated when a user completes a purchase via PayPal. The way it is setup is that PayPal will automatically redirect them to a link I choose.
So, how can I run a script that takes the parameters from the URL and adds a new record to the table?
Thank you for all your help.

So that link would need to be a page called something like payment_success.php
On that page you would need to have a script that looks like this:
<?php
include 'credentials.php'; /*this is a file that will contain your database connection info
Then here you will establish the connection and enter the data into the desired table*/
$var1 = 'some information collected from paypal that you want to update';
$id = 'some identifiable information about your user that can connect them to the correct row in your table';
I won't hold your hand through all this but the SQL query you would need would be:
UPDATE table_name SET column_1 = '$var1' WHERE ID = '$id'

Set the confirmation PayPal link to increment the number of coins value in the MySQL table for the particular user....!!!
I hope that will work out..... If I could get a idea of your table or some more info, I could give you a clear idea about that.

Related

mysql - Query to display record just added

I have two pages. One is a form that I use to simply input data that will be sent to my database and the second page that actually takes the data inputted into the form and sends it to the database and is supposed to display the information that I've just added.
Everything works fine, however I'm struggling with the query slightly. What I need it to do is display all the information for the last data inputted to the database.
The query I currently have just displays the data with the highest ID:
$sql = "SELECT * FROM Results ORDER BY ID DESC LIMIT 1";
So as an example I would be left with the following information after completing my form:
Success! Data being saved:
ID = 900 Amount = 206 Date = 2016-12-26
This is obviously just showing the data with the highest ID, but since the ID and all the data fluctuates, I need it to just show the data that has just been inputted.
I came accross this: Query to select newly added records only. But I don't believe this soultion to be viable as the database is external and I don't want to be creating new tables.
I was thinking that it might be possible to assign a hidden value to each newly added record via the query. e.g. New 1, New 2, New 3 etc. Then printing the latest record for New. However, I couldn't find anything on how to do this.
Any help would be greatly appreciated!
You must use this method to have very correct value:
Input form must send to another file that do inserting (we call it here insert.php)
insert.php must insert the data after validation and after that you can fetch the last ID number from database. Depending on the method you are working with it can be different. for example if you are using PDO you can get it by PDO::lastInsertId
after getting the ID you need to forward it to the viewing or editing page. for example view.php?id=LastInsertId. This forward have some reasons:
Codes can be cleaner.
We prevent refresh and resend inserting. for example if you do inserting inside view.php and user hit F5 to refresh the page, The insertion happening again.
This is the whole idea. you can use this method for only one page:
page.php?do=new
page.php?do=insert
forward to the page.php?do=view&id=lastInsertID
why you trying to get just inputted data from database? you can do it using HTTP POST/GET method easily.just send data as parameters and show them in second page.
If you already have the data you are inserting, you don't need to run a query to get it back from the database again, you could just ensure that the query was successful and display the data directly. Anyways:
You can get the insert ID from the last insert using the MySQLi object. For example:
$sql = "<your insert statement>"
$conn->query($sql);
$last_id = $conn->insert_id; //Id of the row you just inserted
$sql = "SELECT * FROM Results WHERE id=$last_id";
This is assuming you do the insert in the same page that you display the result.

SQL/PHP: Calling unique ID

I have made a form page with some radio buttons, text/textarea inputs and a total amount (price) at the end.
It is working/possible to input this into a table in MYSQL with a unique ID (AUTO INCREMENT at 100000).
Here is the situation:
When I submit my page i would like to automatically navigate to another page that still holds the "UNIQUE ID" and the "TOTAL AMOUNT (price)" so I could use it there to put it into another variable that i have to use for the redirection to the payment website.
I thought just to read the last entry in my database but what if 2 people are paying at the same time (no option!).
My unique ID is made into the database itself maybe that is my problem?
Is there somebody who could help me and provide me a walktrough?
Ex. My code:
if(isset($_POST['verzenden'])) {
$firstname = htmlentities ($_POST['firstname']);
$name = htmlentities ($_POST['name']);
$con= mysqli_connect("sqladres","username","password","databasename");
$query = 'INSERT INTO `inputorder`
(`contact_firstname`, `contact_name`)
VALUES ("'.$firstname.'","'.$name.'")';
Now I think I have to use $_SESSION to generate a session ID and also to write the amount (price) into this session and take it to the action page. But I've never used it before and really dont know how to use it in a good safe way!
see similar question to get last insert id:
How do I get the last inserted ID of a MySQL table in PHP?
To redirect with your variables you can use get method as:
header("Location:yourwebsite.com/payment.php?uniqe_id=". $uniqe_id . "&total=" . $total);

How to restrict user from updating same row twice+

I'm trying to create a system similar to a Facebook LIKE button. I want to allow a user to like a picture, but only let them like it once. After they have liked it, they should only be able to unlike it.
My current code is:
<?php
$id = $_GET["picID"];
include 'db.php';
$colID = str_replace('_', ' ', $_GET['picID']);
$colID = mysql_escape_string($picID);
$sql = "SELECT * FROM picture WHERE id = $id";
$result = mysql_query($sql) or die (mysql_error()."<br/>".sql);
$row = mysql_fetch_array($result);
$update = "UPDATE picture SET likes=likes+1 WHERE id='$id'";
mysql_query($update) or die (mysql_error()."<br/>".update);
echo "Thank you";
?>
Currently I use a hyperlink to trigger the 'Like' action:
<a href='like.php?colID=$row[id]'>Like</a>
At the end I'd like to change this to AJAX so the user can simply click Like (as on Facebook) without any page change. I'm still reading up on how to do this. Is there a particular name for this task? Or can someone show me show I could do this?
EDIT:
The user needs to be logged in to be able to like a picture. I have two unlinked tables, User and Pictures. Currently a user can just keep clicking 'Like' and it adds 1 to the like column.
One method would be to create a table that contains columns for picture IDs and user IDs to show that a person has liked a particular picture. If a user ID is listed in the table with a picture ID, then you should not allow them to like the picture since they have already liked it. You can perform this check with a query to the database that will return a count of a 1 or a 0.
Make a string column in your database to indicate who liked the picture.
If the user is logged in (so can vote), search his name in the string by exploding it and looping it. Then remove his name from the string or add it.
Note that you could also save the array directly to the database using serialization: http://php.net/manual/en/function.serialize.php
For a fast loading, you could have an other column with the count of names of the first columns.

Allow user that created entry to modify it only

I have been googling for some time now with out success primarily because the way i am asking is most probably the wrong way.
What i want to know
I have made some PHP scripts that allow users to add data to a table (In a MYSQL datanase) and its displayed on a website, now i am working on the ability to edit and delete the entry however the script i have allow anyone to do this to anyone's entry's.
Now i don't know if there are PHP or Mysql functions that help with this, like i said my goggling has been in vain. So if i could be pointed to a webpage that has this information or better yet an example syntax chunk that will only allow the user that created the entry to modify the entry.
I am assuming there is some type of while or if statement to achieve this.
Example scenario
A user comes along adds an entry the entry is given an id can the user be linked to that id so only he can edit it ?.
Note
I am adding PHP to a word press site so as for the users login information a word press widget is controlling that however i can see the entries are still put in a database field when I'm strolling threw the databases.
What i am asking
Do you know of a webpage/example syntax or tutorial that will show me what i need to know or if ya got enough time explain it to me :).
Thanks
Ben
Update the new entry with the current user's id, where $entry_id is the ID of the new entry
$current_user_id = get_current_user_id(); // current user logged into WordPress
mysql_query("UPDATE `table` SET `user_id`='$current_user_id' WHERE `id`='$entry_id'") or die(mysql_error());
UPDATE: You could also insert the entry with the user_id at the same time with the rest of the data.
mysql_query("INSERT INTO `table` ([other column names], `user_id`) VALUES ([other values], '$current_user_id')") or die(mysql_error());
Check if the current user ID is equivalent to the allowed edit user ID in your database.
$query = mysql_query("SELECT * FROM `table` WHERE `id`='$entry_id' LIMIT 1") or die(mysql_error());
$data = mysql_fetch_array($query);
$allowed_user_edit_id = $data['user_id']; // user id who is allowed to edit
$current_user_id = get_current_user_id(); // current user logged into WordPress
if ($current_user_id == $allowed_user_edit_id) {
// Your code for editing the entry
}

How to create a Profile URL for a user using $_GET['id']

Can someone advise me if I am performing the below steps correctly:
When a user wants to register on the website, register.php handles his/her request. Below is some of the code from register.php:
$sql="INSERT INTO Members (fldFullName, fldEmail, Password, Gender, DOB)
VALUES
('$fname','$email','$pass', '$gender', '$date')";
Particularly when I wrote the above code, I was somewhat new to PHP/MySQL and still am. Therefore, I made all of the fields above manually in the table via phpmyadmin. Furthermore, I also added the ID field manually via phpmyadmin, as the first field with auto increment and primary key of course. Why I did it manually, I can't remember the reason of. But I'm pretty sure that this may be the reason why I'm having problems.
What I'm trying to do is, when a user registers on the website, I want a profile URL to be created for him/her. For example, the field in the table could be named ProfileURL, whereas the actual value could be http://www.domain.com/profile.php?id=1, where the id is inherited from the actual ID in the table. How can I do this with my above code? Did I do something wrong when I decided to save all the fields manually via phpmyadmin? Note: I've also been creating tables, databases, fields manually via phpmyadmin. However, its values are INSERTed automatically of course. Am I even on the right track?
Thank you.
As stated above, you don't need to save a profile URL to the database. I'm guessing all profile URLs are going to follow some standard form (i.e. www.example.com/profile.php?id=1)?
Well, if you saved all of those in your database and then you decided you were going to change the format to something like www.example.com/profile/1 you're going to have a lot of out-of-date data in your database. You're going to have to go through each record and update it, and that could be dangerous on a database table with say, millions of rows.
Therefore, the solution is to have a script that takes a parameter. Say profile.php. As above, you would check for the profile using the data in the $_GET array:
<?php
if (isset($_GET['id'])) {
$id = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM members WHERE id = '$id' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows() > 0) {
$member = mysql_fetch_object($res);
// handle displaying of member's profile here
}
else {
// member does not exist with ID
}
}
?>
That way, if you decide to change the script name or use search engine-friendly URLs, you don't need to change your database structure.
In profile.php, check for $_GET['id'], then if it exists, use a SELECT query for the same ID in the database. It would look something like this.
<?php
if (isset($_GET['id']))
{
$id = (int) $_GET['id'];
$sql = 'SELECT * FROM Members WHERE ID = ' . $id;
// Then the rest of the code to check the results goes here
}
?>
A user with an ID of 1 would be profile.php?id=1
You are doing right. Now write SQL like this:
$sql = sprintf("SELECT * FROM Members WHERE ID=%d", mysql_real_escape_string($_GET['id']));
And you'll be able to get userdata by $_GET['id']. Remember to use mysql_real_escape_string to protect your queries against SQL injection. sprintf is also a good thing to substitute right data types like numbers or strings.
You don't heed to save profile url.
You have to build it dynamically.
Because most of the url remains the samy, only id is changing.
So, get id from the database and add it to the url.

Categories