PHP/MYSQL updateform

Hi All,

I am struggling to get the correct information from mysql db to my php page.

My page has an Index page:

Like this: index.php:

[php]<?php
require “dbinfo.php”;

$sql=“SELECT * FROM stats”;

$result = mysql_query($sql, $db) or die (mysql_error());
$pageTitle = “My Stats Database”;
include “header.php”;

print <<<HERE

My Contacts

Select a Record to update add new stat. HERE;

while ($row=mysql_fetch_array($result)){
$id=$row[“id”];
$type=$row[“type”];
$depthead=$row[“depthead”];
$person=$row[“person”];
$descr=$row[“descr”];
$recdate=$row[“recdate”];
$tolog=$row[“tolog”];
$senttorev=$row[“senttorev”];
$recfromrev=$row[“recfromrev”];

print <<<HERE

HERE; } print "
<input type="submit" name="update" value=" Edit " Description: $descr,

Type: $type

Department Head: $depthead

Test Analyst: $person
";

?>

[/php]

Then it has the actual adding of the stats:
Here: addstats.php:

[php]<?php
if($_POST[‘submit’]=“Submit”)
//if(isset($_POST[‘submit’]))

{
$type = cleanData($_POST[‘type’]);

$depthead = cleanData($_POST['depthead']);

$person = cleanData($_POST['person']);

$descr = cleanData($_POST['descr']);

$recdate = cleanData($_POST['recdate']);

$tolog = cleanData($_POST['tolog']);

$senttorev = cleanData($_POST['senttorev']);

$recfromrev = cleanData($_POST['recfromrev']);

//print "Data Cleaned";
addData($type, $depthead, $person, $descr, $recdate, $tolog, $senttorev, $recfromrev);

}
else
{

printForm();
}

function cleanData($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
$data = strip_tags($data);
return $data;
}

function addData ($type, $depthead, $person, $descr, $recdate, $tolog, $senttorev, $recfromrev)

{
//print “ready to add data”;
include(“dbinfo.php”);
include(“header.php”);
$sql=“INSERT INTO stats VALUES (null, ‘$type’, ‘$depthead’, ‘$person’, ‘$descr’, ‘$recdate’,
‘$tolog’, ‘$senttorev’, ‘$recfromrev’)”;
$result=mysql_query($sql) or die(mysql_error());

print <<<HERE

You have added the following

  • Type: $type
  • Deptartment Head: $depthead
  • Test Analyst: $person
  • Description: $descr
  • Received Date: $recdate
  • Date to log: $tolog
  • Sent to Rev: $senttorev
  • Received from Revision : $recfromrev
HERE; }

function printform(){

$pagetitle = “Add Stats”;
include(“header.php”);

print <<<HERE

Add Stats Here

Type*:

Department Head*:

Test Analyst*:

Description*:

Date Received*:

Date to log*:

Sent to Rev:

Received from Rev*:

HERE; }

?>
[/php]

Then it has the updating of the stats: updateform.php this is where I am stuck beyond belief:

[php]<?php
require"dbinfo.php";

$sel_record = $_POST[‘sel_record’];
//$sel_record = (isset($_POST[‘sel_record’])) ? $_POST[‘sel_record’] : ‘’;

$sql = “SELECT * FROM stats WHERE id = ‘$sel_record’”;

//execute sql query and get result
$result = mysql_query($sql, $db) or die (mysql_error());
if (!$result) {
print "

Something went wrong!

";
} else

{ //begin while loop

while ($record = mysql_fetch_array($result, MYSQL_ASSOC)){
$id = $record['id'];
$type = $record['type'];
$depthead = $record['depthead'];
$person = $record["person"];
$descr = $record["descr"];
$recdate = $record["recdate"];
$tolog = $record["tolog"];
$senttorev = $record["senttorev"];
$recfromrev = $record["recfromrev"];

}
}

//end while loop

$pagetitle = “Edit Stat”;
include “header.php”;

print <<<HERE

<h2> Modify this Stat</h2>
<p> Change the values in the boxes and click "Modify Record" button </p>

<form id="myform" method="POST" action="update.php">
<input type="hidden" name="id" value="$id">
Type*:

Department Head*:

Test Analyst*:

Description*:

Date Received*:

Date to log*:

Sent to Rev:

Received from Rev*:

HERE;

?>
[/php]

and the then update portion istself:

[php]<?php

include “dbinfo.php”;

$id = $_POST['id'];
$type = $_POST['type'];
$depthead = $_POST['depthead'];
$person = $_POST['person'];
$descr=$_POST['descr'];
$recdate=$_POST['recdate'];
$tolog=$_POST['tolog'];
$senttorev=$_POST['senttorev'];
$recfromrev=$_POST['recfromrev'];

$sql="UPDATE stats SET
		
		type='$type',
		depthead='$depthead',
		person='$person',
		descr='$descr',
		recdate='$recdate',
		tolog='$tolog',
		senttorev='$senttorev',
		recfromrev='$recfromrev'
	WHERE id='$id' ";

$result=mysql_query($sql) or die (mysql_error());

print "<html><head><title>Update Results</titlel></head><body>";
include "header.php";
print <<<HERE
<h1>The new Record looks like this: </h1>

<td>
<p><strong>Type: </strong>$type</p>	
<p><strong>Department Head: </strong>$depthead</p> 
<p><strong>Test Analyst: </strong> $person</p>
<p><strong>Description: </strong>$descr</p>	 
<p><strong>Received Date:</strong>$recdate</p>
<p><strong>Date to Log:</strong>$tolog</p>
<p><strong>Sent to rev:</strong>$senttorev</p>
<p><strong>Received from Rev:</strong>$recfromrev</p>
<br/>

HERE;
[/php]

My problem is when I select the “edit” record on the index.php page I get the wrong information to my updateform.php screen. I don’t know why, I have tried everything that I know of. Does not matter which record I select I keep on getting the same numbered one.

Please see fi you can help me with this?

Thanking you in advance.

first thing you need to do is verify that the information is in the hidden field, which you can do easily by right clicking and viewing the source, or if you have the dev tools enabled, hit F12.

If its there, then make sure its getting to the update page by viewing the contents of the post array[php]echo “

”;
print_r($_POST);
echo “
”;[/php]If that is showing the right id, then check the query, make sure its not throwing an error. There’s some other suggestions, but it needs to be working first.
Sponsor our Newsletter | Privacy Policy | Terms of Service