GD77 Posted August 13, 2012 Share Posted August 13, 2012 Hello: I've made a function to retrieve columns from a DB and output an input tag for each returned value in order to insert new data... I've reached the validation process :/ how to validate input without making validation function for each table just want it to be dynamic if possible... function Data_Fetch($tbls) { $qry_tbl=mysql_query("SELECT COUNT( * ) FROM `information_schema`.`TABLES` WHERE (`TABLE_NAME` LIKE '%".$tbl."%')"); $qry_Resutl=mysql_result($qry_tbl,0); if($qry_Resutl==0){ echo "TABLE NOT AVAILBLE!!!"; }else{ $tbls=mysql_real_escape_string($tbls); $qry_Fields=mysql_query("SELECT * FROM `".$tbls."` ORDER BY `id` ASC"); $i = 0; print_r("<form name='form_tbl_insert' method='post' target='_self' class='form_tbl_insert'><table class='tbl_insert'>"); while ($i < mysql_num_fields($qry_Fields)) { $col = mysql_fetch_field($qry_Fields, $i); $value_Insert=$_POST[$col->name]; if(isset($value_Insert)) {$value_Insert1=$value_Insert;}else{$value_Insert1="";} if (!$col) {print_r( "No information available<br />");exit;} if($col->name!=='id'){ print_r("<tr><td>".$i."</td><td>".$col->name."</td><td><input type='text' name='".$col->name."' value='".$value_Insert1."'>".$col->type." ".$col->max_length."</td></tr>"); }//END if not id $i++; }//END while print_r("<tr><td> </td><td> </td><td><button type='submit'>Insert Data</button></td></tr></table></form>"); mysql_free_result($qry_Fields); }//END else }/*END Fn Data_Fetch*/ Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 13, 2012 Share Posted August 13, 2012 Please use the [code[/code] tags when posting code, as it helps make both your post and your code a lot more readable. As for validation: You have to make it specific for each types of input, otherwise it will not work. That's like building an apartment highrise, and putting the same lock in every single door. Quote Link to comment Share on other sites More sharing options...
GD77 Posted August 13, 2012 Author Share Posted August 13, 2012 Please use the [code[/code] tags when posting code, as it helps make both your post and your code a lot more readable. As for validation: You have to make it specific for each types of input, otherwise it will not work. That's like building an apartment highrise, and putting the same lock in every single door. You re right did not notice the before, ok i ll explain more: Let s say you ve created new table along with existing ones... instead coding new validation rules for each newly created table you ll have one function able to do so... so far can t manage it and facing to manually recode validation for each new table... :/ Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 How would your code know what rules to apply where? Quote Link to comment Share on other sites More sharing options...
GD77 Posted August 13, 2012 Author Share Posted August 13, 2012 How would your code know what rules to apply where? Already I ve coded that section and It select the requested table, generate a form based on each table's columns only facing the validation Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 So where are you stuck? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 13, 2012 Share Posted August 13, 2012 What you could do, is to make a function (or a class of functions) that uses the table name to select the correct validation rules and their order. That said, it sounds like you're are making an overly complex structure here. Are you using a variable database structure, or does it just sound like that? Also, why are you giving users direct access to your database? Whatever the case might be, we need a bit more background information on this, including what you're trying to accomplish; The end result, not what you think you have to do to get there. Examples of the calling code never hurts, btw. Quote Link to comment Share on other sites More sharing options...
GD77 Posted August 14, 2012 Author Share Posted August 14, 2012 ChristianF you re right about complex things... yet m not giving this sections to users it s for my back-end as small cms to control the data. I'll try to simply it more and yes they all are class functions ... -Let s say you have 7 or 20 tables and you want to insert data in each table I m talking about 100-1500+ row of data and each table has no less then 25 columns... instead of coding the insert form and validation for each table manually M trying to do it dynamically. -So far I ve made class functions to retrieve tables names then select each table's columns then make a form with < input > for each entry... -Now here where I m stuck validating each entry for each table dynamically one function to validate all entries from any table, what if I wanted to add another table or 10 do I ve to recode each table validation? this is it. jesirose this is where I m now Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 As I said in my previous post, you have to set up the rules for validation individually tailored for each table. That means having a function that can do this for you, based upon the name of the table. If you only need rudimentary validation (not recommended at all), then you can base it upon the results of DESCRIBE TABLE, and use the field definitions to select the correct validation rules. Speaking from experience, you will want to go with the statically coded version. Having one .php file for each table, so that you can tailor it to do exactly what you want. Meta/dynamic programming is not only lots more complicated, but it is also fraught with perils and security risks. Not to mention really inefficient in just about every single case. Quote Link to comment Share on other sites More sharing options...
GD77 Posted August 14, 2012 Author Share Posted August 14, 2012 Totally agree with ChristianF just wanted to see if we can save time with this well back to coding each table's requirements. I'll keep the post open in case anyone got any idea. Thanks again everyone. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 You're welcome, glad I could be of help. Quote Link to comment 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.