如何在我的order.php中插入产品名称,单位名称,价格

时间:2018-09-26 00:09:59

标签: php

我正在使用动态选择框并获取数据库中的数据,但是我有一个大问题,我无法解决,任何人都可以告诉我或更正我在这里写的错误。因为每次我尝试提交按钮时,只有Product_id保存在数据库中,这是我的代码,我希望它们分别为Product_nameProduct_quantity Product_unitProduct_price将存储在数据库中,而不是product_id

delivery_form.php

<form class="form-horizontal" method="POST" action="deliveryformsave.php" enctype="multipart/form-data">
<tr>
<td align="right"><h3>Product:</h3></td>
</tr>
    <tr>
    <?php
    //Include database configuration file
    include('connect-db.php');

    $query = $DBcon->query("SELECT * FROM product WHERE product_status = 'available' ORDER BY product_name ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;
    ?>

    <select name="item1" id="product" >
        <option value="">Select Country</option>
        <?php
        if($rowCount > 0){
            while($row = $query->fetch_assoc()){ 
                echo '<option value="'.$row['product_id'].'">'.$row['product_name'].'</option>';
            }
        }else{
            echo '<option value="">Product not available</option>';
        }
        ?>
    </select>

    <select name="quantity1" id="quantity">
        <option value="">Select Product first</option>
    </select>

    <select name="unit1" id="unit">
        <option value="">Select state first</option>
    </select>

<div class="control-group">
            <div class="controls">
            <button name="submit" type="submit" class="btn btn-success"><i class="icon-save icon-large"></i>&nbsp;Save</button>
            </div>
        </div>
    </form>                 

        <script type="text/javascript">
$(document).ready(function(){
    $('#product').on('change',function(){
        var product_id = $(this).val();
        if(product_id){
            $.ajax({
                type:'POST',
                url:'ajaxData2.php',
                data:'product_id='+product_id,
                success:function(html){
                    $('#quantity').html(html);
                    $('#unit').html('<option value="">Select state first</option>'); 
                }
            }); 
        }else{
            $('#quantity').html('<option value="">Select country first</option>');
            $('#unit').html('<option value="">Select state first</option>'); 
        }
    });

    $('#quantity').on('change',function(){
        var product_unit = $(this).val();
        if(product_unit){
            $.ajax({
                type:'POST',
                url:'ajaxData2.php',
                data:'product_unit='+product_unit,
                success:function(html){
                    $('#city').html(html);
                }
            }); 
        }else{
            $('#unit').html('<option value="">Select state first</option>'); 
        }
    });
});
</script>

这是我的ajaxdota2.php

 <?php
//Include database configuration file
include('connect-db.php');

if(isset($_POST["product_id"]) && !empty($_POST["product_id"])){
    //Get all state data

    $query = $DBcon->query("SELECT * FROM product WHERE product_id = ".$_POST['product_id']." AND product_status = 'available' ORDER BY product_quantity ASC");


    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display states list
    if($rowCount > 0){
        echo '<option value="">STOCK</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['product_id'].'">'.$row['product_quantity'].'</option>';
        }
    }else{
        echo '<option value="">State not available</option>';
    }
}


if(isset($_POST["product_unit"]) && !empty($_POST["product_unit"])){
    //Get all state data

    $query = $DBcon->query("SELECT * FROM product WHERE product_id = ".$_POST['product_unit']." AND product_status = 'available' ORDER BY product_unit ASC");


    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display states list
    if($rowCount > 0){
        echo '<option value="">UNIT</option>';
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['product_id'].'">'.$row['product_unit'].'</option>';
        }
    }else{
        echo '<option value="">State not available</option>';
    }
}

if(isset($_POST["product_price"]) && !empty($_POST["product_price"])){
    //Get all city data


        $query = $DBcon->query("SELECT * FROM product WHERE product_id = ".$_POST['product_price']." ORDER BY product_price ASC");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display cities list
    if($rowCount > 0){

        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['product_id'].'">'.$row['product_price'].'</option>';
        }
    }else{
        echo '<option value="">not available</option>';
    }
}
?>

这是我在表单和数据库中的输出

enter image description here

这是我的deliveryformsave.php

<?php
$mysqli = new mysqli("localhost", "root", "", "database");


if ($_SERVER["REQUEST_METHOD"] == "POST") {



    $Date_ = $mysqli->real_escape_string($_POST['Date_']);  
    $Time_Travel = $mysqli->real_escape_string($_POST['Time_Travel']);
    $Time_IN_OUT = $mysqli->real_escape_string($_POST['Time_IN_OUT']);      
    $Driver_Name = $mysqli->real_escape_string($_POST['Driver_Name']);
    $Project_name = $mysqli->real_escape_string($_POST['Project_name']);
    $Jobsite = $mysqli->real_escape_string($_POST['Jobsite']);
    $Foreman_name = $mysqli->real_escape_string($_POST['Foreman_name']);
    $supplier = $mysqli->real_escape_string($_POST['supplier']);    
    $Vehicle = $mysqli->real_escape_string($_POST['Vehicle']);      
    $vehicle_plate_no = $mysqli->real_escape_string($_POST['vehicle_plate_no']);    
    $item1 = $mysqli->real_escape_string($_POST['item1']);
    $quantity1 = $mysqli->real_escape_string($_POST['quantity1']);          
    $unit1 = $mysqli->real_escape_string($_POST['unit1']);


            $sql = 
            "INSERT INTO deliveryrecords (Date_, Time_Travel, Time_IN_OUT, Driver_Name, Project_name, Jobsite, Foreman_name, supplier, Vehicle, vehicle_plate_no, item1, quantity1, unit1) "
            . "VALUES ('$Date_', '$Time_Travel', '$Time_IN_OUT', '$Driver_Name', '$Project_name', '$Jobsite', '$Foreman_name', '$supplier', '$Vehicle', '$vehicle_plate_no', '$item1', '$quantity1', '$unit1')";

             if ($mysqli->query($sql) === true){
                header("location: viewdelivery.php");
            }
        }   


?>

1 个答案:

答案 0 :(得分:0)

您的问题尚不清楚,但是如果您希望在deliveryformsave.php中存储产品数据数组,则可以执行以下操作:

echo '<option value="'.$row.'">'.$row['product_name'].'</option>';

通过将整个$ row数组作为option标记的值,它将把$ row中的所有属性发送到您的deliveryformsave.php。 在其中,您可以通过将$ row数组的索引设为:

$row['Product_name'];
$row['Product_quantity'];

,依此类推... 希望这会有所帮助。

**更新: 查看您的deliveryformsave.php文件,您可以使用:

在$ id [item1]中使用$ row ['Product_name'] 在$ id [quantity1]中使用$ row ['Product_unit'] 以及您需要存储为值的其他字段。

在打印$ row之前,先查看数组是否包含Product_name和其他项。