Skip to main content
Participant
September 27, 2013
Question

Update form deletes record text but no id. And I don't WANT to delete, I want to update

  • September 27, 2013
  • 1 reply
  • 1039 views

Using dreamweaver cs3, php and mysql, I want to change the name of a text record, but when I do so, it deletes the name and leaves the record ID.

Below is the code from the 2 pages, and an image from phpmyadmin.

Page with the link to update a cookbook name:

<?php require_once('Connections/GE.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $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;

}

}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_RSGetCookbooks = 10;

$pageNum_RSGetCookbooks = 0;

if (isset($_GET['pageNum_RSGetCookbooks'])) {

  $pageNum_RSGetCookbooks = $_GET['pageNum_RSGetCookbooks'];

}

$startRow_RSGetCookbooks = $pageNum_RSGetCookbooks * $maxRows_RSGetCookbooks;

mysql_select_db($database_GE, $GE);

$query_RSGetCookbooks = "SELECT * FROM cookbook ORDER BY cookbook_name DESC";

$query_limit_RSGetCookbooks = sprintf("%s LIMIT %d, %d", $query_RSGetCookbooks, $startRow_RSGetCookbooks, $maxRows_RSGetCookbooks);

$RSGetCookbooks = mysql_query($query_limit_RSGetCookbooks, $GE) or die(mysql_error());

$row_RSGetCookbooks = mysql_fetch_assoc($RSGetCookbooks);

if (isset($_GET['totalRows_RSGetCookbooks'])) {

  $totalRows_RSGetCookbooks = $_GET['totalRows_RSGetCookbooks'];

} else {

  $all_RSGetCookbooks = mysql_query($query_RSGetCookbooks);

  $totalRows_RSGetCookbooks = mysql_num_rows($all_RSGetCookbooks);

}

$totalPages_RSGetCookbooks = ceil($totalRows_RSGetCookbooks/$maxRows_RSGetCookbooks)-1;

$queryString_RSGetCookbooks = "";

if (!empty($_SERVER['QUERY_STRING'])) {

  $params = explode("&", $_SERVER['QUERY_STRING']);

  $newParams = array();

  foreach ($params as $param) {

    if (stristr($param, "pageNum_RSGetCookbooks") == false &&

        stristr($param, "totalRows_RSGetCookbooks") == false) {

      array_push($newParams, $param);

    }

  }

  if (count($newParams) != 0) {

    $queryString_RSGetCookbooks = "&" . htmlentities(implode("&", $newParams));

  }

}

$queryString_RSGetCookbooks = sprintf("&totalRows_RSGetCookbooks=%d%s", $totalRows_RSGetCookbooks, $queryString_RSGetCookbooks);

$queryString_RSGetCookBook = "";

if (!empty($_SERVER['QUERY_STRING'])) {

  $params = explode("&", $_SERVER['QUERY_STRING']);

  $newParams = array();

  foreach ($params as $param) {

    if (stristr($param, "pageNum_RSGetCookBook") == false &&

        stristr($param, "totalRows_RSGetCookBook") == false) {

      array_push($newParams, $param);

    }

  }

  if (count($newParams) != 0) {

    $queryString_RSGetCookBook = "&" . htmlentities(implode("&", $newParams));

  }

}

$queryString_RSGetCookBook = sprintf("&totalRows_RSGetCookBook=%d%s", $totalRows_RSGetCookBook, $queryString_RSGetCookBook);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Display Cookbooks</title>

</head>

<body>

