Jump to content

Dynamic PHP MySQL tales view and insert!!!


GD77

Recommended Posts

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*/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... :/

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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 :P...

 

-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 :P

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.