Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can populate a select list from a database but can't update it to a field

    Hi,

    I have a select list that is populated from a table and shows all the names of pages in the select list. Trouble is when I press update it doesn't update the selected page to a field. Can anyone advise please?

    Code:
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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;
    }
    }
    
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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;
    }
    }
    
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
      $updateSQL = sprintf("UPDATE menu_item JOIN dow ON dow.id_content = menu_item.id SET title=%s, parent_id=%s, admin_hide=%s, delete_hide=%s, nav_hide=%s, content=%s, background_color=%s, `position`=%s WHERE id=%s",
                           GetSQLValueString($_POST['title'], "text"),
                           GetSQLValueString($_POST['parent_id'], "int"),
    					   GetSQLValueString($_POST['admin_hide'], "text"),
    					   GetSQLValueString($_POST['delete_hide'], "text"),
    					   GetSQLValueString($_POST['nav_hide'], "text"),
    					   GetSQLValueString($_POST['content'], "text"),
    					   GetSQLValueString($_POST['background_color'], "text"),
                           GetSQLValueString($_POST['position'], "int"),
                           GetSQLValueString($_POST['id'], "int"));
    
      mysql_select_db($database_design_online_connection, $design_online_connection);
      $Result1 = mysql_query($updateSQL, $design_online_connection) or die(mysql_error());
    
      $updateGoTo = "update_page.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $updateGoTo));
    }
    
    $colname_DetailRS1 = "-1";
    if (isset($_GET['ID'])) {
      $colname_DetailRS1 = $_GET['ID'];
    }
    mysql_select_db($database_design_online_connection, $design_online_connection);
    $query_DetailRS1 = sprintf("SELECT * FROM menu_item JOIN dow ON dow.id_content = menu_item.id WHERE id = %s", GetSQLValueString($colname_DetailRS1, "int"));
    $DetailRS1 = mysql_query($query_DetailRS1, $design_online_connection) or die(mysql_error());
    $row_DetailRS1 = mysql_fetch_assoc($DetailRS1);
    $totalRows_DetailRS1 = mysql_num_rows($DetailRS1);
    
    mysql_select_db($database_design_online_connection, $design_online_connection);
    $query_page_position = "SELECT title, parent_id, `position` FROM menu_item";
    $page_position = mysql_query($query_page_position, $design_online_connection) or die(mysql_error());
    $row_page_position = mysql_fetch_assoc($page_position);
    $totalRows_page_position = mysql_num_rows($page_position);
    ?>
    Code:
    <select name="parent_id" style="height:30px; width:60px; margin-top:-5px">
              <?php
    do {  
    ?>
              <option value="<?php echo $row_DetailRS1['parent_id']?>"><?php echo $row_page_position['title']?></option>
              <?php
    } while ($row_page_position = mysql_fetch_assoc($page_position));
      $rows = mysql_num_rows($page_position);
      if($rows > 0) {
          mysql_data_seek($page_position, 0);
    	  $row_page_position = mysql_fetch_assoc($page_position);
      }
    ?>
            </select>
    Obviously I have only included the select code there is alot more on the page

    Many Thanks
    Joe

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Is this a copy and paste error? You have two identical functions here; good thing they're wrapped in a function_exists check or it would throw fatal. Remove the one since its not required.

    Do you receive an error when you issue an update? I'm not 100% sure if you can issue it with a joined table without using identified tablespaces, but I'm sure it would complain if there were ambiguity.
    If you do not have an error from the sql, remove the header call and place a printf("Updated %d records.", mysql_affected_rows($design_online_connection)); to see how many records were updated. If its 0 AND the data is actually different for at least one record to change, than the where clause is incorrect.

    Also, with the work you have here you probably should upgrade to mysqli or PDO libraries. The mysql is deprecated and will be removed in the nearish future. Using either of the above let you prepare your statements, so they will be similar to what you are doing already without all the extra code.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •