Topic: Trouble updating records

Hello,

I have created a simple display / update form

It works ok, 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???

Example:

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']; ?>">&nbsp; </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">&nbsp;</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>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

I know it must be a detail, but I don't see it...


Thanks

Eduardo

Last edited by edoplaza (September 17, 2009 5:23 pm)

Vote up Vote down

Re: Trouble updating records

I don't have the time to look at this in detail at the moment, but I know that it has to do with these lines:

$query_Recordset1 = "SELECT * FROM colores";
$Recordset1 = mysql_query($query_Recordset1, $conn2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

Rather than selecting all of the records (SELECT *) you need to get the value of the query from the URL (using $_GET[]) and get that specific data. Probably something along the lines of "SELECT * FROM colores WHERE id=(URL value here)"

More info: http://www.tizag.com/mysqlTutorial/mysqlwhere.php

One comment... When you click a link to edit a record, this is the url that I get: http://www.puntabahareque.com/enterupda … te.php?1=1 or http://www.puntabahareque.com/enterupda … e.php?2=2. I think it would make a lot more sense to have "id=1" or "id=2" rather than "1=1" or "2=2".

To do that, you'd need to change this line in detail.php:

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

to

<p><a href="update.php?id=<?php echo $row_DetailRS1['id']; ?>">Update</a></p>
Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Trouble updating records

Thanks for your response,

I'll take a look

Regards,

Eduardo

Vote up Vote down

Re: Trouble updating records

I'm still struggling with this code.

I've tried this:


$value= $_GET['id'];
$query_Recordset1 ="SELECT * FROM colores WHERE id='value'";


and all I get is blank results:

Id:     
Color:    
Cosa:    
Numero:    

I guess the id value is not passing correctly from the detail.php to update.php.

It shouldn't be that hard, though....    It's a simple insert/update record form

Eduardo

Vote up Vote down

Re: Trouble updating records

hi

$value= $_GET['id'];
$query_Recordset1 ="SELECT * FROM colores WHERE id='value'";

I think you missed the $ in 'value'.
if this is not an example then this one below is right.

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

Last edited by bishwadeep (September 18, 2009 6:47 am)

Vote up Vote down

Re: Trouble updating records

Thanks bishwadeep,

I copied and pasted your code, but I still  get blank results


Could you point me to a insert/update record tutorial??

I just need this:

a) A simple for where I can insert data
b) A page with all data displayed
c) A form where I can update the data for each record

I already have a) and b)  I just need c)

Thanks

Eduardo

Vote up Vote down

Re: Trouble updating records

I'll see if I can put together a basic tutorial for you.

One question: how much of this are you actually coding, and how much of this is Dreamweaver doing for you? Just curious, since I see a lot of Dreamweaver generated code in your example code.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Trouble updating records

Hello Benjamin,

You are right; you see a lot of Dreamweaver because I am a complete newbie in php. I used to know a lot of Action Script but I haven't had the time to learn php correctly. Now, my ignorance is giving me a hard time and I'm paying for it... and I know it's very amateurish of me to ask for big pieces of code in this forum, so I apologize for that.

When you start using a software like Dreamweaver you think the built-in features are going to do everything for you (spry, wizards, etc.), but then, when you need to code an email form, or other more complicated things, you are lost.

I think next month I'll start learning php in the correct way (I already bought some books) so I won't have to ask for help every time.

Now, I really need to make this work, for I have to build a web site very soon.

Thank you,

Eduardo

N.B. Not an excuse, but I am not a professional programmer, I am a professional musician who loves computers…

Vote up Vote down

Re: Trouble updating records

Nothing wrong with being a beginner and asking for help. I do agree -- WYSIWYG editors like Dreamweaver do a good job with visual elements, but for actual programming you kinda need to know what you are doing.

As for resources for learning PHP whenever you get to that, here are a couple:
-- http://www.killerphp.com, obviously
-- http://phpvideotutorials.com (both the free and paid videos are good quality -- that's partially how I got started)
-- http://blog.themeforest.net/?s=diving+into+php+day (I've heard a lot of good things about this free video screencast series. I plan to do it at some point myself.)
-- http://www.tizag.com/ has good PHP/MySQL tutorials as well

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Trouble updating records

