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):
- WordPress
- Navicat
- SQL
- PHP
I have a database table called "Bets". It contains the following columns;
- ID (just to assign each match a continuously growing number)
- gameid (the id of the current match)
- userid (the users id)
- homebet (the users homebet)
- awaybet (the users awaybet)
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!
}
?>