如何通过DIV指定SQL值?

时间:2012-06-13 22:45:33

标签: php sql

我仍然在艰难而无尽的追求中奋力前进;创建足球博彩系统。这不仅是为了实际创造一些东西,而且还是为了提高我的技能。

在这个项目中,我使用(如果有任何用途):

  • WordPress的
  • 的Navicat
  • SQL
  • PHP

我有一个名为“Bets”的数据库表。它包含以下列;

  • ID (仅为每个匹配分配一个不断增长的数字)
  • gameid (当前比赛的ID)
  • userid (用户ID)
  • homebet (用户homebet)
  • awaybet (用户偏离)

我创建了一个基于PHP的表单,用户可以输入两个数字 - homebet awaybet - 对应于匹配中的目标。通过那里,每次用户按下提交按钮时都会调用 insert.php 文件,抢夺用户ID - 用户ID - 当然还会分配一个连续的号码到新创建的赌注。

我的问题是;我不知道如何分配网站上发布的每个新匹配 - 这是通过 SQL选择 - 陈述通过 gameid 调用信息来完成的。

PHP表格

<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>

最终问题

如何将 gameid 分配给div<form>(或最适合的情况),以便将其注册到数据库表“Bets “?

我不知道,如果需要,你需要弄清楚哪个代码片段。

编辑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)
?>

编辑2 - 修改后的Insert.php - 文件!

<?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!
}
?>

3 个答案:

答案 0 :(得分:2)

您可以使用隐藏的<input>元素:

<input type="hidden" name="gameid" value="5280" />

编辑:这样它会显示在您的$_POST或您正在使用的任何内容中,就像homebetawaybet一样。

EDIT2:澄清评论中提出的问题:

<?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)
}


?>

答案 1 :(得分:1)

您可以将gameid应用于网址,这样就可以了:

http://yourdomain.com/betpage/?gameid=123

然后,您可以使用gameid方法抓取$_GET

$gameid = $_GET['gameid'];

但请记住清理变量。

答案 2 :(得分:1)

基于来自zebediah49的回答的评论,如果你要在同一页面上有人填写主场/远程投注多场比赛,我建议你使用数组命名你的表单元素。像这样的东西:

<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>

注意,我将表单元素命名为bets[$gameid][homebet]bets[$gameid][awaybet]。这要求游戏玩法由php回显(因此如果$gameid=123该字段将是bets[123][homebet]bets[123][awaybet])。提交表单后,可以使用简单的foreach循环来处理它,如下所示:

<?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);
    }
}
?>

请注意,您仍需要对字段进行一些验证以确保数字并防止sql注入。