Jump to content

Can´t update MySQL db with correct data


MrVaux

Recommended Posts

Dear PHP Guru

 

I have been strugling with a piece of code I can´t get to work, so I hope someone can help me out!

 

It is a simple league system, and what I wan´t is this:

 

Two teams play each other 5 times, the one team who wins 3 or more gets 2 points.

 

So my problem is this. When I add the score for the two teams say Team 1 wins 3 times and Team 2 wins 2 times, the db with this data updates fine. But I would also like a piece of code that that adds 2 points to the winning team. I have tried to do so below, but it doesn.t seems to work. I get no data in the tabel where the 2 points should go in to?

 

What is wrong here?

 

 

 

<?
include "config.php";
$link = mysql_connect($dbhost, $dbuser, $dbpasswd) or die("Couldn't establish connection");
mysql_select_db($dbname);


if (isset($id))
{
        $query = "UPDATE league_matchresults SET result_1 = '$result_1', result_2 = '$result_2' WHERE id = $id";
        @$result = mysql_query($query, $link);
echo mysql_error();
if($result)
{
		if ($result_1 >= 3)
		{
		$query = "UPDATE league_teamdata SET points = points + 2 WHERE team_1 = $team_name";
		 echo $team_1; // This is only to make sure I have the correct data in $team_1 - Seems to work!
		}		
}

}
?>	

Link to comment
Share on other sites

Try this:

 

<?php
include 'config.php';

if ($link = mysql_connect($dbhost, $dbuser, $dbpasswd)) {
  if (!mysql_select_db($dbname, $link)) {
    trigger_error('Could not select the database: ' . mysql_error(), E_USER_ERROR);
  }
}
else {
  trigger_error('Could not connect to the database: ' . mysql_error(), E_USER_ERROR);
}

if (isset($id)) {
  $query  = "UPDATE league_matchresults SET result_1 = '%s', result_2 = '%s' WHERE id = %d";
  $result = mysql_query(sprintf($query, 
    mysql_real_escape_string($result_1), 
    mysql_real_escape_string($result_2), 
    mysql_real_escape_string($id)
    ), $link
  );
  
  if ($result !== FALSE) {
    if ($result_1 >= 3) {
      $query  = "UPDATE league_teamdata SET points = points + 2 WHERE team_1 = '%s'";
      $result = mysql_query(sprintf($query, mysql_real_escape_string($team_name)), $link);
      
      if ($result !== FALSE) {
        echo $team_1;
      }
      else {
        trigger_error(mysql_error(), E_USER_ERROR);
      }
    }
  }
  else {
    trigger_error(mysql_error(), E_USER_ERROR);
  }
}
?>

Link to comment
Share on other sites

WOW  :D

 

That did the trick.

Had the team_1 and team_name switched around, but thats not your bad.

Figured that out so it works like a charm...

 

But I must admit.... I have no idea what your code is about???

 

Do you know why it is not possible the way i coded it?

Link to comment
Share on other sites

The reason why yours wouldn't work is because the update query you were using for the points addition, you didn't use mysql_query() to actually execute the query. Therefore the query wasn't being executed on the server and the database remained unchanged.

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.