Write URL parameters into a MYSQL database using PHP

From The IT Community
Jump to: navigation, search




This little page will take two coordinates as URL parameters and write them into a MYSQL database. It will update the table named player with the row named player1.

I use this for an online game where I use these coordinates to move the player.


First here is the script, you can copy it, modify the MySQL variables and save it as a .php file (like write.php):

<?php

$mysqlhost="localhost"; // MySQL-Host

$mysqluser="mysqluser"; // MySQL-User

$mysqlpwd="topsecret"; // Password

$mysqldb="mysqldatabase"; // Name of Database

 

// Create Database Object
$db = @new mysqli($mysqlhost,$mysqluser,$mysqlpwd,$mysqldb);

$create_table =
'CREATE TABLE IF NOT EXISTS player  
(
    name VARCHAR(200) NOT NULL,
    x INT NOT NULL,
    y INT NOT NULL,
    PRIMARY KEY(name)
)';

// Create player table
$create_tbl = $db->query($create_table);
$db->query($create_table);

$x=$_GET['x'];
echo $x . " ";
$y=$_GET['y'];
echo $y;

$sql = 'UPDATE player SET X = ' . $x . ' WHERE name = "player1"';
$db->query($sql);
$sql = 'UPDATE player SET Y = ' . $y . ' WHERE name = "player1"';
$db->query($sql);
			

$db->close();	
?>


To write the coordinates into the database call the file like this: http://yoururl.com/write.php?x=12&y=50

It will also output the coordinates - separated by a blank - for troubleshooting.



Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.






Translate this page:




Articles found in the same category:
(max. 20 shown)