Jump to content

PHP/MYSQL updateform


Lorinda
Go to solution Solved by DavidAM,

Recommended Posts

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
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
<h2> My Contacts</h2>
Select a Record to update <a href="addstat.php"> add new stat</a>.
<table id="home">
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
<tr>
<td>
<form method="POST" action="updateform.php">
<input type="hidden" name="sel_record" value="$id">
<input type="submit" name="update" value="   Edit   " </form>
</td>

<td><strong> Description: </strong>$descr,<p> <strong>Type: </strong>$type</p> <p><strong> Department Head: </strong>$depthead</p> 
<strong> Test Analyst: </strong> $person<br/></td>
HERE;
}
print "</tr></table></body></html>";


?>




Then it has the actual adding of the stats:
Here: addstats.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

<h1>You have added the following</h1>
<ul>
<li> Type: $type</li>
<li> Deptartment Head: $depthead</li>
<li> Test Analyst: $person</li>
<li> Description: $descr</li>
<li> Received Date: $recdate</li>
<li> Date to log: $tolog</li>
<li> Sent to Rev: $senttorev</li>
<li> Received from Revision : $recfromrev</li>
</ul>
HERE;
}

function printform(){

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

print <<<HERE
<h2>Add Stats Here</h2>
<form id = "stats" method ="POST">
<div>
	<label for = "type" >Type*:</label>
	<input type = "text" name = "type" id = "type" required = "required">
</div>
<p>
</p>
<div>
	<label for = "depthead" >Department Head*:</label>
	<input type = "text" name = "depthead" id = "depthead" required = "required">
</div>
<p>
</p>
<div>
	<label for = "person" >Test Analyst*:</label>
	<input type = "text" name = "person" id = "person" required = "required">
</div>
<p>
</p>
<div>
	<label for = "descr" >Description*:</label>
	<input type = "text" name = "descr" id = "descr" required = "required">
</div>
<p>
</p>

<div>
	<label for = "recdate" >Date Received*:</label>
	<input type = "text" name = "recdate" id = "recdate" required = "required">
</div>
<p>
</p>
<div>
	<label for = "tolog" >Date to log*:</label>
	<input type = "text" name = "tolog" id = "tolog" required = "required">
</div>
<p>
</p>
<div>
	<label for = "senttorev" >Sent to Rev:</label>
	<input type = "text" name = "senttorev" id = "senttorev" required = "required">
</div>
<p>
</p>
<div>
	<label for = "recfromrev" >Received from Rev*:</label>
	<input type = "text" name = "recfromrev" id = "recfromrev" required = "required">
</div>
<p>
</p>

<div id="mySubmit">
	<input type="submit" name="submit" value="Submit">
</div>
</form>
HERE;
}






?>

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

<?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 "<h1> Something went wrong!</h1>";
} 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">
<div>
	 <label for="type">Type*:</label>
	 <input type="text" name="type" id="type" value="$type">
</div>
<p>
</p>
<div>
		<label for = "depthead" >Department Head*:</label>
	<input type = "text" name = "depthead" id = "depthead" value = "$depthead">
</div>
<p>
</p>
<div>
	<label for="person">Test Analyst*:</label>
	<input type="text" name="person" id="person" value="$person">
</div>
<p>
</p>
<div>
	<label for="descr">Description*:</label>
	<input type="text" name="descr" id="descr" value="$descr">
</div>
<p>
</p>

<div>
	<label for="recdate">Date Received*:</label>
	<input type="text" name="recdate" id="recdate" value="$recdate">
</div>
<p>
</p>
<div>
	<label for="tolog">Date to log*:</label>
	<input type="text" name="tolog" id="tolog" value="$tolog">
</div>
<p>
</p>
<div>
	<label for="senttorev">Sent to Rev:</label>
	<input type="text" name="senttorev" id="senttorev" value="$senttorev">
</div>
<p>
</p>
<div>
	<label for="recfromrev">Received from Rev*:</label>
	<input type="text" name="recfromrev" id="recfromrev" value="$recfromrev">
</div>
<p>
</p> 
<div id="mySubmit">
<input type="submit" name="submit" value="Modify Record">
</div>
</form>
HERE;

?>

and the then update portion itself:

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

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.

Link to comment
Share on other sites

  • Solution

There is a slight problem in the index.php

 

	    print <<<HERE
	    <tr>
	    <td>
	    <form method="POST" action="updateform.php">
	    <input type="hidden" name="sel_record" value="$id">
	    <input type="submit" name="update" value=" Edit " </form>
	    </td>
	     
	    <td><strong> Description: </strong>$descr,<p> <strong>Type: </strong>$type</p> <p><strong> Department Head: </strong>$depthead</p>
	    <strong> Test Analyst: </strong> $person<br/></td>
HERE;
Look real close at line #6 above. You never closed the INPUT field, so you never closed the FORM, so the whole page is one big form and every submit button will submit all of the IDs.
Link to comment
Share on other sites

Please give some consideration to this:

//begin while loop
while ( $new_array ) {
    $a = $new_array['some_value'];
    $b = $new_array['another_value'];
} //end while loop

Within each iteration of the loop, $a and $b is being assigned values. Yet, nothing is being done with $a and $b. $a and $b is constantly being assigned new values with nothing having been done with the values they had in the prior iteration.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.