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
https://forums.phpfreaks.com/topic/100543-php-mysql-database-update/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.