Form for edit nullifies any fields which aren't altered

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theboyholty
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 5:35 am

Form for edit nullifies any fields which aren't altered

Post by theboyholty »

I've got a form which is for editing purposes, its shown below. The form fills up with the data for editing and any fields the user changes are successfully changed, but any fields which are NOT altered by the user, are tyhen set to NULL (or no data). Is there anything I can do to retain previously stored data?

The form is shown below.

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<table border="1"><tr><td colspan="2">EDIT RESULT DETAILS</td></tr>
<tr><td>MatchID</td><td> <input name="MatchIDedit" value="<?php echo $showdata['MatchID']; ?>" size="5"></td></tr>
<tr><td>MatchTypeID</td><td> <input name="MatchTypeID" value="<?php echo $showdata['MatchTypeID']; ?>" size="5"></td></tr>
<tr><td>SeasonID</td><td> <input name="SeasonID" value="<?php echo $showdata['SeasonID']; ?>" size="5"></td></tr>
<tr><td>MatchNum</td><td> <input name="MatchNum" value="<?php echo $showdata['MatchNum']; ?>" size="5"></td></tr>
<tr><td>TeamID</td><td> <input name="TeamID" value="<?php echo $showdata['TeamID']; ?>" size="5"></td></tr>
<tr><td>Date</td><td> <input name="Date" value="<?php echo $showdata['Date']; ?>" size="15"></td></tr>
<tr><td>Venue</td><td> <input name="Venue" value="<?php echo $showdata['Venue']; ?>" size="6"></td></tr>
<tr><td>HT</td><td> <input name="HTGlsSc" value="<?php echo $showdata['HTGlsSc']; ?>" size="2">
<input name="HTGlsConc" value="<?php echo $showdata['HTGlsConc']; ?>" size="2"></td></tr>
<tr><td>FT</td><td> <input name="FTGlsSc" value="<?php echo $showdata['FTGlsSc']; ?>" size="2">
<input name="FTGlsConc" value="<?php echo $showdata['FTGlsConc']; ?>" size="2"></td></tr>
<tr><td>Result</td><td> <input name="WDL" value="<?php echo $showdata['WDL']; ?>" size="5"></td></tr>
<tr><td>Points</td><td> <input name="Points" value="<?php echo $showdata['Points']; ?>" size="3"></td></tr>
<tr><td>LgePos</td><td> <input name="LgePos" value="<?php echo $showdata['LgePos']; ?>" size="3"></td></tr>
<tr><td>Scorers</td><td> <input name="Scorers" value="<?php echo $showdata['Scorers']; ?>" size="100"></td></tr>
<tr><td>Attendance</td><td><input name="Attendance" value="<?php echo $showdata['Attendance']; ?>" size="8"></td></tr>

<tr><td colspan="2"><input type="submit" name="submit" value="Submit" /></td></tr>
</table>
</form>


Thanks for your help.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Form for edit nullifies any fields which aren't altered

Post by papa »

Sounds like your edit statement overwrites the previous data. Show us your database query.
theboyholty
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 5:35 am

Re: Form for edit nullifies any fields which aren't altered

Post by theboyholty »

Yeah, that's exactly what happens, but i had thought that putting value="<?php echo $showdata['Date']; ?>" would populate the input box with the previous data (which it does) and would then follow the new edited values through to the input statement.

The input statement is below.

$MatchIDedit = $_POST['MatchIDedit'];
$MatchNumber = $_POST['MatchNumber'];
$MatchTypeID = $_POST['MatchTypeID'];
$Replay = $_POST['Replay'];
$SeasonID = $_POST['Season'];
$Date = $_POST['Date'];
$TeamID = $_POST['Opponents'];
$Venue = $_POST['Venue'];
$HTGlsSc = $_POST['HT-F'];
$HTGlsConc = $_POST['HT-A'];
$FTGlsSc = $_POST['FT-F'];
$FTGlsConc = $_POST['FT-A'];
$Result = $_POST['WDL'];
$Scorers = $_POST['Scorers'];
$Attendance = $_POST['Attendance'];
$Points = $_POST['Pts'];
$Position = $_POST['Pos'];


