Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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