Page 1 of 1

UpdateRecord

Posted: Mon Apr 11, 2005 7:11 am
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;
}

Posted: Mon Apr 11, 2005 8:19 am
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.

Posted: Mon Apr 11, 2005 8:22 am
by CoderGoblin
Oh you also need to do the same for $row_ShowContent...

Re: UpdateRecord

Posted: Mon Apr 11, 2005 11:17 am
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