UpdateRecord

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

UpdateRecord

Post by moiseszaragoza »

Hi every one. I have been trying to update a record that's NOT coming from a form.

What I'm trying to do is that every time the page loads to get a field in my DB named ‘Counter’ to add one to itself.

I have some code but I know its wrong because it return NULL

Code: Select all

function update(){
$updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($row_ShowContent['Counter'], "int"),
                       GetSQLValueString($row_ShowContent['contentID'], "int"));

     mysql_select_db($database_DinamicPortfolio, $DinamicPortfolio);
    $Result1= mysql_query($updateSQL, $DinamicPortfolio) or die(mysql_error());

echo ("$updateSQL"); 
# UPDATE CONTENT SET Counter=NULL WHERE contentID=NULL
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You can only use variables within functions that are either passed in, set as globals or defined within the function itself.

Therefore $DinamicPortfolio and $database_DinamicPortfolio do not exist within this function. Two solutions

Solution 1: Pass in parameters (preferred method)

Code: Select all

function update($db,$connect)
{
  $updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($row_ShowContent['Counter'], "int"),
                       GetSQLValueString($row_ShowContent['contentID'], "int"));
 
  mysql_select_db($db, $connect);
  $result1= mysql_query($updateSQL, $connect) or die(mysql_error());
  echo ("$updateSQL");
}
Obviously you need to adjust the where you call it as well.

Solution 2: Set variables global for function

Code: Select all

function update()
{
  global $database_DinamicPortfolio;
  global $DinamicPortfolio;

  $updateSQL = sprintf("UPDATE CONTENT SET Counter=%s WHERE contentID=%s",
                       GetSQLValueString($row_ShowContent['Counter'], "int"),
                       GetSQLValueString($row_ShowContent['contentID'], "int"));
 
  mysql_select_db($database_DinamicPortfolio, $DinamicPortfolio);
  $result1= mysql_query($updateSQL, $DinamicPortfolio) or die(mysql_error());
  echo ("$updateSQL");
}
I think that should do it.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Oh you also need to do the same for $row_ShowContent...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: UpdateRecord

Post by timvw »

you don't need to calculate the new countervalue yourself (leave that to mysql)

Code: Select all

UPDATE CONTENT SET counter=counter+1 WHERE contentID=$contentid
Post Reply