Jump to content

Help with update record forms


edoplaza

Recommended Posts

Hello,

 

I am quite new using php and I have mostly been working with Dreamweaver built in features. I've managed to create a simple update form, but to be honest, I don't master it, it's all automatic functions. The form works ok for the display/detail forms, but only for the first record. When I try to update the rest of them, the record update form always shows the first record. What am I doing wrong???

 

You can see an example here:

 

http://w ww.puntabahareque.com/enterupdate/master.php

 

Codes:

 

master.php file:

 

<?php require_once('Connections/conn2.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_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_conn2, $conn2);
$query_Recordset1 = "SELECT * FROM colores";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $conn2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>
<table border="1">
  <tr>
    <td>color</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><a href="detail.php?recordID=<?php echo $row_Recordset1['id']; ?>"><?php echo $row_Recordset1['color']; ?></a><a href="detail.php?recordID=<?php echo $row_Recordset1['id']; ?>">  </a> </td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<br />
<table border="0">
  <tr>
    <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
          <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
          <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
          <?php } // Show if not last page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
Records <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

Detail.php:

 

<?php require_once('../Connections/conn2.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;
}
}

$maxRows_DetailRS1 = 10;
$pageNum_DetailRS1 = 0;
if (isset($_GET['pageNum_DetailRS1'])) {
  $pageNum_DetailRS1 = $_GET['pageNum_DetailRS1'];
}
$startRow_DetailRS1 = $pageNum_DetailRS1 * $maxRows_DetailRS1;

$colname_DetailRS1 = "-1";
if (isset($_GET['recordID'])) {
  $colname_DetailRS1 = $_GET['recordID'];
}
mysql_select_db($database_conn2, $conn2);
$query_DetailRS1 = sprintf("SELECT * FROM colores WHERE id = %s", GetSQLValueString($colname_DetailRS1, "int"));
$query_limit_DetailRS1 = sprintf("%s LIMIT %d, %d", $query_DetailRS1, $startRow_DetailRS1, $maxRows_DetailRS1);
$DetailRS1 = mysql_query($query_limit_DetailRS1, $conn2) or die(mysql_error());
$row_DetailRS1 = mysql_fetch_assoc($DetailRS1);

if (isset($_GET['totalRows_DetailRS1'])) {
  $totalRows_DetailRS1 = $_GET['totalRows_DetailRS1'];
} else {
  $all_DetailRS1 = mysql_query($query_DetailRS1);
  $totalRows_DetailRS1 = mysql_num_rows($all_DetailRS1);
}
$totalPages_DetailRS1 = ceil($totalRows_DetailRS1/$maxRows_DetailRS1)-1;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>
<table border="1">
  <tr>
    <td>color</td>
    <td><?php echo $row_DetailRS1['color']; ?> </td>
  </tr>
  <tr>
    <td>id</td>
    <td><?php echo $row_DetailRS1['id']; ?> </td>
  </tr>
  <tr>
    <td>cosa</td>
    <td><?php echo $row_DetailRS1['cosa']; ?> </td>
  </tr>
  <tr>
    <td>numero</td>
    <td><?php echo $row_DetailRS1['numero']; ?> </td>
  </tr>
</table>

<p><a href="update.php?<?php echo $row_DetailRS1['id']; ?>=<?php echo $row_DetailRS1['id']; ?>">Update</a></p>
</body>
</html><?php
mysql_free_result($DetailRS1);
?>

 

update.php:

 

<?php require_once('../Connections/conn2.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 colores SET color=%s, cosa=%s, numero=%s WHERE id=%s",
                       GetSQLValueString($_POST['color'], "text"),
                       GetSQLValueString($_POST['cosa'], "text"),
                       GetSQLValueString($_POST['numero'], "int"),
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_conn2, $conn2);
  $Result1 = mysql_query($updateSQL, $conn2) or die(mysql_error());

  $updateGoTo = "master.php?" . $row_Recordset1['id'] . "=" . $row_Recordset1['id'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_conn2, $conn2);
$query_Recordset1 = "SELECT * FROM colores";
$Recordset1 = mysql_query($query_Recordset1, $conn2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>
<form action="<?php echo $editFormAction; ?>" method="post" id="form1">
  <table>
    <tr valign="baseline">
      <td align="right">Id:</td>
      
     
      
      <td><?php echo $row_Recordset1['id']; ?></td>
    </tr>
    <tr valign="baseline">
      <td align="right">Color:</td>
      <td><input type="text" name="color" value="<?php echo htmlentities($row_Recordset1['color'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right">Cosa:</td>
      <td><input type="text" name="cosa" value="<?php echo htmlentities($row_Recordset1['cosa'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right">Numero:</td>
      <td><input type="text" name="numero" value="<?php echo htmlentities($row_Recordset1['numero'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right"> </td>
      <td><input type="submit" value="Update record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1" />
  <input type="hidden" name="id" value="<?php echo $row_Recordset1['id']; ?>" />
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

Sorry for the long codes. Someone told me it had to be somewhere here:

 

$query_Recordset1 = "SELECT * FROM colores";

 

I've try this:

 

$value= $_GET['id'];

$query_Recordset1 ="SELECT * FROM colores WHERE id='".$value."'";

 

But still doesn't work. The "id" value is not passing from the previous page (detail.php) to the last page (updat.php)

 

 

Thank you very much

 

Eduardo

 

 

Link to comment
https://forums.phpfreaks.com/topic/174760-help-with-update-record-forms/
Share on other sites

the id not showing on this line look agin...

 

<p><a href="update.php?<?php echo $row_DetailRS1['id']; ?>=<?php echo $row_DetailRS1['id']; ?>">Update</a></p>

 

you sure it not this info in that link?

 

$colname_DetailRS1 = $_GET['recordID'];

it because of this loop i am sure of it......

 

 

  $totalRows_DetailRS1 = mysql_num_rows($all_DetailRS1);

 

try different looping method...

 

why i say that..

 

  <td>id</td>

    <td><?php echo $row_DetailRS1['id']; ?> </td>

  </tr>

  <tr>

    <td>cosa</td>

    <td><?php echo $row_DetailRS1['cosa']; ?> </td>

  </tr>

  <tr>

    <td>numero</td>

    <td><?php echo $row_DetailRS1['numero']; ?> </td>

  </tr>

</table>

 

THE LOOP WORKS ABOVE BUT NOT NOW ONWARDS?(( YOU SHORT HANDED THE LOOP WHY?  }>STOOPED IT WHY?

 

<p><a href="update.php?<?php echo $row_DetailRS1['id']; ?>=<?php echo $row_DetailRS1['id']; ?>">Update</a></p>

</body>

As I said before, I didn't write this code, I used Dreamweaver built in features. So, please, forgive my ignorance... and I don't really get what you are saying...that's way too complicated for me, but I'll try to read slowly so I can get it.

 

Thanks

Eduardo

 

So you say this line is wrong?:

 

<p><a href="update.php?<?php echo $row_DetailRS1['id']; ?>=<?php echo $row_DetailRS1['id']; ?>">Update</a></p>

 

What should I put then?

 

Look, all I need is a very simple  insert / update / delete form for very simple records (something like name, last name, comments). Do you know a good tutorial where I could learn that?. I have books but they all use very complex examples, using multiple tables, etc. I just need a simple table with 3 columns.

 

Thanks

 

Eduardo

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.