-1

I have code that i use to insert data into my database and it works fine im trying to adapt it to now update the mysql row butitdoes not work. This is my header

<script type='text/javascript'>  

function Bet_game(id)

{
var result = confirm ("Do you want to update your bet?");
if (result)
{
    bet_games(id);
}
}

function bet_games(id)

{
if (window.XMLHttpRequest)
    xmlhttp=new XMLHttpRequest();
else
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.onreadystatechange=function()

{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        if (xmlhttp.responseText !="")
        {
            alert (xmlhttp.responseText);
            document.getElementById(id).style.display="none";
            return true;
        }
    }
}

gid = id.replace('tbl_','');

ML_team = document.getElementById('ML_team'+gid).value;
ML_points = document.getElementById('ML_points'+gid).value;
OU_team = document.getElementById('OU_team'+gid).value;
OU_points = document.getElementById('OU_points'+gid).value;
SPREAD_team = document.getElementById('SPREAD_team'+gid).value;
SPREAD_points = document.getElementById('SPREAD_points'+gid).value;

val = "&ML_team="+ML_team+"&ML_points="+ML_points;
val = val + "&OU_team="+OU_team+"&OU_points="+OU_points;
val = val + "&SPREAD_team="+SPREAD_team+"&SPREAD_points="+SPREAD_points;

xmlhttp.open("get","update_bet.php?gid="+ gid + val,true);
xmlhttp.send();
}

</script>

My table

<?php 
                foreach ($conn->query("SELECT * FROM game_data INNER JOIN user_betting ON game_data.id=user_betting.game_id WHERE game_data.sport='MLB Baseball' AND user_betting.handicapper_id=2 AND date >= DATE_ADD(NOW(), INTERVAL 2 HOUR) ORDER BY date ASC") as $MLB) { ?>
                    <form id="<?php echo $MLB['id']; ?>" action="update_bet.php" method="post"><table class="table table-bordered"  id="tbl_<?php echo $MLB['id'];?>">';
                        <thead>
                            <tr>
                                <th width="5%" class="head0">Rotation</th>
                                <th width="50%" class="head1">Team</th>
                                <th width="10%" class="head0">Money Line</th>
                                <th width="10%" class="head0">Over/Under</th>
                            </tr>
                        </thead>
                    <tr>
                        <td colspan="6">
                        <?php $date = date_create($MLB['date']);
                        echo date_format($date, 'l F jS Y \@ g:iA'); ?>
                        </td>
                    </tr>           
                    <tr>
                        <td><?php echo $MLB['awayrotation']; ?></td>
                        <td><?php echo $MLB['awayteam'];?> </td>
                        <td><?php echo $MLB['awaymoneyline'];?></td>
                        <td><?php echo $MLB['total'];?></td>
                    </tr>
                    <tr>
                        <td><?php echo $MLB['homerotation'];?></td>
                        <td><?php echo $MLB['hometeam'];?></td>
                        <td><?php echo $MLB['homemoneyline'];?></td>
                        <td><?php echo $MLB['total'];?></td>
                    </tr>               
                    <tr>
                        <td colspan="5"><div style="width:100%"><form method="post" name="tbl_<?php echo $MLB['id']; ?>" id="tbl_<?php echo $MLB['id']; ?>" action="update_bet.php"><div align="left" style="width:25%;float:left">Place your bet:</div>  <div align="right" style="width:75%;float:right;">
                        Money Line: 
                        <select name="ML_team<?php echo $MLB['id']; ?>">
                            <option value=""></option>
                            <option value="<?php echo $MLB['awayteam'];?>" <?php if($MLB['awayteam'] == $MLB['ML_team']){ echo " selected"; }; ?>><?php echo $MLB['awayteam'];?> </option>
                            <option value="<?php echo $MLB['hometeam'];?>" <?php if($MLB['hometeam'] == $MLB['ML_team']){ echo " selected"; }; ?>><?php echo $MLB['hometeam'];?> </option>
                        </select>
                        <select name="ML_points<?php echo $MLB['id']; ?>">
                            <option value=""></option>
                            <option value="1" <?php if ($MLB['ML_points'] == '1') { echo " selected"; }; ?>>1</option>
                            <option value="2" <?php if ($MLB['ML_points'] == '2') { echo " selected"; }; ?>>2</option>
                            <option value="3" <?php if ($MLB['ML_points'] == '3') { echo " selected"; }; ?>>3</option>
                            <option value="4" <?php if ($MLB['ML_points'] == '4') { echo " selected"; }; ?>>4</option>
                            <option value="5" <?php if ($MLB['ML_points'] == '5') { echo " selected"; }; ?>>5</option>
                        </select>
                        <span style="padding-left:10px">Over/Under: </span>
                        <select name="OU_team<?php echo $MLB['id']; ?>">
                            <option value=""></option>
                            <option value="over" <?php if ($MLB['OU_team'] == 'over') { echo " selected"; }; ?> >Over</option>
                            <option value="under" <?php if ($MLB['OU_team'] == 'under') { echo " selected"; }; ?>>Under</option>
                        </select>
                        <select name="OU_points<?php echo $MLB['id']; ?>">
                            <option value""></option>
                            <option value="1" <?php if ($MLB['OU_points'] == '1') { echo " selected"; }; ?>>1</option>
                            <option value="2" <?php if ($MLB['OU_points'] == '2') { echo " selected"; }; ?>>2</option>
                            <option value="3" <?php if ($MLB['OU_points'] == '3') { echo " selected"; }; ?>>3</option>
                            <option value="4" <?php if ($MLB['OU_points'] == '4') { echo " selected"; }; ?>>4</option>
                            <option value="5" <?php if ($MLB['OU_points'] == '5') { echo " selected"; }; ?>>5</option>
                        </select>
                        <select hidden="true" id="SPREAD_team<?php echo $MLB['id']; ?>">
                            <option value=""></option>
                        </select>

                        <select hidden="true" id="SPREAD_points<?php echo $MLB['id']; ?>">
                            <option value="0">0</option>
                        </select>
                        <input type="hidden" name="game_id" value="<?php echo $MLB['id'];?>">
                        <input type="hidden" name="handicapper_id" value="<?php echo $userid; ?>">
                        <input type="hidden" name="sport" value="<?php echo $MLB['sport'];?>">
                        <input type="hidden" name="action" value="bet">
                        <span style="padding-left:5px;">
                        <input type="button" onClick="Bet_game('tbl_<?php echo $MLB['id'] ?>')" value="Update Bid"></span>
                    </form>
                    <form action="actions.php" method="post">
                    <input type="button" onClick="delete_bet( ' <?php echo $MLB['game_id']; ?> ' , ' <?php echo $MLB['handicapper_id']; ?> ' )" value="Cancel Bid"></input>
                    </form>
                    </div></div>
                        </td>
                    </tr>
            </table>
<?php } ?>

