RopeADope Posted February 18, 2011 Share Posted February 18, 2011 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>"; }; Quote Link to comment https://forums.phpfreaks.com/topic/228110-form-generating-function-need-help-with-modification/ Share on other sites More sharing options...
denno020 Posted February 18, 2011 Share Posted February 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/228110-form-generating-function-need-help-with-modification/#findComment-1176306 Share on other sites More sharing options...
RopeADope Posted February 18, 2011 Author Share Posted February 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228110-form-generating-function-need-help-with-modification/#findComment-1176332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.