[thelist] MySQL/PHP Associative Array Insert

Bill Haenel mail at webmarketingworx.com
Sat Feb 1 10:38:01 CST 2003


> Can someone give me an example of how to do an insert to the
> database using these arrays?  My array is this...$item['itemID']

Maybe I'm not understanding the question clearly, but if I do you should
be able to use the array values just as you would any variable.

Here's a function I use to update announcement items in a calendar app
that takes an array as you described, $data is the array (like your
$item) and $update is a switch that tells the function whether to update
or insert, defaulting to insert:

// update announcement item
function update_announcement($data, $update=0) {
	$connect = dbconnect();
	$query = "UPDATE cal_announcements
		    SET
			user_id='".$data['user_id']."',
			s_date='".$data['s_date']."',
			e_date='".$data['e_date']."',
			title='".$data['title']."',
			content='".$data['content']."'
		    WHERE annc_id='".$data['annc_id']."'";
	if (empty($update)) {
		$query = "INSERT INTO cal_announcements
				(user_id,s_date,e_date,title,content)
			    VALUES
				('".$data['user_id']."',
				 '".$data['$s_date']."',
				 '".$data['e_date']."',
				 '".$data['title']."',
				 '".$data['content']."')";
	}
	$result = mysql_query($query) or die (mysql_error());
}

HTH.

BH




More information about the thelist mailing list