开发者

Insert MySQL table column array into new MySQL table

I have a php form that gets dumped into a MySQL table. I create an array that is populated from a Product table using php.

Queries the table to get the values for the array - works fine.

 <?php 
 function getProducts ($id)
 {
    $sql =("SELECT * FROM products where products.CAT_ID = $id AND DISABLED ='NO'");
    $result = mysql_query($sql);
    if ($result > 0)
    {
    $products = array();
    $x=0;
    while($row = mysql_fetch_array($result))
    {
        $products[$x]= $row;
        $x += 1;
    }
    return $products;
    }
    else return false;  
}
function newProd(){
global $products;
}
?>

Then I use the above array to create labels in a javascript accordion. Next to the labels -are select boxes. (Removed unnecessary page elements.. ) Html/php form below that displays the above array.

<?php include('includes/query.php');?>

Inventory

<body onload="toggleField()">

        <div id="container" class="container_16">
        <form name="form1" id="form1" action="insert.php" method="post" enctype="multipart/form-data" style="height: inherit">
            <div id="Info" class="grid_16"> 
                <fieldset>
                    <label id="Label1">
                        <span class="Labels">Store Information</span> <br>
                    </label>
                    <div>
                    <label for="name"> 
                        <span class="Labels"> Name :</span> </label> 
            <input id="name" name="name" type="text">  
                    </div>  
          </fieldset>
            </div>
            <hr id="SpacePadding">
            <hr class="SpacePadding">
            <div class="clear"></div>
                <!-- Start of the Accordion Container-->
                <!-- Start array, make sure Category Array isn't false -->
                <?php if($categoryArray !=false):?>
                    <!-- create the variables for modulus -->
                    <!-- Start the foreach loop for the category Array -->
                    <?php $cataCounter = 0; ?>
                    <?php $closeSection = false; ?>
                    <?php foreach($categoryArray as $row):?>
                        <?php if($cataCounter % 2 == 0): ?>
                            <div class="push_1 grid_8">
                            <?php $closeSection = false; ?>
                        <?php else: ?>
                            <div class="grid_8">
                            <?php $closeSection = true; ?>
                        <?php endif; ?><!-- insert modulus-->
                                <div class="AccordionTitle"><?php echo $row['CATEGORY_NAME'];?></div>
                                <div class="Accordi开发者_JAVA技巧onContent">
                                    <table>
                                        <?php foreach(getProducts($row['CAT_ID']) as $row):?>
                                        <tr>
                                            <th class="boldLabels"><?php echo $row['PRODUCT_NAME'];?>
                                            <span class="label"></span></th>
                                            <th><span class="Labels">Count:</span></th>
                                            <td>
                                                <select name="selCount[]" onChange="toggleField(this.value);" class="Listbox">
                                                    <option value="Select">Select One</option>
                                                    <option value="0">0</option>
                                                    <option value="1">1</option>
                                                    <option value="2">2</option>
                                                    <option value="3">3</option>
                                                    <option value="4">4</option>
                                                    <option value="5">5</option>
                                                    <option value="6">6</option>
                                                    <option value="7">7</option>
                                                    <option value="8">8</option>
                                                    <option value="9">9</option>
                                                    <option value="10">10</option>
                                                    <option value="Other">Other(Specify)</option>
                                                </select>
                                                <input type="text" name="otherCount[]" class ="Listbox" style="display: none;">
                                            </td>
                                            <td><span class="Labels">Need:</span></td>  
                                            <td>
                                                <select name="selNeed[]" onChange="toggleField2(this.value);" class="Listbox">
                                                    <option value="Select">Select One</option>
                                                    <option value="0">0</option>
                                                    <option value="1">1</option>
                                                    <option value="2">2</option>
                                                    <option value="3">3</option>
                                                    <option value="4">4</option>
                                                    <option value="5">5</option>
                                                    <option value="6">6</option>
                                                    <option value="7">7</option>
                                                    <option value="8">8</option>
                                                    <option value="9">9</option>
                                                    <option value="10">10</option>
                                                    <option value="Other">Other(Specify)</option>
                                                </select>
                                                <input type="text" name="otherNeed[]" class ="Listbox" style="display: none;">
                                            </td>
                                        </tr>
                                        <?php endforeach;?>
                                    </table>
                                </div>
                            </div>
                            <?php if($closeSection): ?>
                                <div class="clear"></div>
                            <?php endif; ?>
                        <?php $cataCounter += 1; ?>
                    <?php endforeach;?>
            <?php endif;?>

        <input name="Submit" type="submit" value="submit">
        </div>
        </form>
        <br>
    </div>
        <footer>
        <div align="center">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Daily Inventory Counts</a></li>
                <li><a href="#">Contact System Admin</a></li>   
            </ul>
        </div>
    </footer>
</body>

When submitted, I want the Product_Name to go inside a new table with the correlating select option. (i took out the database connection info out.)

    <?php
//Assign array
$SelCount = $_POST['selCount'];
$SelNeed = $_POST['selNeed'];
$otherNeed = $_POST['otherNeed'];
$otherCount = $_POST['otherCount'];

$limit = count($SelCount);

for($i=0;$i<$limit;$i++) {
    $SelCount[$i] = mysql_real_escape_string($SelCount[$i]);
    $SelNeed[$i] = mysql_real_escape_string($SelNeed[$i]);
    $otherNeed[$i] = mysql_real_escape_string($otherNeed[$i]);
    $otherCount[$i] = mysql_real_escape_string($otherCount[$i]);    

if  (($SelCount[$i]) !="Select" ) {
$sql = ("INSERT INTO inventory ( STORE_ID, REQUESTOR, ITEM_COUNT, ITEM_NEED, NEED_COUNT, OTHER_COUNT) 
VALUES ('$_POST[store]', '$_POST[name]', '".$SelCount[$i]."', '".$SelNeed[$i]."', '".$otherNeed[$i]."', '".$otherCount[$i]."')");

    if(mysql_query($sql ,$db)) 
        echo "$i successfully inserted.<br/>";
        else 
        echo "$i encountered an error. <br/>";

    } 
}

?>


Here's what I would do:

 function getProducts($id)
 {
    $sql = 'SELECT * FROM products where products.CAT_ID = ' . $id . ' AND DISABLED = "NO"';
    $result = mysql_query($sql);
    if($result !== false) {
        while($row = mysql_fetch_array($result)) {
            $products[]= $row;
        }
    } else {
        $products = false;
    }
}

Then you can print it out via:

if($products = getData(id)) {
   foreach($products as $product) {
      echo $product['database_field_name'];
   }
}

To see them all for debugging purposes, you can use this:

echo '<pre>' . print_r($products, true) . '</pre>';

Also, you have to sanitize $id somewhere using: http://php.net/manual/en/function.mysql-real-escape-string.php and perhaps use intval() if it's an integer.

Store data in session:

session_start();
$_SESSION['name'] = $product['database_field_name'];

And then on the insert.php page you can read the data via:

echo $_SESSION['name'];

Then after you don't need it anymore, you can unset it:

unset($_SESSION['name']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