Jump to content

[SOLVED] Help -- PHP and ODBC Database application


wulfgar

Recommended Posts

[quote]Shouldn't there be a very easy solution to what i am trying to achieve here.[/quote]

yes. The fact that it is failing tells me your query isn't working. Also, I notice you are doing this all within a while() loop. If you have only one record you do not need a loop, if you have more then one record your code will produce some quite unexpected results as you will have generated more then one html page within page. ie, A new <html> tag for each record.

We need to see your query.
Ok let me recap for better understanding ::

Firstly i have a login screen :

<a href="http://img169.imageshack.us/img169/8615/loginqj7.jpg">http://img169.imageshack.us/img169/8615/loginqj7.jpg</a> :: Login screen works as desired.

Next i have a search form :

<a href="http://img147.imageshack.us/img147/5674/searchix5.jpg">http://img147.imageshack.us/img147/5674/searchix5.jpg</a> :: Search works as desired, displaying only the first/last name of the student with three buttons for alternate querys.

Here is the results page :

<a href="http://img335.imageshack.us/img335/6839/resultje8.jpg">http://img335.imageshack.us/img335/6839/resultje8.jpg</a> :: This is how the results of the first query are displayed, along with the buttons for further queries.

Now, the search query is basic, and works as desired.

[code]

//form text boxes.
$FName = $_REQUEST['FName'];
$LName = $_REQUEST['LName'];

$query = "SELECT * from stu_db WHERE F_Name LIKE '$FName%' AND L_Name LIKE '$LName%'";

[/code]

Now i display these results as desired, along with three buttons adjacent the result.

[code]

while($row = odbc_fetch_row($rs))
{
  //variables store search results, second parameter is actual database coloumn names
  $NFirst = odbc_result($rs,"F_Name");
  $NLast = odbc_result($rs,"L_Name");

  echo "<html>
        <head>
            <link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\">
        </head>
        <body>
        <table border=\"0\" class=\"tablex\" width=\"400px\" cellpadding=\"0\">
            <b>//Display result</b>
            <tr><td><b>$NLast</b>, $NFirst</td>
            <td align=\"right\" valign=\"top\" width=\"10\">
                 
                  <form method=\"post\" action=\"viewDetail.php\">
                    <input type=\"hidden\" name=\"firstname\" value=\"<?php echo $NFirst ?>\">
                    <input type=\"hidden\" name=\"lastname\" value=\"<?php echo $NLast ?>\">
                    <input type=\"submit\" value=\"View\" class=\"button\">
                  </form>

  <td align=\"right\" valign=\"top\" width=\"10\">
          <form name=\"edit\" method=\"post\" action=\"viewEdit.php\">
                    <input type=\"submit\" value=\"Edit\" class=\"button\">
                  </form></td>

  <td align=\"right\" valign=\"top\" width=\"10\">
          <form name=\"delete\" method=\"post\" action=\"<?$rs2 = odbc_exec($conn,$delQuery);?>\">
                    <input type=\"submit\" value=\"Delete\" class=\"button\">

                  </form></td>
                </td></tr>";
}

[/code]

Next i have a query in order to display <b>all</b> of the information regarding the search query in question, this information i want to display on the view detail page (the script in which i wish the variables $NFirst and $NLast to be passed too.) This is the section in which i am getting grief, i have utilized the code in which you have given me, but have failed to get a result.

[code]

viewQuery = "SELECT * from stu_db WHERE F_Name = '$NFirst' AND L_Name = '$NLast'";

[/code]

Now as you can see within the above query, it will function correctly, although in order for it to function it requires the value of both the $NFirst and $NLast variables.

And finally i have a delete query (which is irrelevant at this time).

[code]

deleteQuery = "DELETE from stu_db WHERE F_Name = '$NFirst' AND L_Name = '$NLast'";

[/code]


So basically i need to send the values of $NFirst and $NLast to the viewDetails.php script, this should occur when i click the view button. Once the values of these two variables are available within the viewDetails.php script i can use them to query the database.

Please feel free to change any of the code in order to make it function.

Thanks i appreciate your time and effort looking over it.

James.
I need to see the [b]code[/b] (not just the query!) that runs your query before your....

[code=php:0]
while($row = odbc_fetch_row($rs))
[/code]

on the page with the forms. I dont thin $NFirst and $NLast are making it into your form.

As for this line....

[code]
<form name=\"delete\" method=\"post\" action=\"<?$rs2 = odbc_exec($conn,$delQuery);?>\">
[/code]

