Question

I'm trying to make a football betting system based on the WordPress CMS. I apologize if this is a redundant question, but I've been searching through Google and a few Wiki's all day, and I haven't been able to find any clue to how this question is answered.

BACKGROUND INFORMATION:

I use:

  • MySQL
  • PHP
  • Navicat

Every time the user inputs two integers, corresponding to the goalscorings in a football match, on the php-scripted website, the two integers get sent off to the database table "bets", through an insert.php-file.

The insert.php looks like this:

<?php $con = mysql_connect("host","username","password"); if (!$con)   {   die('Could not connect: ' . mysql_error());   }

mysql_select_db("database", $con);

$sql="INSERT INTO bets (homebet, awaybet) VALUES ('$_POST[homebet]','$_POST[awaybet]')";

if (!mysql_query($sql,$con))   {   die('Error: ' . mysql_error());   } echo "Thanks for betting!";

mysql_close($con); ?>

The php-form looks like this:

I have the following self-made tables: bets, teams, matches, and I have the WordPress table, wp-users, where each member is listed.

The bets table looks like this: each row corresponds to a new bet

As you can see, the userid is empty - which it shouldn't be. It should contain the ID of the user, who entered the bet.

My two-part question is then..

How should I obtain the ID of the user and display it in the database table, bets? Perhaps WordPress have a code-snippet for it.

And finally, how should I attach a gameid to each match? Should I create a new form for each new match?

Thanks SO much in advance - I hope I was able to make this thread readable and easy to understand.

Frederick Andersen

Was it helpful?

Solution

You're missing quite some things here. I can't advise you how to manage the different games since I don't know what the rest of your application looks like.

Here are some pointers to get you going:

Don't create your own DB connection

Remove:

$con = mysql_connect("host","username","password"); if (!$con)   {   die('Could not connect: ' . mysql_error());   }

mysql_select_db("database", $con);

Add instead:

require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

WordPress current user ID

Add this to top off script:

global $wpdb, $current_user;
get_currentuserinfo();

Change $sql code to this:

$sql= $wpdb->prepare("INSERT INTO `bets` (`userid`, `homebet`, `awaybet`) VALUES (%d, %d, %d)", $current_user->ID, $_POST['homebet'], $_POST['awaybet']);

I've also used the wpdb prepare function so your post data will be escaped before getting queried.

Also change this to use WP native querying.

Remove:

if (!mysql_query($sql,$con))   {   die('Error: ' . mysql_error());   } echo "Thanks for betting!";

mysql_close($con);

Add instead:

$wpdb->query($sql)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top