#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    42
    Rep Power
    6

    Wanting to add one field to SQL DB and use PHP to upload a photo


    Hi All,

    I have a script that has a SQL Database Table named setup_category and has the following existing fields :::

    category_id
    category_parent
    category_path
    category_pathname
    category_name

    Then there is an admin page where you enter the fields through a PHP form and submit to insert into the SQL DB table above of course...

    My question is how can I easily add a field to assign uploaded images to each different category so the categories do not look bland and remain text only?

    The script does use other forms and SQL Queries to upload and place photos throughout listings on the site, but I am not sure how to pull what code is needed from those in order to add another table field to the setup_category existing table, add the upload function in the admin create category page form and then populate it to show on the category pages afterwards....

    I know some of you Gurus could look at the code below and see things in a second whereas I am scared to play around with it as I might crash the site and have spent hours reading up on methods to no avail...

    I pulled the code from the existing CREATE A CATEGORY ADMIN PAGE and it is:

    PHP Code:
    <form action="system_setup_category_add.php" method="post" name="form_setup_category_add" style="margin:0px">
                                        <table width="450" border="0" cellpadding="5" cellspacing="0" class="form_field">                                        
                                            <tr>
                                                <td width="80">
                                                    <? if ($ccategory_name_warn) { $field_class "form_field_warning"; } else { $field_class "form_field"; } ?>
                                                    <font class="<?= $field_class ?>"><?= $lang[lang_setup_categoryadd_field_name?></font> <font class="asterix">*</font>
                                                </td>
                                                <td>:</td>
                                                <td>
                                                    <input type="text" name="category_name" value="<?= $ccategory_name ?>" class="form_textbox" style="width:300px">
                                                    <script language="JavaScript">document.form_setup_category_add.category_name.focus();</script>
                                                    <? $_SESSION['ccategory_name_warn'] = ''?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td><?= $lang[lang_setup_categoryadd_field_parent?></td>                                                
                                                <td>:</td>
                                                <td>                                
                                                    <select name="category_location" class="textbox" style="width:305px">
                                                        <option value="0">Root</option>
                                                        <?

                                                            $db_connect 
    mysql_connect($db_host$db_username$db_password);
                                                            
    mysql_select_db($db_name$db_connect) || die(mysql_error());

                                                            
    $i    0;
                                                            
    $sql_query    "SELECT * FROM setup_category ORDER BY category_pathname ASC";
                                                            
    $result        mysql_query($sql_query) or die(mysql_error());
                                                            while (
    $row    mysql_fetch_array($result)) {

                                                                
    $category_id        $row[category_id];
                                                                
    $category_parent    $row[category_parent];
                                                                
    $category_name        $row[category_name];
                                                                
    $category_path        translate_path($row[category_path]);

                                                                
    ?>
                                                               <option value="<?= $category_id ?><? if ($category_id    == $ccategory_location) { ?> selected <? ?>><?= $category_path ?></option>
                                                                <?
                                                                
                                                            
    }
                                                            
    mysql_close($db_connect);
                                                            
                                                            
                                                        
    ?>
                                                    </select>                                                
                                                </td>
                                            </tr>                                            
                                            <tr>
                                                <td colspan="2"><img src="images/spacer.gif" width="1" height="1"></td>
                                                <td><input type="submit" value="<?= $lang[lang_setup_categoryadd_button?>" class="form_button"></td>
                                            </tr>                                        
                                            <tr>
                                                <td height="20"><img src="images/spacer.gif" width="1" height="1"></td>
                                            </tr>
                                        </table>
                                        </form>
    and the page that it calls (system_setup_category_add.php) to see as well :

    PHP Code:
    <?



        
    include("setting.php");
        
        
        
        
    // Add To Session
        
    while (list ($key$val) = each ($HTTP_POST_VARS)) {
            
    session_register("c$key");
            
    $_SESSION["c$key"] = $val;
        }
        
        
        
    // Reset Array
        
    $warning_fields    = array();    
        
    reset ($warning_fields);


        
    // Validate each fields
        
    if (empty($category_name))            { $warning_fields[] = "category name";        $_SESSION["ccategory_name_warn"]        = 1; }
        
    $warning_text get_error_msg($warning_fields);


        
    // Continue ...
        
    if ($warning_text) {
            
            
    setcookie("warning"$lang[lang_setup_categoryadd_warning]);
            
    $destination    "setup_category_add.php";

        }
        else {
            
            if (
    $setup_demo    == "N") {
            
                
    $db_connect mysql_connect($db_host$db_username$db_password);
                
    mysql_select_db($db_name$db_connect) || die(mysql_error());                
                
                
    $sql_query     "
                INSERT INTO setup_category SET
                category_parent    = '
    $category_location',
                category_name    = '
    $category_name'
                "
    ;
                
    mysql_query($sql_query) or die(mysql_error());
                
    mysql_close($db_connect);
                
                
                
    // clear session ...
                
    reset($HTTP_POST_VARS);
                while (list (
    $key$val) = each ($HTTP_POST_VARS)) {
                    
    session_unregister("c$key");
                }            


            }
            else { 
    setcookie("warning"$setup_demo_sorry); }
            
    $destination "setup_category.php";            
            
        }    
        
    header("Location: $destination");


    ?>
    Can anyone help with what I would need to add to the simple admin CREATE A CATEGORY page form and/or the the system_setup_category_add.php that form calls in order to have it check if table/field exists and upload a photo to assign to the category_id so I can then use a call function to populate the different images on each of the different category pages?

    Any help is greatly appreciated and if you also know the query string or image call code that would go on the page when the front-side user visits that category that displays the image for that category it would be extremely helpful...

    The image would be under the header and its size would be 1000px by 405px if that is needed or maybe just a 100% tag and I can put a note in the admin the images need to be 1000px by 405px...

    Thanks to ALL and hopefully the answer comes quickly...

    Jay
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    To start with, you need to set up a test server so you don't have to worry about crashing the production server. You never work on a production server to do development. The second thing you need to do is switch from the deprecated (for more than a decade) MySQL extensions to PDO and prepared statements. That will satisfy most of the third thing you need to do which is secure your code.

    If I understand your question correctly, this is really a mysql question. You add a new field to the table with the ALTER ADD command. However, that is done outside of PHP by manually entering the command in a 'mysql' session from the command line (or with phpMyAdmin). The field you want to add would be a path to the associated image which is a 'char' type.
    Last edited by gw1500se; June 17th, 2014 at 06:36 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    42
    Rep Power
    6

    Thanks!!


    Thanks for helping point me in the right direction...

    I will read on the items you suggested ...

    Thanks again...

IMN logo majestic logo threadwatch logo seochat tools logo