(Anyone with PHP knowledge is welcome to comment on the code. If there are issues I haven't noticed, please let me know. Do realize that it is intended for beginners, so I didn't want to do anything too advanced that might lead to confusion. Yes, I realize I could use OOP, or could separate some of these out into methods, etc. etc.)

OK... Here's some code for you to play with. It's a basic system that allows you to:
-- view existing records
-- edit existing records
-- delete existing records
-- add new records

Basically, just imagine that you are in charge of a sports team, and you want to keep a list of all your player's contact information. The code I've created could be a starting point for that (it only includes fields for their first name/last name, but could obviously could be expanded to use more fields)

This is just a starting point for you, and you'll need to adapt it if you want it to work in your project. I know it may seem a lot to understand at first, but read all the comments in the code -- I try to explain what I am doing step by step. I'm also happy to help if you have questions.

How to create a system that allows a user to add/edit/remove data in a database seems to be a commonly asked topic, so I may adapt this into an actual tutorial at some point in the future.

DATABASE:
-- You'll need to create a database (I named mine 'records' but it can be changed) using PHPMyAdmin
-- Save the included sql file on your desktop as a .txt file
-- Once you've created the database, make sure the database is selected, then click the "import" tab
-- Select the .txt file on your desktop, and import it into your database. PHPMyAdmin will create all of the necessary tables/import some test data for you to play with

SQL file:

--
-- Table structure for table `players`
--

CREATE TABLE `players` (
  `id` tinyint(4) NOT NULL auto_increment,
  `firstname` varchar(32) NOT NULL,
  `lastname` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `players`
--

INSERT INTO `players` VALUES(1, 'Bob', 'Baker');
INSERT INTO `players` VALUES(2, 'Tim', 'Thomas');
INSERT INTO `players` VALUES(3, 'Rachel', 'Roberts');
INSERT INTO `players` VALUES(4, 'Sam', 'Smith');

Save these php files all in the same folder in a place where you can run them using your server (I'm assuming you are using something like WAMP for the server? I'm not sure if Dreamweaver includes something like that by default.)

connect-db.php

<?php
/* 
    CONNECT-DB.PHP
    Allows PHP to connect to your database
*/

    // Database Variables (edit with your own server information)
    $server = 'localhost';
    $user = 'root';
    $pass = 'root';
    $db = 'records';
    
    // Connect to Database
    $connection = mysql_connect($server, $user, $pass) 
        or die ("Could not connect to server ... \n" . mysql_error ());
    mysql_select_db($db) 
        or die ("Could not connect to database ... \n" . mysql_error ());


?>

view.php

<html>
<head>
    <title>View Records</title>
</head>
<body>

<?php
/* 
    VIEW.PHP
    Displays all data from 'players' table
*/

    // connect to the database
    include('connect-db.php');

    // get results from database
    $result = mysql_query("SELECT * FROM players") 
        or die(mysql_error());  
        
    // display data in table
    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $result )) {
        
        // echo out the contents of each row into a table
        echo "<tr>";
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['firstname'] . '</td>';
        echo '<td>' . $row['lastname'] . '</td>';
        echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
        echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
        echo "</tr>"; 
    } 

    // close table>
    echo "</table>";
?>
<p><a href="new.php">Add a new record</a></p>

</body>
</html>    

new.php

<?php
/* 
    NEW.PHP
    Allows user to create a new entry in the database
*/

    // connect to the database
    include('connect-db.php');
    
    // check if the form has been submitted. If it has, process the form and save it to the database
    if (isset($_POST['submit']))
    {    
        // get form data, making sure it is valid
        $firstname = htmlspecialchars($_POST['firstname']);
        $lastname = htmlspecialchars($_POST['lastname']);
        
        // save the data to the database
        mysql_query("INSERT players SET firstname='$firstname', lastname='$lastname'")
            or die(mysql_error()); 
        
        // once saved, redirect back to the view page
        header("Location: view.php");
    }
    else
    // if the form hasn't been submitted, display the form
    {
        ?>
        <html>
        <head>
            <title>New Record</title>
        </head>
        <body>
            <form action="" method="post">
            <div>
                <strong>First Name:</strong> <input type="text" name="firstname" /><br/>
                <strong>Last Name:</strong> <input type="text" name="lastname" /><br/><br/>
                
                <input type="submit" name="submit" value="Submit">
            </div>
            </form>    
        </body>
        </html>
        <?php
    }
