Mysql Insert Function

PHP

MIT License

Mysql Insert Function

Download (right click, save as, rename as appropriate)

Embed

Tags:

Mysql
 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
<?php
function insert($table, $array)
	{	
		if(!is_array($array))
		{
			return 1;
		}else
		{
			$query = "INSERT INTO $table (";
			$turns = 1;
			$elements = sizeof($array);
			
			foreach ($array as $column => $value)
			{
				$query .= $column;
				if($turns != $elements)
					$query .= ",";
				$turns++;
			}
			
			$query .= ") VALUES (";
			$turns = 1;
			
			foreach ($array as $column => $value)
			{
				$query .= "'" . $value . "'";
				if($turns != $elements)
					$query .= ",";
				$turns++;
			}
			$query .= ");";
			return mysql_query($query);
		}
}
?>