duankua3620 2012-06-13 22:45
浏览 51
已采纳

如何通过DIV指定SQL值?

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!
}
?>
  • 写回答

3条回答 默认 最新

  • douao2019 2012-06-14 00:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符