?>    

edit.php

<?php
/* 
    EDIT.PHP
    Allows user to edit specific entry in database
*/

    // connect to the database
    include('connect-db.php');
    
    // check if the form has been submitted. If it has, process the form and save it to the database
    if (isset($_POST['submit']))
    {    
        // confirm that the 'id' value is a valid integer before getting the form data
        if (is_numeric($_POST['id']))
        {
            // get form data, making sure it is valid
            $id = $_POST['id'];
            $firstname = htmlspecialchars($_POST['firstname']);
            $lastname = htmlspecialchars($_POST['lastname']);
            
            // save the data to the database
            mysql_query("UPDATE players SET firstname='$firstname', lastname='$lastname' WHERE id='$id'")
                or die(mysql_error()); 
            
            // once saved, redirect back to the view page
            header("Location: view.php");
        }
        else
        {
            // if the 'id' isn't valid, display an error
            echo 'Error!';
        }
    }
    else
    // if the form hasn't been submitted, get the data from the db and display the form
    {
        
        // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
        if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
        {
            // query db
            $id = $_GET['id'];
            $result = mysql_query("SELECT * FROM players WHERE id=$id")
                or die(mysql_error()); 
            $row = mysql_fetch_array($result);
            
            // check that the 'id' matches up with a row in the databse
            if($row)
            {
            
                // get data from db
                $firstname = $row['firstname'];
                $lastname = $row['lastname'];
            
                // display the form
                ?>
                <html>
                <head>
                    <title>Edit Record</title>
                </head>
                <body>
                    <form action="" method="post">
                        <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                    <div>
                        <p><strong>ID:</strong> <?php echo $id; ?></p>
                        <strong>First Name:</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>
                        <strong>Last Name:</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/><br/><br/>
                        
                        <input type="submit" name="submit" value="Submit">
                    </div>
                    </form>    
                </body>
                </html>    
                <?php
            }
            else
            // if no match, display result
            {
                echo "No results!";
            }
        }
        else
        // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
        {
            echo 'Error!';
        }
    }
?>

delete.php

<?php
/* 
    DELETE.PHP
    Deletes a specific entry from the 'players' table
*/

    // connect to the database
    include('connect-db.php');
    
    // check if the 'id' variable is set in URL, and check that it is valid
    if (isset($_GET['id']) && is_numeric($_GET['id']))
    {
        // get id value
        $id = $_GET['id'];
        
        // delete the entry
        $result = mysql_query("DELETE FROM players WHERE id=$id")
            or die(mysql_error()); 
            
        // redirect back to the view page
        header("Location: view.php");
    }
    else
    // if id isn't set, or isn't valid, redirect back to view page
    {
        header("Location: view.php");
    }
        
?>
Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Trouble updating records

I've temporarily posted the above system to my website, so you can see it in action:

http://www.falkencreative.com/forum/records/view.php

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Trouble updating records

Benjamin,

Thank you very much, this is exactly what I was looking for. I promise I'll try to LEARN php and won't ask for pieces of code anymore...

Best regards,

Eduardo

Vote up Vote down

Re: Trouble updating records

I already uploaded the files to my server and it works:

http://ww w.puntabahareque.com/otrabd/view.php

Thank you again

Eduardo

Vote up Vote down

Re: Trouble updating records

Just one last question,

What would be the best way to link all this to a css style sheet? I notice that in the view.php tables are not shown as a html format but inside a php code.

Thanks  Eduardo

Vote up Vote down

Re: Trouble updating records

go to the view.php page and then do a view source to see the html code produced by the php file.
CSS can be embedded in the head of the page or in an external file sheet and linked just like an html page.

My signature goes here --> X

Vote up Vote down

Re: Trouble updating records

Glad to hear that it is working for you!

edoplaza wrote:

Just one last question,

What would be the best way to link all this to a css style sheet? I notice that in the view.php tables are not shown as a html format but inside a php code.

Just look for the <head></head> tags within the php files, and add a link to the CSS file there.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down