<form id="form1" name="form1" method="post" action="">

  <table width="800" border="1">

    <tr>

      <th scope="col">Cookbooks</th>

      <th scope="col"><a href="AddCookbook.php">Add Cookbook</a></th>

      <th scope="col"> </th>

      <th scope="col"> </th>

    </tr>

    <?php do { ?>

      <tr>

        <td><?php echo $row_RSGetCookbooks['cookbook_name']; ?></td>

        <td>View Chapters</td>

        <td><a href="UpdateCookbook.php?cookbook_id=<?php echo $row_RSGetCookbooks['cookbook_id']; ?>">Update Cookbook Name</a></td>

        <td>Delete Cookbook and all chapters</td>

      </tr>

      <?php } while ($row_RSGetCookbooks = mysql_fetch_assoc($RSGetCookbooks)); ?>

  </table>

  <p> </p>

  <p> 

  <table border="0">

    <tr>

      <td><?php if ($pageNum_RSGetCookbooks > 0) { // Show if not first page ?>

            <a href="<?php printf("%s?pageNum_RSGetCookbooks=%d%s", $currentPage, 0, $queryString_RSGetCookbooks); ?>"><img src="images/First.gif" border="0" /></a>

            <?php } // Show if not first page ?>

      </td>

      <td><?php if ($pageNum_RSGetCookbooks > 0) { // Show if not first page ?>

            <a href="<?php printf("%s?pageNum_RSGetCookbooks=%d%s", $currentPage, max(0, $pageNum_RSGetCookbooks - 1), $queryString_RSGetCookbooks); ?>"><img src="images/Previous.gif" border="0" /></a>

            <?php } // Show if not first page ?>

      </td>

      <td><?php if ($pageNum_RSGetCookbooks < $totalPages_RSGetCookbooks) { // Show if not last page ?>

            <a href="<?php printf("%s?pageNum_RSGetCookbooks=%d%s", $currentPage, min($totalPages_RSGetCookbooks, $pageNum_RSGetCookbooks + 1), $queryString_RSGetCookbooks); ?>"><img src="images/Next.gif" border="0" /></a>

            <?php } // Show if not last page ?>

      </td>

      <td><?php if ($pageNum_RSGetCookbooks < $totalPages_RSGetCookbooks) { // Show if not last page ?>

            <a href="<?php printf("%s?pageNum_RSGetCookbooks=%d%s", $currentPage, $totalPages_RSGetCookbooks, $queryString_RSGetCookbooks); ?>"><img src="images/Last.gif" border="0" /></a>

            <?php } // Show if not last page ?>

      </td>

    </tr>

  </table>

  </p>

</form>

</p>

<p> </p>

<p>  </p>

</body>

</html>

<?php

mysql_free_result($RSGetCookbooks);

?>

Page with the update record:

<?php require_once('Connections/GE.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $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 cookbook SET cookbook_name=%s WHERE cookbook_id=%s",

                       GetSQLValueString($_POST['Update Cookbook Name'], "text"),

                       GetSQLValueString($_POST['cookbook_id'], "int"));

  mysql_select_db($database_GE, $GE);

  $Result1 = mysql_query($updateSQL, $GE) or die(mysql_error());

  $updateGoTo = "index.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";

    $updateGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $updateGoTo));

}

$colname_RSRename_cookbook = "-1";

if (isset($_GET['cookbook_id'])) {

  $colname_RSRename_cookbook = $_GET['cookbook_id'];

}

mysql_select_db($database_GE, $GE);

$query_RSRename_cookbook = sprintf("SELECT cookbook_id, cookbook_name FROM cookbook WHERE cookbook_id = %s", GetSQLValueString($colname_RSRename_cookbook, "int"));

$RSRename_cookbook = mysql_query($query_RSRename_cookbook, $GE) or die(mysql_error());

$row_RSRename_cookbook = mysql_fetch_assoc($RSRename_cookbook);

$totalRows_RSRename_cookbook = mysql_num_rows($RSRename_cookbook);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body>

<p> </p>

<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">

  <label for="CookBook Name"></label>

<p>

    <label for="Update Cookbook Name"><br />

    Update Cookbook Name</label>

    <input name="Update Cookbook Name" type="text" id="Update Cookbook Name" value="<?php echo $row_RSRename_cookbook['cookbook_name']; ?>" />

    <label for="Update Cookbook Name"><br />

    </label>

</p>

  <p>

    <label for="Update"></label>

    <input type="submit" name="Update" id="Update" value="Update!" />

    <input name="cookbook_id" type="hidden" id="cookbook_id" value="<?php echo $row_RSRename_cookbook['cookbook_id']; ?>" />

  </p>

   

   

  <input type="hidden" name="MM_update" value="form1" />

</form>

<p> </p>

</body>

</html>

<?php

mysql_free_result($RSRename_cookbook);

?>

This topic has been closed for replies.

1 reply

Participating Frequently
September 28, 2013

I'd start by removing the spaces from your field name and all references to that name in forms and associated php scripting:

  <input name="Update Cookbook Name"

Participant
September 28, 2013

Turns out I had a cookbook_name where I should have had a cookbook_id from the tables passed as a perameter to the update page.

Thanks!

Participating Frequently
September 29, 2013

OK, good deal.