1

I would like to have a page that, when someone clicks a pre-formatted link I've sent, writes a variable in the URL to a MySQL database and just displays "Thank You" or something to the user.

Example:

The user would click a link formatted something like http://www.example.com/click.php?id=12345

When the page loads the 12345 would be written to a table in a MySQL database, it would show a Thank you, and that is it.

Seems like it should be simple enough but I can't find anything on it. I'm probably searching wrong, since this is all new to me.

ColLeslieHapHapablap
  • 1,045
  • 2
  • 9
  • 12
  • 1
    Like `echo 'Thank You, '.$_GET["id"];`? – Logan Wayne Jun 16 '16 at 00:33
  • 1
    Key search phrases would be (after breaking down your request): "*insert value into mysql database*" and "*get value from URL in php*" I guarantee the google machine will return 900M results for both of those queries. Then you can just piece those together and form a perfectly working script! If you run into any issues, be sure to post them here. – mferly Jun 16 '16 at 00:47

3 Answers3

2

Your best bet is to utilise $_GET['id'] which will take in the value from your url.

After grabbing the id from your url you will want to use PDO or mysqli prepared statements in order to protect yourself from sql injection.

I hope this helps.

Updated as per Kevin Voorn's comment.

if(isset($_GET['id']) && !empty($_GET['id'])) {

$logged_id = $_GET['id'];

$stmt = $mysqli->prepare("INSERT INTO tableName (`logged_id`) VALUES (?)");
$stmt->bind_param('i', $logged_id);
$stmt->execute();

if($stmt->affected_rows > 0){

    echo "Thank You.";

}

$stmt->close();

}

Ash Dawson
  • 36
  • 3
  • 1
    Although your answer is correct, you might want to give a code example of PDO / mysqli to show how you can insert data into a database to complete your answer. – Kevin Jun 16 '16 at 00:52
  • `isset($_GET['id']) && !empty($_GET['id'])` is an antipattern that should never exist in any code for any reason. [Why check both isset() and !empty()](https://stackoverflow.com/a/4559976/2943403) – mickmackusa Mar 11 '22 at 02:18
2

User $_GET to retrive the value and put into your table. Example:

code inside click.php

<?php
$id=$_GET['id'];
$sql="Insert into table1 VALUES ($id)";
mysqli_query($connect,$sql);
echo "<script>alert('Thank you')</script>";
?>
Gurtej Singh
  • 225
  • 1
  • 9
0

Thanks for the responses. I ended up finding this page: https://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17 that described the process for using mysqli to connect to my database. I used that page to create the necessary functions in ../db.php and included it in the actual PHP script that would catch the url. My script ended up looking like this:

<?php
require '../db.php';
date_default_timezone_set('UTC');

$date = date("Y-m-d H:i:s T");

$db = new Db();

$db_id = $db -> quote($_GET['id']);
$db_date = $db -> quote($date);

$result = $db -> query("INSERT INTO `table` (`id`,`GUID`,`AccessTime`) VALUES (NULL, " . $db_id . "," . $db_date . ")");

if($result === false) {
    exit();
} else {
    echo "<html><body><center><br><h1>Thank You!</h1></center></body></html>";
}
?>
ColLeslieHapHapablap
  • 1,045
  • 2
  • 9
  • 12