-1

I want a loop, that echoes some football matches and my users bets on them, and then the possibillity to change the bets. I have two tables in my db:

vm_kampe which is the matches where res is the result:

|id |hhold   |uhold   |res  |
|1  |Rusland |Saudi   |NULL |
|2  |Egypten |Uruguay |NULL |
... and so on

and vm_kampe, which is the users bets:

|id |resu |
|1  |2-1  |
|2  |1-3  |
... and so on.

My update site is like this:

SELECT * FROM vm_kampe k JOIN vm_207 u ON k.id = u.id
...
foreach($results as $row){

echo '<div class="vm_kupon_row">    
<div class="vm_id"><input type="hidden" name="kampids[]" value="'.$row->id.'"></div>    
<div class="vm_kampe">'.$row->hhold.' - '.$row->uhold.'</div>
<div class="vm_result"><input class="vm_resultat" type="text" name="resultats['.$row->id.']" placeholder="X-X" value="'.$row->resu.'"></div>
<input type="submit" class="godkend-vm-kupon" name="submit['.$row->id.']" value="Godkend">

My POST site is like this:

$res = $mysqli->real_escape_string($_POST['resultats']);
$id = $mysqli->real_escape_string($_POST['kampids']);
FOREACH ($_POST as $p) {    
$gid = $p['id'];    
$result = $p['res'];    
$sql = "UPDATE vm_207 SET resu = '$result' WHERE id = '$gid'";    
};

But if I try editting a bet and press submit, the bet will just stay the same as before. All I want is for me to be able to update every match. Please help :)

UPDATE I just tried this in the post code:

$id = $_POST['kampids'];
$res = $_POST['resultats'];
foreach ( $id as $key => $k) {
print "The match is " .$k. " and the result is " .$res[$key];
};

I tried writing 2-1 in the first game and the print was:

The match is 1 and the result is 2-1 The match is...

So as I can see, the function works, so now I just need it to update the table..

  • 2
    In your post code, you never execute the SQL statement. – Sloan Thrasher Apr 09 '18 at 10:38
  • Apart from the common remarks about bind parameters, just setting an `$sql` variable is not enough. You have to actually execute the statement. – GolezTrol Apr 09 '18 at 10:38
  • Also check the loop there. I'm not sure this makes sense: `$result = $p['res'];`. $p is a single post variable (for instance, `resultats`), which may be an array, but it won't be a key-value array. Long story short, try to figure out what *actually* happens, for instance by outputting variables using `var_dump` and see if they match your expectations. I think you will find some obvious issues that you've overlooked now. – GolezTrol Apr 09 '18 at 10:41
  • btw, this is 2 different files - don't know if that means anything. But I thought the `$sql` was executed in the post code, when pressing submit in the edit code? I don't know how to use `var_dump` but I'll try to read about it :) – Daniel Jensen Apr 09 '18 at 10:49

1 Answers1

0

In your hidden field, you are not setting the index number statically like you are with the other inputs.

<div class="vm_id"><input type="hidden" name="kampids['.$row->id.']" value="'.$row->id.'"></div>    

I think this question / answer should help.

How to get form input array into PHP array

Greg Klaus
  • 109
  • 5
  • Oh, i did that now :) Okay, I'll take a look at the Q/A. I'm not so good at this, but when I want an query instead of a print, i'll just write the query in the `foreach{}`? – Daniel Jensen Apr 09 '18 at 11:01
  • Do a `var_dump($_POST)` to see how your POST variables are coming back. That will help you debug this further. – Greg Klaus Apr 09 '18 at 11:06
  • Please see my edit in the question :) Just tried printing it and it gets the right result, so I just need it to update the table now .. – Daniel Jensen Apr 09 '18 at 11:13