Jump to content

Form generating function--Need help with modification


RopeADope

Recommended Posts

Hi all.

 

I have the following function which auto-generates a form based on a database table.  What I want to do is "cancel" the function if the table doesn't exist.

 

The function as is...

function build_form($table_name){
$sql="SELECT * FROM $table_name";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$i=0;
echo "<form method=\"post\" action=\"/php/process_data.php\">";
echo "<input type=\"hidden\" name=\"selected_table\" value=\"" . $table_name . "\"/>";
echo "<table>";
echo "<tr><td colspan=\"2\" style=\"font:1em arial;font-weight:bold;text-align:center;\">Input Form: " . $table_name ."</td></tr>";
$field_names=array();
while ($i < mysql_num_fields($result)){
	$fields=mysql_fetch_field($result,$i);
	echo "<tr><td>" . $fields->name . "</td><td><input type=\"text\" size=\"30\" name=\"" . $fields->name . "\" /></td></tr>";
	$i++;
	};
echo "<tr><td colspan=\"2\" style=\"text-align:center;\"><input type=\"submit\" value=\"Submit Data\" style=\"width:75%\" /></td></tr>";
echo "</table>";
echo "</form>"; 
};

 

Would something like this work? (note lines 3-5)

function build_form($table_name){
$sql="SELECT * FROM $table_name";
        if(!$sql){
            return;
        };
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$i=0;
echo "<form method=\"post\" action=\"/php/process_data.php\">";
echo "<input type=\"hidden\" name=\"selected_table\" value=\"" . $table_name . "\"/>";
echo "<table>";
echo "<tr><td colspan=\"2\" style=\"font:1em arial;font-weight:bold;text-align:center;\">Input Form: " . $table_name ."</td></tr>";
$field_names=array();
while ($i < mysql_num_fields($result)){
	$fields=mysql_fetch_field($result,$i);
	echo "<tr><td>" . $fields->name . "</td><td><input type=\"text\" size=\"30\" name=\"" . $fields->name . "\" /></td></tr>";
	$i++;
	};
echo "<tr><td colspan=\"2\" style=\"text-align:center;\"><input type=\"submit\" value=\"Submit Data\" style=\"width:75%\" /></td></tr>";
echo "</table>";
echo "</form>"; 
};

As I took the time (<10 seconds) to search Google, I could just rip off the website that I found the code on and post it here to make myself look good, but I'm not going to do that. Instead I'll provide you the link so you can go and learn for yourself.

http://snippets.dzone.com/posts/show/3369

 

I don't mean to sound like a jerk, but so many questions can be easily answered by a simple Google search..

 

Denno

Solved it this way...

if($result=="") return;

 

I'm not meaning to be rude, but I could have goolged it myself and probably found the same thing.  I didn't because I have specific code with a specific problem.  That's why I prefer the forums over google searching.  In most cases, I can get a direct answer for a specific problem instead of something generic.

 

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.