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

    Is there a line number? But, this line isn't correct...from the first batch of code you've posted.

    $middle_initial = $row['middle_initia'];

      Maybe I missed it but I don't see where your GETing the author_id to match in your SQL statement.

        Hi all,

        I will address the responses 1 at time.

        1) The errors are...

        Notice: Undefined index: last_name in C:\wamp\www\DBM_Tests\dbaddedit.php on line 11
        (line 11 is: $last_name = addslashes($_POST['last_name']); )

        Notice: Undefined index: first_name in C:\wamp\www\DBM_Tests\dbaddedit.php on line 12
        (line 12 is: $first_name = addslashes($_POST['first_name']); )

        Notice: Undefined index: middle_initial in C:\wamp\www\DBM_Tests\dbaddedit.php on line 13
        (line 13 is: $middle_initial = addslashes($_POST['middle_initial']); )

        Notice: Undefined index: author_id in C:\wamp\www\DBM_Tests\dbaddedit.php on line 14
        (line 14 is: $author_id = addslashes($_POST['author_id']); )

        Notice: Undefined variable: submit in C:\wamp\www\DBM_Tests\dbaddedit.php on line 17
        (line 17 is: { )

        Notice: Undefined variable: update in C:\wamp\www\DBM_Tests\dbaddedit.php on line 26
        (line 26 is: {)

        2) I fixed the middle_initia in the select page (thanks)

        3) I get the author_id, here

        $author_id = addslashes($_POST['author_id']);

        I am glad I caught the attention of several readers so quickly, I know with help, I can get this working.

        Thanks,
        Don

          For 1) print_r the post array and u'll see that 'last_name', 'first_name', 'middle_initial', 'author_id' is not comming ...

            I have to admit, I am stumped.

            I do not see why the array is empty. The author_id is passed in the URL, so the dbaddedit.php page must be getting it.

            Anyone see what I do not?

            Thanks,
            Don

              That's because they do not yet exist when the page is first loaded. This is why you should check incoming variables with isset() or empty().

                I understand that they 'are not there yet' on the backend (my dbaddedit.php page), but not why they ' are not there yet' on the front end (the db2select.php page)?

                I am using the same technique form another form...

                echo "<TABLE BORDER=2>";
                echo"<TR><TD><B>First Name</B><TD><B>Last Name</B><TD><B>School</B><TD><B>Options</B></TD></TR>";
                while ($myrow = mysql_fetch_array($result))
                {
                echo "<TR><TD>".$myrow["FirstName"]."<TD> ".$myrow["LastName"]."<TD>".$myrow["School"];
                echo "<TD><a href=\"statelookup_view.php?FormID=".$myrow[FormID]."\">View</a> or ";
                echo "<a href=\"statelookup_addedit.php?FormID=".$myrow[FormID]."\">Edit</a> ";
                }
                echo "</TABLE>";
                }
                

                which send the data to another page with the id via the URL and that page works great. And, from what I can tell, this is the exact same technique.

                Thats where my confusion comes from. Not the fact that the variables do exist on the page that processes them, the fact they are not being sent from the page where I picked them to edit.

                Does that make sense?

                I am still at a loss why they are not being passed.

                Am I not giving enough code to spot the error, or are you seeing it, and attempting to lead me to it on my own?

                Thanks,

                Don

                  I had to totally change this to get it to work.

                  Here is what I did...

                  error_reporting(E_ALL);
                  ////Connect to the DB
                  $connectionstring = odbc_connect("Driver={SQL Server};Server=*\*;Database=*","*","*");
                  
                  $author_id = $_GET['author_id'];
                  
                  if (isset($_GET['author_id'])) {
                  
                  $Query = "SELECT * FROM author WHERE author_id=$author_id";
                  $queryexe = odbc_do($connectionstring, $Query);
                  
                  while ($row = odbc_fetch_array($queryexe)) {
                  			 $last_name = $row['last_name'];
                  			 $first_name = $row['first_name'];
                  			 $middle_initial = $row['middle_initial'];
                  			 $author_id = $row['author_id'];
                  }
                  ?>
                  <form method="post" action="db_update.php">
                  <input type="hidden" name="author_id" value="<?PHP echo $author_id; ?>">
                  First Name:<input type="Text" name="last_name" value="<?PHP echo $last_name; ?>"><br>
                  Last Name:<input type="Text" name="first_name" value="<?PHP echo $first_name; ?>"><br>
                  Position:<input type="Text" name="middle_initial" value="<?PHP echo $middle_initial; ?>">
                  <br />
                  <input type="Submit" name="Update" value="Update"></form>
                  <?PHP
                  }
                  else
                  {
                  echo "There is NO author id!!!<br>";
                  ?>
                  <form method="post" action="db_update.php">
                  <input type="hidden" name="author_id" value="<?PHP echo $author_id?>">
                  Last Name:<input type="Text" name="last_name" value="<?PHP echo $last_name;?>"><br>
                  First Name:<input type="Text" name="first_name" value="<?PHP echo $first_name;?>"><br>
                  Middle Initial:<input type="Text" name="middle_initial" value="<?PHP echo $middle_initial;?>"><br>
                  <input type="Submit" name="submit" value="Enter the information"></form>
                  <?PHP } 
                  odbc_close($connectionstring);
                  

                  Thanks,

                  Don

                    Hi Vaaaska,

                    From WikiPedia... (The Tower of Babel)

                    God stopped this project by confusing their languages so that each spoke a different language. As a result, they could no longer communicate with one another and the work was halted.

                    I agree with this passage and would like to "all be speaking the same language" meaning, I would like someone to point out why the variables where not being passed in my first attempt at this.

                    I agree with learning by doing, but I also follow the rule, learn from your mistakes, which is difficult, IF, you do not know what the mistake was.

                    IE, Why did the first attempt fail?

                    Thanks,

                    Don

                      Write a Reply...