Jump to content

Inserting GET_ variable into database


joebudden

Recommended Posts

Hi guys im creating a game in php and have a page which generates a game id

[code]
<?php
//start the games session
session_start();

//connect to database
include("connect.php");

//generate random game id
$gameid = rand(0,1000000);

//if form is submitted add details to the table
require("start.view.php");
?>
[/code]

[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP Football Manager</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr><td><div class="title"></div></td></tr>
<tr><td>
<br />
<a href='players.php?gameid=<?php echo $gameid;?>'>Start</a>
</td></tr>
</table>
</body>
</html>
[/code]

When the user clicks the start link, the game id generated is sent to the players page.

This is where my problem is..

If i use the code...

[code]
<?php
$gameid = $_GET['gameid'];
echo $gameid . '<br />';
?>
[/code]

It echoes out the game id ok.

But when i try to insert it into the database it says undefined index and variable..???

Here is my code for that page

[code]
<?php
//start the games session
session_start();

//connect to database
include("connect.php");

//query to determine wether player table is empty
$ap_query = "select id,name,surname,ability,fitness,skill,position,prefmove,description,teamtalk
from player
order by surname";
$allplayers = mysql_query($ap_query);

//query to select total of players
$tp_query = "select count(id) as total from player";
$total_players = mysql_query($tp_query);
$a = mysql_fetch_array($total_players, MYSQL_ASSOC);

//var containing total of players
$total = $a['total'];

//vars to hold player details
$id = "id";
$name = "name";
$surname = "surname";
$ability = "ability";
$fitness = "fitness";
$skill = "skill";
$position = "position";
$prefmove = "prefmove";
$description = "description";
$teamtalk = "teamtalk";

//if form is submitted add details to the table
if(isset($_POST["add_players"]) && isset($_POST["gamename"]))
{
//vars to hold insert data
$gameid = $_GET['gameid']; //THIS IS THE GAMEID VARIABLE
$players = $_POST["add_players"];
$gamename = $_POST["gamename"];
$test = count($players);
echo $gamename . '<br />';

if(($test > 5 ) || ($test < 5))
{
echo 'You have not selected 5 players <br />';
}
else
{
foreach ($_POST["add_players"] as $players)
{
//insert query
$addsql = "insert into game (id,gameid,gamename) values ('$players','$gameid','$gamename')";
mysql_query($addsql) or die (mysql_error());
}
//query display players selected
$testquery = "select name,surname from player,game where player.id = game.id and gameid = '$gameid'";
$test2 = mysql_query($testquery);
echo '';
if (mysql_num_rows($test2) !== 0)
{
while($row = mysql_fetch_assoc($test2))
{
echo $row['name'] . ' ';
echo $row['surname'] . '<br />';
}
}
}
}
else
{
echo 'No players chosen';
}

if (isset($_POST['delete']))
{
$deletesql = "delete from game where gameid = '$gameid'";
//$deletesql = "delete from game";
mysql_query($deletesql) or die (mysql_error());
}
require("players.view.php");
?>
[/code]


I know its something simple so if anyone has any ideas please let me know

cheers guys
Link to comment
https://forums.phpfreaks.com/topic/34276-inserting-get_-variable-into-database/
Share on other sites

Try this:
[code]<?php
//start the games session
session_start();

//connect to database
include("connect.php");

//query to determine wether player table is empty
$ap_query = "select id,name,surname,ability,fitness,skill,position,prefmove,description,teamtalk
from player
order by surname";
$allplayers = mysql_query($ap_query);

//query to select total of players
$tp_query = "select count(id) as total from player";
$total_players = mysql_query($tp_query);
$a = mysql_fetch_array($total_players, MYSQL_ASSOC);

//var containing total of players
$total = $a['total'];

//vars to hold player details
$id = "id";
$name = "name";
$surname = "surname";
$ability = "ability";
$fitness = "fitness";
$skill = "skill";
$position = "position";
$prefmove = "prefmove";
$description = "description";
$teamtalk = "teamtalk";

//if form is submitted add details to the table
if(isset($_POST["add_players"]) && isset($_POST["gamename"]))
{
//vars to hold insert data
$gameid = $_GET['gameid']; //THIS IS THE GAMEID VARIABLE
$players = $_POST["add_players"];
$gamename = $_POST["gamename"];
$test = count($players);
echo $gamename . '<br />';

if(($test > 5 ) || ($test < 5))
{
echo 'You have not selected 5 players <br />';
}
else
{
foreach ($_POST["add_players"] as $players)
{
//insert query
$addsql = "insert into `game` (`id`, `gameid`, `gamename`) values ('".$players."','".$gameid."','".$gamename."')";
mysql_query($addsql) or die (mysql_error());
}
//query display players selected
$testquery = "select `name`,`surname` from `player`,`game` where player.id = game.id and gameid = '".$gameid."'";
$test2 = mysql_query($testquery);
echo '';
if (mysql_num_rows($test2) !== 0)
{
while($row = mysql_fetch_assoc($test2))
{
echo $row['name'] . ' ';
echo $row['surname'] . '<br />';
}
}
}
}
else
{
echo 'No players chosen';
}

if (isset($_POST['delete']))
{
$deletesql = "delete from `game` where `gameid` = '".$gameid."'";
//$deletesql = "delete from game";
mysql_query($deletesql) or die (mysql_error());
}
require("players.view.php");
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.