PDA

View Full Version : Submit info to database error


chilllax
04-14-2006, 01:42 AM
<?php
$gamertag=$_POST['gamertag'];
$eaddress=$_POST['eaddress'];

if (!$gamertag || !$eaddress)
{
echo 'You have not entered all of the required information.<br />'
.'Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc())
{
$gamertag = addslashes($gamertag);
$eaddress = addslashes($eaddress);
}

@ $db = new mysqli('localhost', 'chilllax_chill', 'red123', 'chilllax_participants');

if (mysqli_connect_error())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "insert into participants values
('$gamertag', '$eaddress'}";
$result = $db->query($query);
if ($result)
echo $db->affected_rows.' participant added.

$db->close();
?>

Theres the code in my script. I have the database set up and also the correct number of tables. My problem is whenever I try and run this script this error comes up parse error, unexpected $. Why is this. Also I am going by a php book thats infront of me and I dont really understand the bit of code that actually inserts the data into the database. How does it know which table to place the variables into? I hope this made sense. Thanks for any help.

Guest190829
04-14-2006, 06:55 AM
The below should work, but it's 4am here: ;)


$query = "INSERT INTO " . TABLE_PREFIX ." participants (fieldname, fieldname)
VALUES ("' $gamertag . '", "' . $eaddress . '")";

Also, you should use some of vBulletin coding standards. Such as santizing variables with $vbulletin->input->clean_array_gpc() and you don't have to connect to a database if your using this a vbulletin hack. (Since you would be including global.php)

chilllax
04-14-2006, 10:48 AM
Thanks, and Im not using it as a vbulletin hack but I will keep that in mind. One more quesiton though. Where participants is should that be the name of the database? Because thats the name of the database. And the table prefix is that only if my table has a prefix becuase it does not. So I am guessing the prefixless table name goes there. Correct?

Guest190829
04-14-2006, 12:18 PM
Thanks, and Im not using it as a vbulletin hack but I will keep that in mind. One more quesiton though. Where participants is should that be the name of the database? Because thats the name of the database. And the table prefix is that only if my table has a prefix becuase it does not. So I am guessing the prefixless table name goes there. Correct?

participants should be replaced by the table name, not database. Also, I see your using some of vbulletin's coding such as:

$db->query

So you should be able to use any other vbulletin code, if your using that.

chilllax
04-15-2006, 07:45 PM
It still will not work. I changed the original value of $query to what you told me to. I substituted the column names for the fieldnames, is that correct?ffa is the name of my table. Also when I try and run this script it brings up this error
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/chilllax/public_html/tournament/yourentry.php on line 40

If you need me to I can also post the code of the forms.

<html>
<head>
<title>Your Entry</title>
</head>
<table align="center">
<tr>
<td>
<a href="http://www.officalhalo.com">Home</a> |

<a href="/tournament/FFA.htm">FFA</a> |

<a href="/tournament/signup.htm">Sign Up</a> |

<a href="/tournament/results.htm">Results</a>
</td>
</tr>
</table>
<body>
<h3 align="center">Your Entry</h3>
<?php
$gamertag=$_POST['gamertag'];
$eaddress=$_POST['eaddress'];

if (!$gamertag || !$eaddress)
{
echo 'You have not entered all of the required information.<br />'
.'Please go back and try again.';
exit;
}

@ $db = new mysqli('localhost', 'chilllax_chillla', 'red123', 'chilllax_tournament');

if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "INSERT INTO " . TABLE_PREFIX ." ffa (gamertag, eaddress)
VALUES ("' $gamertag . '", "' . $eaddress . '")";

$result = $db->query($query);
if ($result)
echo $db->affected_rows.' Entry submitted into database.';

$db->close();

?>
</body>
</html>