MySQL – INSERT Records with HTML Hyperlinks and PHP($_GET)

Using the GET method you can pull variable values from HTML hyperlinks and process them in a PHP script to Insert records into a MySQL Table.

vote.html

<html>
<body>
Vote for Something<br>
<br>
<a href="http://127.0.0.1/php/phpLink.php?yes=1&no=0" > YES</a>
<a href="http://127.0.0.1/php/phpLink.php?yes=0&no=1" > NO</a>
<a href="http://127.0.0.1/php/phpLink.php?yes=1&no=1" > BOTH</a>

</body>
</html>

phpLink.php

<?php

$vote_yes=$_GET['yes'];
$vote_no=$_GET['no'];
$time=time();

$servername = "localhost";
$username = "bob";
$password = "123456";
$db = "classDB";

$conn = new mysqli($servername, $username, $password, $db);

if ($conn->connect_error){
	die("Connection failed: ". $conn->connect_error);
}

$sql = "insert into votes(yes,no,time)values('$vote_yes','$vote_no','$time')"; 

if ($conn->query($sql) === TRUE) {
	echo "Votes Recorded: Yes = ".$vote_yes." No = ".$vote_no." Time = ".$time;
} else {
	echo "Error: ".$sql."<br>".$conn->error;
}

$conn->close();

?>

votes Table

create table votes(
	vote_id int auto_increment primary key,
	yes int,
	no int,
	time int);  

Be the first to comment

Leave a Reply