-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_statement_generator.php.php
More file actions
68 lines (61 loc) · 2.12 KB
/
SQL_statement_generator.php.php
File metadata and controls
68 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php // overloaded functions for sql insert query
// Created by Aaron C. 10/20/2024 Finished 02/06/2025
/**
* Generates an SQL INSERT statement string.
*
* @param string $tableName The name of the table to insert into.
* @param array $SQLValues An array of column names to be inserted.
* @return string The generated SQL INSERT statement.
*/
/* function getColumnName($tableName, $conn){
return $columnNames;
} */
function sql_inserting_com($tableName, $SQLValues){
// declared function variables
$arraySize = count($SQLValues);
$lastNameValue = $SQLValues[$arraySize - 1];
// start of SQL insert string building
$sqlString = "INSERT INTO $tableName (";
foreach($SQLValues as $SQLvalue){
if($SQLvalue != $lastNameValue){
$sqlString .= "`$SQLvalue`, ";
}
else{
$sqlString .= "`$SQLvalue`) VALUES (";
}
}
foreach($SQLValues as $SQLvalue){
if($SQLvalue != $lastNameValue){
$sqlString .= ":$SQLvalue, ";
}
else{
$sqlString .= ":$SQLvalue)";
}
}
return $sqlString;
}
/**
* Generates an SQL UPDATE statement string based on the number of columns to update.
*
* @param string $tableName The name of the table to update.
* @param array $tableColumnName An array of column names to be updated.
* @return string The generated SQL UPDATE statement.
*/
function update_string($tableName, $tableColumnName){
//require 'wLInventory.php';
$count = count($tableColumnName);
$primaryIDName = find_ID($tableName);// to find the primary ID name for the primary key column
$sqlString = "UPDATE $tableName SET `$tableColumnName[0]` =";
for($i = 1; $i <= $count; $i++){
if($count == 1){
$sqlString .= " :columnValue1 WHERE $primaryIDName = :pValue";
}
else if($i == $count){
$sqlString .= " :columnValue$i WHERE $primaryIDName = :pValue";
}
else{
$sqlString .= " :columnValue$i, `$tableColumnName[$i]` =";
}
}
return $sqlString;
}