Why does this not update record anyone?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • toedipper

    Why does this not update record anyone?

    Hello,

    PHP4 and MySql

    I have the code below, a mixture of handcoded and Dreamweaver genaratd
    php code. Basically it's an update record form - I load the values from
    a db and bind text boxes etc. This works ok. But I also have an update
    action - if a user changes any of the text box values then he can click
    update and the code should write the new values to the db. Should, but
    it does'nt! When I go and look in the db I can see that the values are
    still the old ones. But it does not trip up either and I have a 'on
    update success' action that redirects to a congrats page and this DOES work.

    thanks in advance,

    rg.

    <?
    session_start() ;
    require_once('C onnections/ppchcust.php');


    //go and get the pre update values for the form
    $colname_rsedit profile = "1";
    if (isset($_SESSIO N['MM_Username'])) {
    $colname_rsedit profile = (get_magic_quot es_gpc()) ?
    $_SESSION['MM_Username'] : addslashes($_SE SSION['MM_Username']);
    }
    mysql_select_db ($database_loca l, $local);
    $query_rseditpr ofile = sprintf("SELECT custid, password, title,
    firstname, surname, company, displayname, `position`, address1,
    address2, city, country, postcode, telephone, fax, emailaddress FROM
    customers WHERE userid = '%s'", $colname_rsedit profile);
    $rseditprofile = mysql_query($qu ery_rseditprofi le, $local) or
    die(mysql_error ());
    $row_rseditprof ile = mysql_fetch_ass oc($rseditprofi le);
    $totalRows_rsed itprofile = mysql_num_rows( $rseditprofile) ;

    //this is the update part
    function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "",
    $theNotDefinedV alue = "")
    {
    $theValue = (!get_magic_quo tes_gpc()) ? addslashes($the Value) :
    $theValue;

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValu e) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theV alue) . "'"
    : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValu e :
    $theNotDefinedV alue;
    break;
    }
    return $theValue;
    }

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_ SERVER['QUERY_STRING']);
    }

    //this the actual update code and sql
    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] ==
    "custregisterfo rm")) {
    $updateSQL = sprintf("UPDATE customers SET password=%s, title=%s,
    firstname=%s, surname=%s, company=%s, displayname=%s, `position`=%s,
    address1=%s, address2=%s, city=%s, country=%s, postcode=%s,
    telephone=%s, fax=%s, emailaddress=%s WHERE custid=%s",
    GetSQLValueStri ng($_POST['password'], "text"),
    GetSQLValueStri ng($_POST['title'], "text"),
    GetSQLValueStri ng($_POST['firstname'], "text"),
    GetSQLValueStri ng($_POST['surname'], "text"),
    GetSQLValueStri ng($_POST['company'], "text"),
    GetSQLValueStri ng($_POST['displayname'], "text"),
    GetSQLValueStri ng($_POST['position'], "text"),
    GetSQLValueStri ng($_POST['address1'], "text"),
    GetSQLValueStri ng($_POST['address2'], "text"),
    GetSQLValueStri ng($_POST['city'], "text"),
    GetSQLValueStri ng($_POST['country'], "text"),
    GetSQLValueStri ng($_POST['postcode'], "text"),
    GetSQLValueStri ng($_POST['telephone'], "text"),
    GetSQLValueStri ng($_POST['fax'], "text"),
    GetSQLValueStri ng($_POST['emailaddress'],"text"),
    GetSQLValueStri ng($_POST['custid'], "int"));

    mysql_select_db ($database_loca l, $local);
    $Result1 = mysql_query($up dateSQL, $local) or die(mysql_error ());

    // if update ok then redir to somewhere else THIS WORKS....
    $updateGoTo = "regeditprofile ok.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($update GoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf( "Location: %s", $updateGoTo));
    }
    ?>
  • Oli Filth

    #2
    Re: Why does this not update record anyone?

    toedipper said the following on 06/09/2005 00:06:[color=blue]
    > Hello,
    >
    > PHP4 and MySql
    >
    > I have the code below, a mixture of handcoded and Dreamweaver genaratd
    > php code. Basically it's an update record form - I load the values from
    > a db and bind text boxes etc. This works ok. But I also have an update
    > action - if a user changes any of the text box values then he can click
    > update and the code should write the new values to the db. Should, but
    > it does'nt! When I go and look in the db I can see that the values are
    > still the old ones. But it does not trip up either and I have a 'on
    > update success' action that redirects to a congrats page and this DOES
    > work.
    >
    > thanks in advance,
    >
    > rg.
    >
    > <?
    > session_start() ;
    > require_once('C onnections/ppchcust.php');
    >
    >[/color]

    *************** *************** *************** ****[color=blue]
    > //go and get the pre update values for the form
    > $colname_rsedit profile = "1";
    > if (isset($_SESSIO N['MM_Username'])) {
    > $colname_rsedit profile = (get_magic_quot es_gpc()) ?
    > $_SESSION['MM_Username'] : addslashes($_SE SSION['MM_Username']);
    > }
    > mysql_select_db ($database_loca l, $local);
    > $query_rseditpr ofile = sprintf("SELECT custid, password, title,
    > firstname, surname, company, displayname, `position`, address1,
    > address2, city, country, postcode, telephone, fax, emailaddress FROM
    > customers WHERE userid = '%s'", $colname_rsedit profile);
    > $rseditprofile = mysql_query($qu ery_rseditprofi le, $local) or
    > die(mysql_error ());
    > $row_rseditprof ile = mysql_fetch_ass oc($rseditprofi le);
    > $totalRows_rsed itprofile = mysql_num_rows( $rseditprofile) ;
    >[/color]
    *************** *************** *************** *****
    ^^^^
    What is the point of this bit??

    [color=blue]
    > //this is the update part
    > function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "",
    > $theNotDefinedV alue = "")
    > {
    > $theValue = (!get_magic_quo tes_gpc()) ? addslashes($the Value) :
    > $theValue;
    >
    > switch ($theType) {
    > case "text":
    > $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    > break;
    > case "long":
    > case "int":
    > $theValue = ($theValue != "") ? intval($theValu e) : "NULL";
    > break;
    > case "double":
    > $theValue = ($theValue != "") ? "'" . doubleval($theV alue) . "'" :
    > "NULL";
    > break;
    > case "date":
    > $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    > break;
    > case "defined":
    > $theValue = ($theValue != "") ? $theDefinedValu e :
    > $theNotDefinedV alue;
    > break;
    > }
    > return $theValue;
    > }
    >
    > $editFormAction = $_SERVER['PHP_SELF'];
    > if (isset($_SERVER['QUERY_STRING'])) {
    > $editFormAction .= "?" . htmlentities($_ SERVER['QUERY_STRING']);
    > }
    >
    > //this the actual update code and sql
    > if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] ==
    > "custregisterfo rm")) {
    > $updateSQL = sprintf("UPDATE customers SET password=%s, title=%s,
    > firstname=%s, surname=%s, company=%s, displayname=%s, `position`=%s,
    > address1=%s, address2=%s, city=%s, country=%s, postcode=%s,
    > telephone=%s, fax=%s, emailaddress=%s WHERE custid=%s",
    > GetSQLValueStri ng($_POST['password'], "text"),
    > GetSQLValueStri ng($_POST['title'], "text"),
    > GetSQLValueStri ng($_POST['firstname'], "text"),
    > GetSQLValueStri ng($_POST['surname'], "text"),
    > GetSQLValueStri ng($_POST['company'], "text"),
    > GetSQLValueStri ng($_POST['displayname'], "text"),
    > GetSQLValueStri ng($_POST['position'], "text"),
    > GetSQLValueStri ng($_POST['address1'], "text"),
    > GetSQLValueStri ng($_POST['address2'], "text"),
    > GetSQLValueStri ng($_POST['city'], "text"),
    > GetSQLValueStri ng($_POST['country'], "text"),
    > GetSQLValueStri ng($_POST['postcode'], "text"),
    > GetSQLValueStri ng($_POST['telephone'], "text"),
    > GetSQLValueStri ng($_POST['fax'], "text"),
    > GetSQLValueStri ng($_POST['emailaddress'],"text"),
    > GetSQLValueStri ng($_POST['custid'], "int"));
    >[/color]

    *******

    I would highly recommend doing an echo of $updateSQL at this point, to
    ensure that the query syntax and POST values are as expected.

    Of course, you will need to disable the redirect below....

    *******
    [color=blue]
    > mysql_select_db ($database_loca l, $local);
    > $Result1 = mysql_query($up dateSQL, $local) or die(mysql_error ());
    >
    > // if update ok then redir to somewhere else THIS WORKS....
    > $updateGoTo = "regeditprofile ok.php";
    > if (isset($_SERVER['QUERY_STRING'])) {
    > $updateGoTo .= (strpos($update GoTo, '?')) ? "&" : "?";
    > $updateGoTo .= $_SERVER['QUERY_STRING'];
    > }
    > header(sprintf( "Location: %s", $updateGoTo));
    > }
    > ?>[/color]


    --
    Oli

    Comment

    Working...