Hi everyone,
I am working on setting up some pages to insert/edit/view on our testing SQL server, and I can get the insert and view working, but I am getting Undefined index errors on my edit page.
Here is the select page (this seems to be working correctly) ...
<?PHP
session_start();
// include our custom function libraries
include_once("../DBM_Tests/db2lib.php");
include_once("../DBM_Tests/db2form.php");
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
//echo(my_header('Simple SELECT statement'));
function display_authors($dbconn) {
// select all rows from the AUTHOR table
$select_stmt = 'SELECT last_name, first_name, middle_initial, author_id
FROM author';
if ($dbconn != 0) {
// odbc_exec returns 0 if the statement fails;
// otherwise it returns a result set ID
$result = odbc_exec($dbconn, $select_stmt);
if ($result == 0) {
echo("SELECT statement failed.");
$sqlerror = odbc_errormsg($dbconn);
echo($sqlerror);
}
else {
print '<table>
<tr><th>Last</th><th>First</th><th>Initial</th><th>ID</th></tr>';
while ($row = odbc_fetch_array($result)) {
$last_name = $row['last_name'];
$first_name = $row['first_name'];
$middle_initial = $row['middle_initia'];
$author_id = $row['author_id'];
print '<tr><td>' . $row['last_name'] . '</td>';
print '<td>' . $row['first_name'] . '</td>';
print '<td>' . $row['middle_initial'] . '</td>';
print '<td>' . $row['author_id'] . '</td>';
print "<td><a href=\"dbaddedit.php?author_id=".$row['author_id']."\">Edit</a></td>";
//print '<td><a href="db_test_output.php">Edit</a></td>';
print '</tr>';
}
print '</table>';
}
}
}
$verbose = TRUE;
$dbconn = dbconnect($verbose);
display_authors($dbconn);
echo('</body></html>');
// always close your database connection
odbc_close($dbconn);
?>
And here is the edit page where I get the errors....
<?PHP session_start(); ?>
<HTML>
<head>
<title>Edit and Update the records</title><br>
</head>
<body>
<?PHP
error_reporting(E_ALL);
include_once("db2lib.php");
$last_name = addslashes($_POST['last_name']);
$first_name = addslashes($_POST['first_name']);
$middle_initial = addslashes($_POST['middle_initial']);
$author_id = addslashes($_POST['author_id']);
if($submit)
{
echo $submit;
$sql = "INSERT INTO Dons_Test_db (last_name, first_name, middle_initial)
VALUES ('$last_name',' $first_name', '$middle_initial')";
$result = odbc_exec($dbconn, $select_stmt);
echo "Thank you! Information entered.\n";
}
else if($update)
{
echo "What is ".$update."<br>";
$sql = "UPDATE Dons_Test_db SET last_name='$last_name',first_name='$first_name', middle_initial='$middle_initial' WHERE author_id=$author_id";
$result = odbc_exec($dbconn, $select_stmt);
// test code
if(odbc_exec($dbconn, $select_stmt)){
echo "Thank you! Information updated.\n";
echo "<br><br><a href=\"db2select.PHP\">Return to the select page</a>";
} else {
$sqlerror = odbc_errormsg($dbconn);
echo($sqlerror);
}
}
else if($author_id)
{
$result = odbc_exec("SELECT * FROM Dons_test_db WHERE author_id=$author_id",$db);
$row = odbc_fetch_array($result);
?>
<form method="post" action="<?PHP echo $PHP_SELF; ?>">
<input type="hidden" name="author_id" value="<?PHP echo $row["author_id"]; ?>">
First Name:<input type="Text" name="last_name" value="<?PHP echo $row["last_name"]; ?>"><br>
Last Name:<input type="Text" name="first_name" value="<?PHP echo $row["first_name"]; ?>"><br>
Position:<input type="Text" name="middle_initial" value="<?PHP echo $row["middle_initial"]; ?>">
<input type="Submit" name="update" value="Update information"></form>
<?PHP
}
else
{
?>
<form method="post" action="<?PHP echo $PHP_SELF; ?>">
Last Name:<input type="Text" name="last_name"><br>
First Name:<input type="Text" name="first_name"><br>
Middle Initial:<input type="Text" name="middle_initial"><br>
<input type="Submit" name="submit" value="Enter information"></form>
<?PHP } ?>
</body>
</HTML>
I started the session before any HTML and I am defining the variables with the $_POST so I am not seeing where I messed up.
Anyone see something I do not?
Thanks,
Don