PHP Classes

Make SQL: Generate SQL queries from request values array

Recommend this page to a friend!
  Info   Example   Demos   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 70%Total: 291 All time: 7,494 This week: 455Up
Version License PHP version Categories
make-sql 1.1GNU General Publi...5PHP 5, Databases
Description 

Author

This class can generate SQL queries from request values array.

It takes as parameters the array of the current HTTP request values, the database name and the table name.

The class can generate SQL queries from the parameters to perform INSERT, UPDATE and DELETE queries.

The request parameter names define the table field names but it is possible to exclude certain field names that will not be included in the queries.

Innovation Award
PHP Programming Innovation award nominee
May 2016
Number 11


Prize: PHP Tools for Visual Studio Personal license
Many applications need to update database table records with values taken from request parameters.

This class can generate SQL INSERT, UPDATE and DELETE queries to a given table using escaped request parameter values in the queries. It can also exclude request parameters that should be ignored.

Manuel Lemos
Picture of andrea battellocchi
  Performance   Level  
Name: andrea battellocchi <contact>
Classes: 2 packages by
Country: Italy Italy
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Example

<?php
include("class/makeSql.class.php");


if (
$_REQUEST)
{
$sqlC = new makeSql;
 
$sqlC->request = $_REQUEST;
$sqlC->table_name = $_REQUEST["table_name"];
$sqlC->db_name = $_REQUEST["db_name"];
$sqlC->primary_key = "pk";
$sqlC->exclude = array("table_name","db_name");
}


function
makeRandomForm()
{

 
$formID = array("name","surname","city","school","address","pet_name","form_1","form_2","form_3","form_4","form_5","form_6","form_7","form_8","form_9");
 
$html = '<form action="#" method="POST">';
 
$q = rand(5,15);
 
$html .= "<h3>Configuration fields (can set directly in php class instance)</h3>";
 
$html .= "<label for='table_name'>Table Name</label><br /><input type='text' placeholder='table_name' name='table_name' id='table_name' value='table'><br />";
 
$html .= "<label for='db_name'>Data base name</label><br /> <input type='text' placeholder='db_name' name='db_name' id='db_name' value='db'><br />";
 
$html .= "<label for='pk'>Primary key (unique)</label><br /> <input type='text' placeholder='primary key' name='pk' id='pk' value='100'><br />";
 
//$html .= "<label for=''></label><input type='text' placeholder='' name='' id='' ><br />";

 
$html .= "<h3>Random fields</h3>";

  for (
$i=0;$i<$q;$i++)
    {
       
$html .= "<label for='".$formID[$i]."'>".$formID[$i]."</label><br /><input type='text' placeholder='".$formID[$i]."' name='".$formID[$i]."' id='".$formID[$i]."' ><br />";
    }

 
$html .= "<input type='submit' value='Send'><br />";
       
 return
$html;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>makeSql.class.php - example</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 1.24" />
</head>

<body>
<?php echo makeRandomForm();?>
<pre>
<?php


if ($_REQUEST)
{
echo
$sqlC->make_sql_insert() . "<br />";
echo
$sqlC->make_sql_update() . "<br />";
echo
$sqlC->make_sql_delete() . "<br />";
echo
$sqlC->get_last_sql() . "<br />";
}
?>
</pre>
</body>

</html>


Details

This class make a wellformed SQL string for insert,update ad delete. Catch $_REQUEST ($_GET or $_POST) from form and generate sql string. It's simple but userfull.

  demo pageExternal page  

Open in a separate window

  Files folder image Files (3)  
File Role Description
Files folder imageclass (1 file)
Plain text file examples.php Example example file
Plain text file read.me Doc. readme file

  Files folder image Files (3)  /  class  
File Role Description
  Plain text file makeSql.class.php Class Class file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:291
This week:0
All time:7,494
This week:455Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:-
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:70%StarStarStarStar
Rank:286