Forum Moderators: coopster

Message Too Old, No Replies

tables from sql database

Notice: Undefined index: Admissions in...

         

daisee

7:28 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



I've just started using PHP and am trying to create tables from data in MySQL database. I'm getting an error stating that some of my variables are undefined. How do i correct this?.. where would i define them?... this is my code

<html>
<body>

<?php
session_start();
include ("odbc.inc");
include ("utils.inc");
$php_self = $_SERVER['PHP_SELF'];

if ( isset($_POST['submit']) ) { // if form is submitted, process it

for($i=1; $i<=$_POST['lobcod']; $i++) {
if ( isset($_POST["lobcod$i"] ) ) {
print $_POST["lobcod$i"]." is checked.<br/>";
}
}

} else { // if form isn't submitted, output the form

print "<form action=\"tables.php\" method=\"POST\">\n";

$sql_rset = "SELECT admissions FROM bh_systems.net_bh_followups_lob_alos n";
$link = mysql_connect("", "", "");
$db_link = db_connect("");
$resultrset = odbc_exec($db_link, $sql_rset) or die("SELECT FAILED: $sql_rset".odbc_errormsg());
if ($resultrset) {
print "<table width=200 border=1>\n";
print "<tr>\n";

print "<th>&nbsp; </th>\n";
print "<th> Admissions </th>\n";
print "<th> followups7</th>\n";
print "<th> percent_followups7</th>\n";

print "</tr>\n";
//create table
$i = 0;
while ( $row = odbc_fetch_array($resultrset) ) {
$i++;

print "<tr>\n";

print "<td><input type=\"checkbox\" name=\"lobcod$i\" value=\"$row[lobcod]\"></td>\n";

echo "<td>{$row['Admissions']}</td>\n";

echo "<td>{$row['lobcod']}</td>\n";

echo "</tr>\n";

}//end while
print "</table>\n";
} else {
echo("<P>Error performing query: " .
odbc_error() . "</P>");

}

}
?>
</form>
</body>
</html>

cmarshall

8:25 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Undefined variables is usually a PHP warning, not an error.

That means something like this:

$thevar = $first_time_used;

If you had not previously set $first_time_used, it would give that warning, because the variable is undefined. However, you should still be able to run the script, as it merely uses an undefined (blank) for its value. PHP doesn't really have "junked" variable space like C or C++.

daisee

8:38 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



thanks alot for the feedback. Ok so i used isset and it corrected the warning... and like you said i do have some output.. however now i just have table headers and no data is being displayed from my database... i have run checks to ensure that the data is bing picked (print odbc_num_rows($resultrset);) and i can see that it is... but i cant get it to display it on my final output... any ideas?

Here's my updated code:

<html>
<body>

<?php
session_start();
include ("odbc.inc");
include ("utils.inc");
$php_self = $_SERVER['PHP_SELF'];

if ( isset($_POST['submit']) ) { // if form is submitted, process it

for($i=1; $i<=$_POST['lobcod']; $i++) {
if ( isset($_POST["lobcod$i"] ) ) {
print $_POST["lobcod$i"]." is checked.<br/>";
}
}

} else { // if form isn't submitted, output the form

print "<form action=\"tables.php\" method=\"POST\">\n";

$sql_rset = "SELECT * FROM bh_systems.net_bh_followups_lob_alos n";

$db_link = db_connect();
$resultrset = odbc_exec($db_link, $sql_rset) or die("SELECT FAILED: $sql_rset".odbc_errormsg());
print odbc_num_rows($resultrset);
if ($resultrset) {
print "<table width=200 border=1>\n";
print "<tr>\n";

print "<th>&nbsp; </th>\n";
print "<th> lobcod </th>\n";
print "<th> Admissions </th>\n";
print "<th> followups7</th>\n";
print "<th> percent_followups7</th>\n";

print "</tr>\n";
//create table
$i = 0;
while ( $row = odbc_fetch_array($resultrset) ) {
$i++;
//print $row['admissions'];
print "<tr>\n";

if (isset($lobcod)) {
print "<td><" .$row['lobcod']. "</td>\n";
//echo "<td>{.$row['lobcod']}."</td>\n";
}

echo "</tr>\n";

}//end while
print "</table>\n";
} else {
echo("<P>Error performing query: " .
odbc_error() . "</P>");

}

}
?>
</form>
</body>
</html>

cmarshall

8:52 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not familiar with ODBC functions, being a MySQeaLer, myself.

I did see this [w3schools.com], which might be helpful.

I suspect that what is happening is you are not getting back a proper associative array, or the keys aren't exactly what you expect them to be.

I'd do a

print_r ( $row );
to see what's coming out of the DB.

Also, this doesn't make a whole lot of sense:

if (isset($lobcod)) {
print "<td><" .$row['lobcod']. "</td>\n";
}

It will always fail the test. I think you mean to do this:

if (isset($row['lobcod'])) {
print "<td><" .$row['lobcod']. "</td>\n";
}

By the way, welcome to WebmasterWorld.

daisee

9:04 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



PERFECTTT! thanks alot! that helped me get some of my data so now i'll just play with it... i think i have it from here... your the best!..

cmarshall

9:09 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad it worked out. This is kewl site.

I thought that this [webmasterworld.com] was a pretty good post.