Jump to content

PHP MySQL database update


MWG_Thomas

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.