$edit=mysql_query("UPDATE MatchDetails
SET
MatchTypeID='$MatchTypeID',
MatchNum='$MatchNumber',
Replay='$Replay',
SeasonID='$SeasonID',
Date='$Date',
TeamID='$TeamID',
Venue='$Venue',
HTGlsSc='$HTGlsSc',
HTGlsConc='$HTGlsConc',
FTGlsSc='$FTGlsSc',
FTGlsConc='$FTGlsConc',
WDL='$Result',
Points='$Points',
LgePos='$Position',
Scorers='$Scorers',
Attendance='$Attendance'
WHERE MatchID='$MatchIDedit'");
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Form for edit nullifies any fields which aren't altered

Post by papa »

Can't see what might be wrong. You are sure all _POST vars are passing data?
theboyholty
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 5:35 am

Re: Form for edit nullifies any fields which aren't altered

Post by theboyholty »

Well it looks like they're not.

When I display my form, the input boxes are populated with the existing data as they should be.

Then, when I amend any of those fields, the new data is successfully passed to the _POST variables.

My issue is that the ones that are not changed don't seem to be passing the data to the _POST variables even though the form is coded with the data included in the value="...". Would you normally expect this data to be passed through with the new data?

What about coding the sql UPDATE SET WHERE statement so that it only posts new data into the database? How would I go about that (as an alternative)? - or would that constitute sloppy code?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Form for edit nullifies any fields which aren't altered

Post by papa »

Post all your code and in code tags and I'll give it a try.

Your last suggestion would be fine (better) but is a bit more complicated.
theboyholty
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 5:35 am

Re: Form for edit nullifies any fields which aren't altered

Post by theboyholty »

Code: Select all

<!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>
<link rel="stylesheet" type="text/css" href="http://www.mannyroadend.co.uk/leaguetables/lgestylesheet.css"/>
<title>Manny Road End - An Independent Bury Fans Forum (you can speak your bloody mind on here)</title>
 
</head>
<body>
 
<div id="ltfullpage">
 
<?php
include_once 'db.inc.php';
 
 
if (isset($_REQUEST['MatchIDedit'])) { // update data
// need to include code to identify if edited data is different from existing data and if so
// then to use new data but if not, to use existing data. Lots of if statements
 
    $MatchIDedit = $_POST['MatchIDedit'];
    $MatchNumber = $_POST['MatchNumber'];
    $MatchTypeID = $_POST['MatchTypeID'];
    $Replay = $_POST['Replay'];
    $SeasonID = $_POST['Season'];
    $Date = $_POST['Date'];
    $TeamID = $_POST['Opponents'];
    $Venue = $_POST['Venue'];
    $HTGlsSc = $_POST['HT-F'];
    $HTGlsConc = $_POST['HT-A'];
    $FTGlsSc = $_POST['FT-F'];
    $FTGlsConc = $_POST['FT-A'];
    $Result = $_POST['WDL'];
    $Scorers = $_POST['Scorers'];
    $Attendance = $_POST['Attendance'];
    $Points = $_POST['Pts'];
    $Position = $_POST['Pos'];
 
 
$edit=mysql_query("UPDATE MatchDetails
        SET
        MatchTypeID='$MatchTypeID',
        MatchNum='$MatchNumber',
        Replay='$Replay',
        SeasonID='$SeasonID',
        Date='$Date',
        TeamID='$TeamID',
        Venue='$Venue',
        HTGlsSc='$HTGlsSc',
        HTGlsConc='$HTGlsConc',
        FTGlsSc='$FTGlsSc',
        FTGlsConc='$FTGlsConc',
        WDL='$Result',
        Points='$Points',
        LgePos='$Position',
        Scorers='$Scorers',
        Attendance='$Attendance'
        WHERE MatchID='$MatchIDedit'");
 
if (!$edit) { // check query
    exit('Error performing query: ' . mysql_error());
                    }    // check query 
 
} // update data
 
if (isset($_REQUEST['ID'])) { // 1
// IF INFO SUBMITTED FROM VIEW RESULTS PAGE
$MatchID = $_REQUEST['ID'];
echo 'MatchID: ' . $MatchID . '<br />';
 
$getdata=mysql_query("SELECT * FROM MatchDetails WHERE MatchID = '$MatchID'");
if(!$getdata) {
    exit('Error performing query: ' . mysql_error());
        }
 
while($showdata=mysql_fetch_array($getdata)) { // start while
 
?>
 
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
    <table border="1"><tr><td colspan="2">EDIT RESULT DETAILS</td></tr>
    <tr><td>MatchID</td><td>    <input name="MatchIDedit" value="<?php echo $showdata['MatchID']; ?>" size="5"></td></tr>   
    <tr><td>MatchTypeID</td><td>    <input name="MatchTypeID" value="<?php echo $showdata['MatchTypeID']; ?>" size="5"></td></tr>   
    <tr><td>SeasonID</td><td>   <input name="SeasonID" value="<?php echo $showdata['SeasonID']; ?>" size="5"></td></tr> 
    <tr><td>MatchNum</td><td>   <input name="MatchNum" value="<?php echo $showdata['MatchNum']; ?>" size="5"></td></tr> 
    <tr><td>TeamID</td><td>     <input name="TeamID" value="<?php echo $showdata['TeamID']; ?>" size="5"></td></tr> 
    <tr><td>Date</td><td>       <input name="Date" value="<?php echo $showdata['Date']; ?>" size="15"></td></tr>    
    <tr><td>Venue</td><td>      <input name="Venue" value="<?php echo $showdata['Venue']; ?>" size="6"></td></tr>   
    <tr><td>HT</td><td>     <input name="HTGlsSc" value="<?php echo $showdata['HTGlsSc']; ?>" size="2">
                    <input name="HTGlsConc" value="<?php echo $showdata['HTGlsConc']; ?>" size="2"></td></tr>
    <tr><td>FT</td><td>     <input name="FTGlsSc" value="<?php echo $showdata['FTGlsSc']; ?>" size="2"> 
                    <input name="FTGlsConc" value="<?php echo $showdata['FTGlsConc']; ?>" size="2"></td></tr>
    <tr><td>Result</td><td>     <input name="WDL" value="<?php echo $showdata['WDL']; ?>" size="5"></td></tr>   
    <tr><td>Points</td><td>     <input name="Points" value="<?php echo $showdata['Points']; ?>" size="3"></td></tr> 
    <tr><td>LgePos</td><td>     <input name="LgePos" value="<?php echo $showdata['LgePos']; ?>" size="3"></td></tr> 
    <tr><td>Scorers</td><td>    <input name="Scorers" value="<?php echo $showdata['Scorers']; ?>" size="100"></td></tr> 
    <tr><td>Attendance</td><td><input name="Attendance" value="<?php echo $showdata['Attendance']; ?>" size="8"></td></tr>  
 
<tr><td colspan="2"><input type="submit" name="submit" value="Submit" /></td></tr>
</table>
</form>
 
<?php
        } // end while
 
} else if (!isset($_REQUEST['ID'])) {// 1 
echo '<p>Select a result to edit</p>';
 
} // 1
?>
<a href="resultsadmin.php">Results Admin</a><br />
</div>
</body>
</html>
Thank you for all your help.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Form for edit nullifies any fields which aren't altered

Post by aceconcepts »

Personally i wouldn't have a field named "Date" as this may well cause a conflict with MySQL reserved names. Call strDate or something.
Post Reply