Jump to content

Archived

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

chico1st

Calling Table Using Variable

Recommended Posts

Okay I have one page with a form that selects a table to open for editing.
Then i try to open that table and display the values. The display function is held in a library.
(PHP 5.1.4,MySQL 5.0.22 )
(mysqlfreaks.com is broken im sorry)

Whenever i try to open the table using the variables i get:
[b]Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Lib.php on line 100[/b]
but if i just type in a table name instead of using the variable it works fine.
i think it might have to do with how i use the variable either .. the way i name it in the form (just below)
or the way i use it in my query (bottom)

I included most of the code but bolded what i think is important

[b]here is the form that grabs the table:[/b]
<body>
<form action = "editTable.php"
method = "post">

<table border = 1>
<tr>
<td colspan = 2><center>
<h2>Edit table data</h2>
</center></td>
</tr>

<tr>
<td colspan = 2><center>
[b] <select name = "tableName" size = 5>
<option value = "news">News</option>
<option value = "publications">Publications</option>
<option value = "author">Author</option>
<option value = "date">Date</option>
<option value = "picture">Picture</option>
<option value = "topic">Topic</option>
</select>
</center></td>[/b]
</tr>

<tr>
<td colspan = 2><center>
<input type = "submit"
value = "edit table">
</center></td>
</tr>
</table>

</form>

</body>

[b]Here is me calling my library function[/b]
<?php
include "Lib.php";

//check password

if ($pwd == $adminPassword){
$dbConn = connect();
[b]print Edit("$tableName");[/b]
} else {
print "<h3>You must have administrative access to proceed</h3>\n";
} // end if

?>

[b]
Here is the function[/b]
<?php
function Edit($tableName){
//given a table name, generates HTML table including
//add, delete and edit buttons

global $dbConn;
$output = "";
[b]$query = "SELECT * FROM {$tableName}";[/b]

[b]$result = mysql_query($query, $dbConn);[/b]

$output .= "<table border = 1>\n";
//get column headings

//get field names
$output .= "<tr>\n";
[b]while ($field = mysql_fetch_field($result)){[/b]
$output .= " <th>$field->name</th>\n";
} // end while

//get name of index field (presuming it's first field)
$keyField = mysql_fetch_field($result, 0);
$keyName = $keyField->name;

//add empty columns for add, edit, and delete
$output .= "<th></th><th></th>\n";
$output .= "</tr>\n\n";

//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
$output .= "<tr>\n";
//look at each field
foreach ($row as $col=>$val){
$output .= " <td>$val</td>\n";
} // end foreach
//build little forms for add, delete and edit


//delete = DELETE FROM <table> WHERE <key> = <keyval>
$keyVal = $row["$keyName"];
$output .= <<< HERE

<td>
<form action = "deleteRecord.php">
<input type = "hidden"
name = "tableName"
value = "$tableName">
<input type= "hidden"
name = "keyName"
value = "$keyName">
<input type = "hidden"
name = "keyVal"
value = "$keyVal">
<input type = "submit"
value = "delete"></form>
</td>

HERE;
//update: won't update yet, but set up edit form
$output .= <<< HERE
<td>
<form action = "editRecord.php"
method = "post">
<input type = "hidden"
name = "tableName"
value = "$tableName">
<input type= "hidden"
name = "keyName"
value = "$keyName">
<input type = "hidden"
name = "keyVal"
value = "$keyVal">
<input type = "submit"
value = "edit"></form>
</td>

HERE;

$output .= "</tr>\n\n";

}// end while

//add = INSERT INTO <table> {values}
//set up insert form send table name
$keyVal = $row["$keyName"];
$output .= <<< HERE

<td colspan = "5">
<center>
<form action = "addRecord.php">
<input type = "hidden"
name = "tableName"
value = "$tableName">
<input type = "submit"
value = "add a record"></form>
</center>
</td>

HERE;


$output .= "</table>\n";
return $output;
} // end Edit
?>

Share this post


Link to post
Share on other sites
Did you tried echoing your query before sending it to the mysql_query.

Then you can see what is wrong directly from your query. Probably your tablename is missing and you forget to send it somewhere by somehow.

Check with echoing step by step. And see what is wrong.

Share this post


Link to post
Share on other sites
okay whenever i:
echo $tableName;
there is just nothing there.. like no display... this happens in my library file and in my function calling file.
i dont know what gives
thanks though

Share this post


Link to post
Share on other sites
Okay it turns out that there is this new fangled $_POST() command that i didnt know about, all is now well in the world of php :D
ERIK!

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

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.