1
I have a system where you have registered matches and the logged in user tries to match the results of these matches. Each match has a id
registration that is with the name (game) in the database.
My question is: how do I get the user to only bet once in this game? Example: the user bets on match 1 there soon after he can no longer bet on it again. Each bet has a button bet. I wanted to prevent this button from being clicked if the user has already bet or a script appears that says the user has already bet on this match. Follow the betting code:
bet php.:
<?php
session_start();
include_once("../conn/conexao.php");
if(!empty($_SESSION['email'])){
}else{
echo"<script language='javascript' type='text/javascript'>alert('Aréa Restrita.');
window.location.href='/bolao/index.php';</script>";
}
$nome=$_SESSION['nome'];
$email=$_SESSION['email'];
$saldo="SELECT * FROM tb_usuario WHERE email= '$email'";
$exe= mysqli_query($conexao, $saldo);
$linha = mysqli_fetch_array($exe);
$btnApostar = filter_input(INPUT_POST, 'btnApostar', FILTER_SANITIZE_STRING);
if($btnApostar){
include_once ("../conn/conexao.php");
$dados = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$aposta = "INSERT INTO tb_aposta(apostacasa, apostafora, valor, data, usuario, jogo)VALUES(
'".$dados['apostacasa']."',
'".$dados['apostafora']."',
'".$dados['valor']."',
'".$dados['data']."',
'".$_SESSION['email']."',
'".$dados['jogo']."'
)";
$r_aposta = mysqli_query($conexao, $aposta) or die (mysqli_error($conexao));
$verificacao = "SELECT * FROM tb_jogos WHERE jogo=".$dados['jogo'];
$exe= mysqli_fetch_array(mysqli_query($conexao, $verificacao));
$email=$_SESSION['email'];
$saldo=$linha['saldo'];
$valor=$_POST['valor'];
// var_dump($exe);
if (($valor) > ($saldo)){
echo
"<script>
alert('Voce nao possui saldo para realizar a aposta.')
window.location = 'apostar.php';
</script>";
}else{
if(($dados['apostacasa'] == $exe['placarcasa']) && ($dados['apostafora'] == $exe['placarfora'])){
$up = mysqli_query($conexao,"UPDATE tb_usuario SET saldo=saldo+'$valor' WHERE email = '$email' ")or die (mysqli_error($conexao));
echo "<script> alert('Voce acertou, parabéns')
window.location.href='apostar.php';
</script>";
;
}else{ //se estiver errado irá diminuir o valor apostado
$up = mysqli_query($conexao,"UPDATE tb_usuario SET saldo=saldo-'$valor' WHERE email = '$email' ")or die (mysqli_error($conexao));
echo "<script> alert('Voce errou, tente novamente')
window.location.href='apostar.php';
</script>";
}
}
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'/>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<!-- As 3 meta tags acima *devem* vir em primeiro lugar dentro do `head`; qualquer outro conteúdo deve vir *após* essas tags -->
<title>Lance Web</title>
<!-- Bootstrap -->
<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim e Respond.js para suporte no IE8 de elementos HTML5 e media queries -->
<!-- ALERTA: Respond.js não funciona se você visualizar uma página file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style type="text/css">
/*Aqui deixa a imagem de fundo responsiva*/
body{
background: url(../img/principal.png) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#font{
color:white;
font-family: 'Open Sans', sans-serif;
font-size: 20px;
text-align: center;
}
.img-responsive {
max-width:250px;
max-height:150px;
width: auto;
height: auto;
}
</style>
</head>
<body>
<!-- nav e o menu -->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Aqui e como ira aparece em um telefone -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<!-- Aqui no span, são os 3 pontos ao abrir em um telefone -->
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">Voltar</a>
</div>
<!-- Aqui se edita a parte do saldo -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class=""><a>Saldo R$<?php echo $linha['saldo']; ?><span class="sr-only">(current)</span></a></li>
</ul>
<ul class="nav navbar-nav">
<li class=""><a href="apostar.php">Apostar<span class="sr-only">(current)</span></a></li>
</ul>
<ul class="nav navbar-nav">
<li class=""><a href="historico.php">Histórico<span class="sr-only">(current)</span></a></li>
</ul>
<ul class="nav navbar-nav">
<li class=""><a href="ranking.php">Ranking<span class="sr-only">(current)</span></a></li>
</ul>
<ul class="nav navbar-nav">
<li class=""><a href="loja/loja.php">Loja<span class="sr-only">(current)</span></a></li>
</ul>
<!-- Aqui se edita a parte do sair -->
<ul class="nav navbar-nav navbar-right">
<li><a href="sair.php"><i class="glyphicon glyphicon-off"></i></a></li>
</ul>
</div><!-- fim da div collapse, ela faz com que abra um menu ao aumentar o site -->
</div><!-- /.container-fluid -->
</nav>
<!------------------------------------------------------------------------------------------------------------------------>
<?php
include("../conn/conexao.php");
//ORDER BY serve para organizar os dados de acordo com o que voce quiser
$buscar="SELECT * FROM tb_jogos ORDER BY jogo DESC";
$exe= mysqli_query($conexao, $buscar) or die ("OCORREU UM ERRO AO MOSTRAR OS DADOS");
//começo da tabela
echo "<br><br><div class='container'>
<table class='table table-inverse'>
<thead>
<tr bgcolor='#222222' align='center'>
<th><font color='white'>Codigo Partida</font></th>
<th><font color='white'>Time Casa</font></th>
<th><font color='white'>Placar</font></th>
<th><font color='white'>Time Fora</font></th>
<th><font color='white'>Placar</font></th>
<th><font color='white'>Lance</font></th>
<th><font color='white'></font></th>
</tr>
</thead>
</div>";
while($linha = mysqli_fetch_array($exe)){
echo "<form class='form-group' action='' method='post'>
<input class='form-control' type='hidden' name='data' id='id_01' readonly>
<tbody>
<tr bgcolor='#222222'>
<td><font color='white'><input type='int' class='form-control' name='jogo' maxlength='1' value=".$linha['jogo']." style='text-align: center;' readonly='readonly'></font></td>
<td><font color='white'>".$linha['casa']."</font></td>
<td><input type='int' class='form-control' name='apostacasa' maxlength='1' value='' style='text-align: center;'></td>
<td><font color='white'>".$linha['fora']."</font></td>
<td><input type='int' class='form-control' name='apostafora' maxlength='1' value='' style='text-align: center;'></td>
<td><input type='int' class='form-control' name='valor' maxlength='5' value='' style='text-align: center;'></td>
<td><input class='btn btn-success submit-botao' type='submit' value='Apostar' name='btnApostar'></td>
</tr>
</tbody>
</form>";
}
?>
<script> // script da data atual...
var today = new Date();
var dy = today.getDate();
var mt = today.getMonth()+1;
var yr = today.getFullYear();
document.getElementById('id_01').value= yr+"-"+mt+"-"+dy;
</script>
<script src='http://code.jquery.com/jquery-2.1.3.min.js'></script>
<script src='//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js'></script>
</body>
</html>
would you explain to me the search part in the database? I didn’t quite understand how to replace in my code, what would that be a.game and b.game? on my bench and in the code I only use the game as the match id
– Ferb
Hi @pherb! Dude, I did this: the two tables are correlated by the game id on the "play" field. So I made a "left Join" to relate one table to another by pulling the games available on the table "tb_games". I placed an alias for each table with "a" and "b" respectively, and the "ON (a.game = b.game)" is to pick from the second table the same code as the first. Thus, the "while" will detect the game code that the user has in the table "tb_bet".
– Sam
@pherb [continuing...] This way it is possible to know if the user has already bet on that game id. The substitution is simple: just exchange your query for the suggested query and change the "while" as indicated in the reply.
– Sam
now I understand, thanks even, another doubt, how do I make to hit I play the value of Victory or Defeat to the field Result in the database? I tried to update the tb_bet more when I update it updates all the resulting fields
– Ferb
@pherb query
UPDATE tb_aposta SET resultado = 'vitoria' WHERE jogo = '$id_jogo' AND usuario = '$usuario'
when it hits. When it misses, the same thing, just changing'vitoria'
for'derrota'
... replacing the respective variables ($id_jogo
,$usuario
) for what you use in the code.– Sam