1

I am new to PHP and want to create a form where the user inserts data into the form (which works) and then that gets stored on MYSQL DB (that works), now the data has to be displayed and then must be able to modify certain records, now I have the part where the records shows and also the "edit" button, but something went wrong somewhere as the same record keeps appearing, so I guess something is wrong with my code :(

Please help:

Here is the index.php code:

<?php
include('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 here is my update updateform.php script:

<?php
include("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="stat" 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 then the actual updating of the mysql has an update.php script:

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

            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;

Can someone please tell me why only one of the records keeps appearing doesn't matter which one I select from my index.php page. For some reason I think it is my $sel_record variable, but I am not sure and have run out of Ideas..

Thank you in advance..

1
  • What have you tried so far to debug this? You say you've run out of ideas but what have you covered to run out of ideas? Commented Apr 7, 2014 at 13:27

1 Answer 1

1

Here's your issue in updateform.php:

$sql = "SELECT * FROM stats WHERE id = 'sel_record'";

That should be:

$sql = "SELECT * FROM stats WHERE id = $sel_record";

You missed out the $ symbol to call a variable, and you don't need quotation marks around an ID.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I changed that but still when I select the "Edit" button on any of the other records, it still just returns the one record to update/modify? Am I missing something else maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.