PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE Accounts ( account_sid INT AUTO_INCREMENT PRIMARY KEY, account_name VARCHAR(255) NOT NULL, account_address VARCHAR(255), account_city VARCHAR(100), account_state VARCHAR(50), account_zip VARCHAR(20) ); -- Insert some example values INSERT INTO Accounts (account_name, account_address, account_city, account_state, account_zip) VALUES ('John Doe', '123 Main St', 'Springfield', 'IL', '62701'), ('Jane Smith', '456 Elm St', 'Springfield', 'IL', '62702'), ('Bob Johnson', '789 Oak St', 'Chicago', 'IL', '60601');
Copy Clear
Copy Format Clear
<?php function getSql($post, $table, $db) { $result = $db->query("DESCRIBE $table"); $rows = $result->fetch_all(MYSQLI_ASSOC); $fields = array_column($rows, "Field"); $result->free(); if (!is_array($post) || empty($post)) { return ""; } $sqlParts = []; foreach ($post as $key => $val) { if (in_array($key, $fields)) { // $escapedVal = $db->real_escape_string($val); // $sqlParts[] = "$key = '$escapedVal'"; $sqlParts[] = "$key = '" . addslashes("$val") . "'"; } } return implode(', ', $sqlParts); } $post = array( 'account_sid' => 123, 'account_name' => "John Doe", 'account_address' => "123 Main's St", 'non_existent_field' => "This should be ignored" ); echo getSql($post, "Accounts", $mysqli); // $query = "DESCRIBE Accounts;"; // $result = $mysqli->query($query); // $rows = $result->fetch_all(MYSQLI_ASSOC); // $fields = array_column($rows, 'Field'); // $line = "'" . implode("','", $fields) . "'"; // print_r($rows);
Show:  
Copy Clear