如何通过DIV指定sql值?

时间:2022-11-16 10:52:24

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
  • WordPress
  • Navicat
  • Navicat
  • SQL
  • SQL
  • PHP
  • PHP

I have a database table called "Bets". It contains the following columns;

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

  • ID (just to assign each match a continuously growing number)
  • ID(只分配每个匹配一个不断增长的数字)
  • gameid (the id of the current match)
  • gameid(当前匹配的id)
  • userid (the users id)
  • userid(用户id)
  • homebet (the users homebet)
  • homebet homebet(用户)
  • awaybet (the users awaybet)
  • awaybet 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.

我创建了一个基于php的表单,用户可以输入两个数字——homebet和awaybet——对应于匹配的目标。在那里,插入。php-file,每当用户按下submit-button时,它就会被调用,它会抓取用户的id - userid,当然,也会将一个连续的数字分配给新创建的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.

我的问题是,我不知道应该如何分配站点上的每一项新匹配——这是通过一个SQL选择语句调用信息来完成的——使用一个gameid。

THE PHP FORM

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>

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

我如何将一个gameid分配给一个div或一个

(或者是什么最适合这个情况),所以它将registert放入数据库表“下注”中?

I wouldn't know, which code-snippet you would need to figure this out, if any is needed.

我不知道,如果需要的话,你需要把这个代码段弄清楚。

EDIT 1:

编辑1:

THE INSERT.PHP-FILE

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

编辑2 -修改后的插入。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 个解决方案

#1


1  

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:

根据zebediah49的回答,如果你想让某人在同一个页面上填写多个匹配的home/away赌注,我建议你用数组来命名你的表单元素。是这样的:

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

请注意,我给表单元素命名为“下注”(gameid) [homebet]和押注[gameid][awaybet]。这需要用php来响应gameid(因此,如果$gameid=123,字段将是下注[123][homebet]和bet [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);
    }
}
?>

Please note, you will still need to do some validation on the fields to ensure numbers and prevent sql injection.

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

#2


2  

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.

编辑:这样它就会出现在你的$_POST或者你正在使用的任何东西中,就像homebet和awaybet一样。

EDIT2: to clarify a question posed in the comment:

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


?>

#3


1  

You could apply the gameid to the URL so it would be something like this:

你可以将gameid应用到URL中,所以它是这样的:

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

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

You would then grab the gameid using the $_GET method.

然后使用$_GET方法获取gameid。

$gameid = $_GET['gameid'];

But remember to sanitize the variable.

但是记住要对变量进行消毒。

#1


1  

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:

根据zebediah49的回答,如果你想让某人在同一个页面上填写多个匹配的home/away赌注,我建议你用数组来命名你的表单元素。是这样的:

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

请注意,我给表单元素命名为“下注”(gameid) [homebet]和押注[gameid][awaybet]。这需要用php来响应gameid(因此,如果$gameid=123,字段将是下注[123][homebet]和bet [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);
    }
}
?>

Please note, you will still need to do some validation on the fields to ensure numbers and prevent sql injection.

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

#2


2  

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.

编辑:这样它就会出现在你的$_POST或者你正在使用的任何东西中,就像homebet和awaybet一样。

EDIT2: to clarify a question posed in the comment:

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


?>

#3


1  

You could apply the gameid to the URL so it would be something like this:

你可以将gameid应用到URL中,所以它是这样的:

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

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

You would then grab the gameid using the $_GET method.

然后使用$_GET方法获取gameid。

$gameid = $_GET['gameid'];

But remember to sanitize the variable.

但是记住要对变量进行消毒。