I'm trying to code a page that checks a database if a users ip address exists, if not it offers them to signup in a form, if the user does exist then it offers a "play" button.
For some odd reason it's not working, I have an account in the DB but it still won't offer the "play" button. I've also tried signing up with the form I've made, but the page doesn't do anything when I click submit.
<?php
$con = mysqli_connect("localhost","root","","********");
$ip = $_SERVER['REMOTE_ADDR'];
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Checking DB Connection//
$result = mysqli_query($con,"SELECT * FROM users WHERE ip='$ip'");
while($row = mysqli_fetch_array($result)) {
$rsname = $row['rsname'];
$tokens = $row['tokens'];
}
if (mysqli_num_rows($result) > 0) {
if ($tokens >= 1) {
echo '<h2>Hello '.$rsname.'! Click The Button Below To Play!';
echo '<a href="slots.php"><button>Play!</button></a>';
}
else {
echo '<h2>Your Account "'.$rsname.'" Already Exists, But You Do Not Have Any Tokens.</h2><br />';
echo '<h3>You Can Purchase More Token From "Got Your IP" In Game For 20k/Token.</h3>';
}
}
else if (isset($_POST['rsname'])) {
$sql="INSERT INTO users (rsname, ip)
VALUES
('$_POST[rsname]','$_SERVER[REMOTE_ADDR]')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo '<h2>Your account has been setup! Click the button below to play! </h2>';
echo '<a href="slots.php"><button>Play!</button></a>';
}
else {
echo '<h2>You Need To Register To Play RSLOTTO.</h2>';
echo '<form id="form1" name="form1" method="post" action="play.php">';
echo 'RuneScape Username:';
echo '<input name="rsname" type="text" id="rsname" size="32" maxlength="40" />';
echo '<input name="submit" type="button" value="submit" /></form>';
echo 'We only ask for your username and not a password, because we tie your username to your IP address so you don\'t need to enter a password. Safe as can be! :D';
}
mysqli_close($con);
?>