View Full Version : populate mysql table w/ php

12-10-2008, 11:14 PM
I am trying to populate my two mysql tables that are already created using php. my two tables are 'logos' and 'files'.

Here is my php code:
$con = mysql_connect("localhost.localdomain","username","userpassword");
die('Could not connect: ' . mysql_error());

mysql_select_db("jwfugate_db", $con);

* Initial Database Setup
* run this only once per company, then comment it out or remove
$company = 'KSM';

// $logos will be an array of the types of logos, and each type will be an array of the files for that type of logo
$logos['KSM - 1 Color'] = array('ksmcpa.eps','ksmcpa.jpg');
$logos['KSM - BW'] = array('ksmcpa_bw.eps','ksmcpa_bw.jpg'); // just for example purposes

// the thumbnails array will be associated to the logo type by using the same key value
$thumbnails['KSM - 1 Color'] = 'ksm_color.gif';
$thumbnails['KSM - BW'] = 'ksm_bw.gif';

// this is a more complex foreach loop
// it gives you access to the array keys -- foreach($array as $key=>$value)
foreach($logos as $type=>$files){
$sql = "INSERT INTO logos (company,title,filename) VALUES ($company,$type,{$thumbnails[$type]})";
$logos_id = mysql_insert_id(); // this returns the id of the last inserted item
foreach($files as $filename){
mysql_query("INSERT INTO files (logos_id,filename) VALUES ($logos_id,$filename)");



Can anyone explain why my tables are not being populated via this php code?

12-10-2008, 11:22 PM
Judging by the column names, at least some of your columns are expecting string data. String data within your query must be enclosed in single-quotes.

I'd suggest looking into proper error reporting and proper error handling. MySQL will report errors if you let it.

I'd also suggest, in the future, wrapping your posted code in
... or
... tags. It makes it easier to read and thus makes it easier for people to help you.