My update_bet.php

<?php 
include 'header.php'; 
//
$gid = $_REQUEST['gid'];
//
$ML_team=$_REQUEST['ML_team'];
$ML_points=$_REQUEST['ML_points'];
$OU_team=$_REQUEST['OU_team'];
$OU_points=$_REQUEST['OU_points'];
$SPREAD_team=$_REQUEST['SPREAD_team'];
$SPREAD_points=$_REQUEST['SPREAD_points'];
//
$user_id= $_SESSION['user']['id'];
//
//$sql = "SELECT * FROM game_data WHERE id='$gid'";
//$games = mysql_query($sql);
//while($game = mysql_fetch_assoc($games))
//{
//  $id = $game['id'];
//  $sport = $game['sport'];
//  $dt=date('Y-m-d H:s:i');    
//}
$sql="UPDATE user_betting SET (ML_team, ML_decision, ML_points, OU_team, OU_decision, OU_points, SPREAD_team, SPREAD_decision, SPREAD_points)
values ('$ML_team','','$ML_points','$OU_team','','$OU_points','$SPREAD_team','','$SPREAD_points') WHERE handicapper_id='".$user_id."' AND game_id='".$gid."'";
//
$games = mysql_query($sql) or die("Record no updated".mysql_error());
echo "Record save successfully";

?>

what am i missing??

DexDeadly
  • 61
  • 6
  • 1
    What does _does not work_ actually mean. What does it do and what does it not do. – RiggsFolly Jun 07 '14 at 19:50
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Jun 07 '14 at 20:09
  • I get no errors and the record does not update. No error log gets written either. I have this written a little different to insert the record but when I changed it to update record it does not work. I get the prompt to confirm but the record does not update. – DexDeadly Jun 07 '14 at 20:35
  • I had figured out the fact I was using getelementsbyid and not having an id assigned to the fields, just names . I did not realize this until after I finally got myself some sleep. Thanks for the help! – DexDeadly Jun 15 '14 at 06:48

1 Answers1

0

Try checking the network response in Google Dev Tools Inspector. If you're getting a 500 the script has a syntax error. If you are seeing your "Record save success" message, try turning on ini_set('display_errors',1); at the top of the script to see if any of the variables were missing, or echo your query back and check in Dev Tools, then try that query directly on mysql. Also note you should escape your variables for security or use mysqli prepared statements.

DWils
  • 390
  • 1
  • 4
  • 16
  • Do you mean by right clicking and doing inspect element? I do not gwt the record saved message. I only get the confirmation message and then nothing happens. Ill have to update the script for inserting to use PDO as well. – DexDeadly Jun 08 '14 at 04:36
  • so using that i found an uncaught typeerror cannot read property type of null then references my function Bet_game() however my onclick event sends the value of tbl_16 when i look at it using inspect element so this confuses me – DexDeadly Jun 08 '14 at 05:30
  • So it sounds to me what you are saying is the onclick cannot call the function you have specified in the header, in that case it may be that the dom is loading before the script is defined somehow. Most times I have better luck attaching a click event to the element after the page is loaded, to avoid any function being called out of order if the browser decides to process javascript late. – DWils Jun 08 '14 at 08:50
  • Something like `elem = document.getElementById('betButton'); document.attachEventListener('click',Bet_game());`. And add id='betButton' to the input element. See MDN: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener – DWils Jun 08 '14 at 08:55
  • would I do this for each element I have? Thats within the function Bet_game(id). – DexDeadly Jun 08 '14 at 16:04
  • Whoops, just noticed in my above example I meant elem.attachEventListener. And yes attach to each element that uses it. To make that task easier I use jQuery, and attach it to anything with a certain class on it. For 1.7+ its $('#parentId').on('click','.class',Bet_game('tbl_16')); See related: http://stackoverflow.com/questions/6658752/jquery-click-event-doesnt-work-on-dynamically-generated-elements – DWils Jun 08 '14 at 19:54