Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts

    populate mysql table w/ php

    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:
    <?php
    $con = mysql_connect("localhost.localdomain","username","userpassword");
    if(!$con){
    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]})";
    mysql_query($sql);
    $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)");
    }
    }

    /**********/

    mysql_close($con);
    ?>

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

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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 [code] ... [/code] or [php] ... [/php] tags. It makes it easier to read and thus makes it easier for people to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •