MWG_Thomas Posted April 10, 2008 Share Posted April 10, 2008 Ok guys here is the code I have just written and am getting some really weird outputs from it: $team1 and $team2 will always be integers. The problem is that when I submit the form, I get the output: 4 5 Connected to MySQL Connected to Lacrosse 10 4 UPDATE teamstat SET GF=GF+10, GA=GA+10 WHERE id= 4 successfully inserted recordUPDATE teamstat SET GF=GF+10, GA=GA+10 WHERE id= 5 successfully inserted record The problem is that sometimes it doesnt actually update the database properly. Regularly only 2 of the database fields are updated, sometimes 3 are, and sometimes all 4 are updated properly. It seems to be completely random whether or not it updates properly, and im completely stumped. Using just 2 teams, the table should always be symetric, however quite often its not!? Any help is really appreciated! <?php //get team name variables from last page $team1 = $_GET['team1']; $team2 = $_GET['team2']; echo $team1 ."<p>"; echo $team2 ."<p>"; //set up server connection $username = "root"; $password = ""; $hostname = "localhost"; $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); print "Connected to MySQL<br>"; $selected = mysql_select_db("lacrosse",$dbh) or die("Could not select lacrosse"); print "Connected to Lacrosse<br>"; //get team score variables out $team1score = $_POST['team1score']; $team2score = $_POST['team2score']; echo "<p>"; echo $team1score; echo "<p>"; echo $team1; echo "<p>"; //Update team 1 details in teamstat table //goals for/against $sql = "UPDATE teamstat SET GF=GF+$team1score, GA=GA+$team2score WHERE id= $team1" ; echo $sql; echo "<p>"; if (mysql_query($sql)) { print "successfully inserted record"; } else { print "Failed to insert record"; } //Update team 2 details in teamstat table //goals for/against $sql = "UPDATE teamstat SET GF=GF+$team2score, GA=GA+$team1score WHERE id= $team2" ; echo $sql; echo "<p>"; if (mysql_query($sql)) { print "successfully inserted record"; } else { print "Failed to insert record"; } mysql_close($dbh); ?> Tom Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 11, 2008 Share Posted April 11, 2008 Hi Tom if you are using variable names inside a query they must be inside single quotes such as // all numbers pulled out of my hair and have no bearing on your code $variable = 12; //this wont work - no single quotes $query = "UPDATE tablename SET columnName = columnName = $variable"; // this will work - single quotes included $query = "UPDATE tablename SET columnName = columnName = '$variable'"; hope that helps Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.