Jump to content

Can i use this class for insert query ??


yaserloz

Recommended Posts

hi i made this glass for insert query i need some input about it and if its valid and if there any idea to put on it post it please

 

the function take array data and table name

 

<?php
class DatabaseActions{
    function Add($DataArray,$TableName){
        $text="'";
        foreach($DataArray as $key => $val)
        $text .= $val."','";


        $text  = substr($text,0,-2); 
        $query = "INSERT INTO $TableName VALUES ($text)";
        return $query;
    }
}
$a= new DatabaseActions ;
$array = array (1,"string","11-12-1009:2233","+++//4rrrr");
$tablename = "subject";
$output=$a->Add($array,$tablename);
   echo $output;
?> 

 

output

 

INSERT INTO subject VALUES ('1','string','11-12-1009:2233','+++//4rrrr')

Link to comment
Share on other sites

 

Not a fan of that tutorial because it claims to be a real world example, but has the following flaws:

 

No error handling.  In fact the author squelches errors by using the '@' symbol liberally through his code.  A true real world example would have exception handling.

 

No escaping of values or anything else to combat an injection attack.  An incredibly egregious omission for a tutorial aimed at newbies.

 

A complete lack of ability to handle complex queries.  Unless you have an embarrassingly simple domain, chances are you'll want to JOIN tables at some point.

 

It's just a bad tutorial all around.  Looks like the author wanted to make something that mimics a generic ORM, but neglected to build any of the functionality that makes them worthwhile to use.

Link to comment
Share on other sites

 

thanks dude

 

@Nightslyr 

can i use it as a base then develope it more and close all lacks u point to or should i ignore it and start with freash idea ?

i need to start somthing can Bear any change in the future even if its simple for starter :)

Link to comment
Share on other sites

To be honest, I think you should start with basic OO methodology before trying to move further.  Knowing the syntax of objects does nothing to prepare one to use them properly.  Right now, I have the feeling that if you tried to adapt the code in the tutorial, you would just blindly slap on parts that relate to the particular problem you're trying to solve rather than something more universally useful.

 

In short, you need to learn how to crawl before you can walk.

 

You should get two books before progressing much further:

 

PHP 5: Objects, Patterns, and Practice by Matt Zandstra [Link]

Design Patterns: Elements of Reusable Object-Oriented Software by the Gang of Four [Link]

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.