Create SQL Insert script from a MySQL Database and a $_GET[]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hagar
Forum Newbie
Posts: 18
Joined: Mon May 20, 2002 3:28 am
Location: Johannesburg, South Africa

Create SQL Insert script from a MySQL Database and a $_GET[]

Post by Hagar »

Code: Select all

<?php
# This code will generate you a sql script from a $_GET[] where you also have the
#option of a ID field in the Mysql DB
function insertion_script($tablename,$field_names,$has_id_field = "yes"){
		if($has_id_field == "yes"){
			$sql = "insert into {$tablename} (id,";
		}else{
			$sql = "insert into {$tablename} (";
		}

		for($i=1;$i <= count($field_names);$i++){
			if($i == count($field_names)){
			$sql .= "{$field_names[$i]})";
			}else{
			 $sql .= "{$field_names[$i]},";
			}
		#echo  "SQL Script: {$sql}<br>";
		}

		if($has_id_field == "yes"){

			$sql .="VALUES ('',";

		}else{
			$sql .="VALUES (";
		}

		for($i=1;$i <= count($field_names);$i++){
			if($i == count($field_names)){
				$sql .= "{$_GET[$field_names[$i]]}";
			}else{
				$sql .= "{$_GET[$field_names[$i]]},";
			}
		}
		$sql .= ")";

	#echo $sql;
	return $sql;
	}


?>
:D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is there a problem you are having with this code, or just wanted to share it?
Hagar
Forum Newbie
Posts: 18
Joined: Mon May 20, 2002 3:28 am
Location: Johannesburg, South Africa

Post by Hagar »

No I thought that I'll share it :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay, just wanted to make sure. :D
Post Reply