I'm still battling my way through a tough and endless quest; creating a Football Betting System. This is not only to actually create something, but also to improve my skills.
In this project I use (if to any use):
I have a database table called "Bets". It contains the following columns;
I've created a PHP-based form, where the user can input two numbers - homebet and awaybet - corresponding to the goalscorings in a match. Through there, an insert.php-file, which gets called everytime the user presses the submit-button, snatches the users id - userid - and of course assigns a continous number to the newly created bet.
My problem is then; I have no idea how I should assign each new match posted on the site - this is done by calling information via a SQL Select-statement - with a gameid.
THE PHP FORM
<form action="/scripts/insert.php" method="post" name="gameid"> <!-- Not sure if name="gameid" serves a purpose or just a test - I'm sorry. -->
<div id="betting-structure">
<div class="main-betting-bg">
<div class="right-betting-team">
<h8 class="centering-teamname1">Arsenal</h8><img src="/images/pl-logo-arsnl.png"> <!-- Homebet -->
</div>
<div class="center-betting-input" align="center">
<input type="text" name="homebet" class="bets" /><span class="versus">VS</span><input type="text" name="awaybet" class="bets" />
</div>
<div class="left-betting-team">
<img src="/images/pl-logo-arsnl.png"> <h8 class="centering-teamname2">Arsenal</h8> <!-- Awaybet -->
</div>
</div>
<div class="extra-betting-bg"> <!-- Just design -->
</div>
</div>
<div id="submit-button-area" align="center">
<input type="submit" value="" class="submit-button"> <!-- Submit-button -->
</div>
</form>
FINAL QUESTION
How would I assign a gameid to a div
or maybe a <form>
(or what fits the situation best), so it gets registert into the database table "Bets"?
I wouldn't know, which code-snippet you would need to figure this out, if any is needed.
EDIT 1:
THE INSERT.PHP-FILE
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb, $current_user;
get_currentuserinfo();
$sql= $wpdb->prepare("INSERT INTO `bets` (`userid`, `gameid`, `homebet`, `awaybet`) VALUES (%d, %d, %d, %d)", $current_user->ID, $_POST['gameid'], $_POST['homebet'], $_POST['awaybet']);
$wpdb->query($sql)
?>
EDIT 2 - The modified Insert.php
-file!
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb, $current_user;
get_currentuserinfo();
//make sure the bets exist in $_POST
if ((isset($_POST['bets'])) && is_array($_POST['bets'])){
//loop through the bets getting the gameid and bet information
foreach($_POST['bets'] as $gameid=>$bet){
//prepare sql
$sql= $wpdb->prepare("INSERT INTO `bets` (`userid`, `gameid`, `homebet`, `awaybet`) VALUES (%d, %d, %d, %d)", $current_user->ID, $gameid, $bet['homebet'], $bet['awaybet']);
//run sql
$wpdb->query($sql)
} // This is line 15!
}
?>
based on the comments from zebediah49's answer, if you are going to have someone fill out home/away bets for multiple matches on the same page, I suggest you then name your form elements using arrays. something like this:
<form action="/scripts/insert.php" method="post" name="gameid"> <!-- Not sure if name="gameid" serves a purpose or just a test - I'm sorry. -->
<div id="betting-structure">
<!--Team1 vs Team2-->
<div class="main-betting-bg">
<div class="right-betting-team">
<h8 class="centering-teamname1">Team1</h8><img src="/images/pl-logo-arsnl.png"> <!-- Homebet -->
</div>
<div class="center-betting-input" align="center">
<input type="text" name="bets[$gameid][homebet]" class="bets" /><span class="versus">VS</span><input type="text" name="bets[$gameid][awaybet]" class="bets" />
</div>
<div class="left-betting-team">
<img src="/images/pl-logo-arsnl.png"> <h8 class="centering-teamname2">Team2</h8> <!-- Awaybet -->
</div>
</div>
<div class="extra-betting-bg"> <!-- Just design -->
</div>
<!--Team3 vs Team4-->
<div class="main-betting-bg">
<div class="right-betting-team">
<h8 class="centering-teamname1">Team3</h8><img src="/images/pl-logo-arsnl.png"> <!-- Homebet -->
</div>
<div class="center-betting-input" align="center">
<input type="text" name="bets[$gameid][homebet]" class="bets" /><span class="versus">VS</span><input type="text" name="bets[$gameid][awaybet]" class="bets" />
</div>
<div class="left-betting-team">
<img src="/images/pl-logo-arsnl.png"> <h8 class="centering-teamname2">Team4</h8> <!-- Awaybet -->
</div>
</div>
<div class="extra-betting-bg"> <!-- Just design -->
</div>
<!--Team5 vs Team6-->
<div class="main-betting-bg">
<div class="right-betting-team">
<h8 class="centering-teamname1">Team5</h8><img src="/images/pl-logo-arsnl.png"> <!-- Homebet -->
</div>
<div class="center-betting-input" align="center">
<input type="text" name="bets[$gameid][homebet]" class="bets" /><span class="versus">VS</span><input type="text" name="bets[$gameid][awaybet]" class="bets" />
</div>
<div class="left-betting-team">
<img src="/images/pl-logo-arsnl.png"> <h8 class="centering-teamname2">Team6</h8> <!-- Awaybet -->
</div>
</div>
<div class="extra-betting-bg"> <!-- Just design -->
</div>
</div>
<div id="submit-button-area" align="center">
<input type="submit" value="" class="submit-button"> <!-- Submit-button -->
</div>
</form>
Notice, I named the form elements bets[$gameid][homebet]
and bets[$gameid][awaybet]
. This requires the gameid be echo'ed by php (so if $gameid=123
the field will be bets[123][homebet]
and bets[123][awaybet]
). Once the form is submitted, it could be handled using a simple foreach loop like so:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb, $current_user;
get_currentuserinfo();
//make sure the bets exist in $_POST
if(isset($_POST['bets'] && is_array($_POST['bets'])){
//loop through the bets getting the gameid and bet information
foreach($_POST['bets'] as $gameid=>$bet){
//prepare sql
$sql= $wpdb->prepare("INSERT INTO `bets` (`userid`, `gameid`, `homebet`, `awaybet`) VALUES (%d, %d, %d, %d)", $current_user->ID, $gameid, $bet['homebet'], $bet['awaybet']);
//run sql
$wpdb->query($sql);
}
}
?>
Please note, you will still need to do some validation on the fields to ensure numbers and prevent sql injection.
You can use a hidden <input>
element:
<input type="hidden" name="gameid" value="5280" />
EDIT: That way it will show up in your $_POST
or whatever you're using, just like homebet
and awaybet
.
EDIT2: to clarify a question posed in the comment:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb, $current_user;
get_currentuserinfo();
for($n=1;$n<=8;$n++) {
$sql= $wpdb->prepare("INSERT INTO `bets` (`userid`, `gameid`, `homebet`, `awaybet`) VALUES (%d, %d, %d, %d)", $current_user->ID, $_POST["gameid$n"], $_POST["homebet$n"], $_POST["awaybet$n"]);
$wpdb->query($sql)
}
?>
You could apply the gameid
to the URL so it would be something like this:
http://yourdomain.com/betpage/?gameid=123
You would then grab the gameid
using the $_GET
method.
$gameid = $_GET['gameid'];
But remember to sanitize the variable.