action needs to be a url or a page. Where are you getting these ideas from?
LOL the internet is my source whilst on break from uni; sorry about my incompetence, I'm new to PHP.

Here is the script which runs when a search is initialized :


[code]

<?

echo "<title>Search Results</title>";
echo "<h4><u>Search Results</u></h4><br/>";

//Variable stores textbox string
$FName = $_REQUEST['FName'];
$LName = $_REQUEST['LName'];

$data = 0;

if ($FName != "")
{
  $data++;
}

if ($LName != "")
{
  $data++;
}

//connect to specified database (including username/pass for secure data)
$conn = odbc_connect('student_db', '', '') or die("<br/><b>Database Initialisation failed</b>");

//SQL Query to find matching strings in the database
$query = "SELECT * from stu_db WHERE F_Name LIKE '$FName%' AND L_Name LIKE '$LName%'";

//execute connection to database
$rs = odbc_exec($conn,$query);

/* Our query finds all matches of given data, even if the LName is not provided or vice versa; minimal data
is sufficiant. Rows are fetched according to the query string, once a row is fetched its data can only be
displayed with the use of the odbc_result function. */

while(odbc_fetch_row($rs))
{
  //variables store search results, second parameter is actual database coloumn names
  $NFirst = odbc_result($rs,"F_Name");
  $NLast = odbc_result($rs,"L_Name");
 
//Delete query
$delQuery = "DELETE from stu_db WHERE F_Name = '$NFirst' AND L_Name = '$NLast'";

  echo "<html>
        <head>
            <link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\">
        </head>
        <body>
        <table border=\"0\" class=\"tablex\" width=\"400px\" cellpadding=\"0\">
            <tr><td><b>$NLast</b>, $NFirst</td>
            <td align=\"right\" valign=\"top\" width=\"10\">
                  <form method=\"post\" action=\"viewDetail.php\">
                    <input type=\"hidden\" name=\"firstname\" value=\"<?php echo $NFirst ?>\">
                    <input type=\"hidden\" name=\"lastname\" value=\"<?php echo $NLast ?>\">
                    <input type=\"submit\" value=\"View\" class=\"button\">
                  </form>
  <td align=\"right\" valign=\"top\" width=\"10\">
          <form name=\"edit\" method=\"post\" action=\"viewEdit.php\">
                    <input type=\"submit\" value=\"Edit\" class=\"button\">
                  </form></td>
  <td align=\"right\" valign=\"top\" width=\"10\">
          <form name=\"delete\" method=\"post\" action=\"<?$rs2 = odbc_exec($conn,$delQuery);?>\">
                    <input type=\"submit\" value=\"Delete\" class=\"button\">
                  </form></td>
                </td></tr>";
}

//If now rows are returned display message, and supply back button
if ($NFirst == "" && $NLast == "")
{
  echo "<html>
        <head>
            <link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\">
        </head>
        <body>
<table class=\"tabley\" width=\"400px\">
            <tr><td>No results</td></tr>
        </table>
        <form>
            <br/><input type=\"button\" name=\"returnButton\" value=\"Back\" class=\"button\" onclick=\"history.back();\">
        </form>
        </body>
        </html>";
}

if ($data == 0)
{
  $ANFirst = odbc_fetch_row($rs,"F_Name");
  $ANLast = odbc_fetch_row($rs,"L_Name");

  echo "<b>$ANLast</b> $ANFirst<br/>";
}

//close connection to database
odbc_close($conn);

?>

[/code]

Here is the code which is on the viewDetail.php script (for when the user clicks the view button)

[code]

<?

$NFirst = $_REQUEST['firstname'];
$NLast = $_REQUEST['lastname'];

echo "$NFirst";

?>

[/code]

This script will have the viewDetail query within it when i can see that the variables are being passed correctly.
Change this line....

[code=php:0]
$rs = odbc_exec($conn,$query);
[/code]

to....

if (!$rs = odbc_exec($conn,$query)) {
  echo odbc_error();
  exit;
} else {
  if (!odbc_num_rows($rs) > 0) {
    echo "No results found";
    exit;
  }
}
[/code]

That ought to help you debug some. You whole while() should really be wrapped within that. You need to check your query was successfull and that it actually found a result before your try and use it.

Also... as I said before. You do not need a while() loop if your only expecting one record.

Archived

This topic is now archived and is closed to further replies.

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