在表单上调用两个php文件提交

时间:2015-06-14 13:52:35

标签: php jquery forms

我编写了一个表单,该表单从用户获取2种值,一种用于促销详细信息,另一种用于请求照片(照片是可选的)。我需要将照片上传到一个文件夹中并进行幻灯片播放。我的问题是如何将照片发送到文件夹并将促销详细信息插入数据库,我应该使用哪种幻灯片工具,只需在文件夹中选择随机图像?这是我的代码,用于询问用户输入和照片:

<FORM action="insertPromo.php" method="post">
            <div class="form-group">
                <label class="control-label col-sm-2" for="txtPromoName">Promo Title:</label>
                    <div class="col-sm-10" id="divCmbService"">
                        <input type="text" class="form-control" name="txtPromoName" id="txtPromoName" placeholder="Promo Name">
                    </div>
            </div>

            <div class="form-group">
                <label class="control-label col-sm-2" for="cmbService">Service to discount:</label>
                    <div class="col-sm-10">          
                            <select id="cmbService" name="cmbService" class="form-control" onchange="showService(this.value)">
                                    <option value="0">- Select One -</option>
                        <?php                                       
                            $dbHost = "localhost";
                            $dbUser = "mdchadmin";
                            $dbPass = "123456";
                            $dbName = "mdch_new";

                            $conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
                                if ($conn->connect_error) {
                                    die("Connection failed: " . $conn->connect_error);
                                } 
                                $sql = "SELECT service_id, service_name FROM services";
                                $result = $conn->query($sql);
                                if ($result->num_rows > 0) {
                                    while($row = $result->fetch_assoc()) {
                                    echo "<option value=\"{$row['service_id']}\">{$row['service_name']}</option>";
                                                }
                                }                                       
                                $conn->close();                                         
                            ?>
                            </select>
                </div>
            </div>


            <div class="form-group">
                <label class="control-label col-sm-2" for="txtPresyo">Input Promo Cost:</label>
                    <div class="col-sm-10" id="divCmbService"">
                        <input type="text" class="form-control" name="txtPresyo" id="txtPresyo" placeholder="0.00">
                    </div>
            </div>


                <div class="form-group">
                      <label class="control-label col-sm-2" for="txtValidFrom">Valid From:</label>
                        <div class="col-sm-10">
                            <input class="form-control login-field" type="date" id="txtValidFrom" name="txtValidFrom" size=17>
                        </div>
                </div>
                <div class="form-group">
                      <label class="control-label col-sm-2" for="txtValidUntil">Valid Until:</label>
                        <div class="col-sm-10">
                            <input class="form-control login-field" type="date" id="txtValidUntil" name="txtValidUntil" size=17>
                        </div>
                </div>
                        <?php echo "<br>";echo "<br>";echo "<br>";echo "<br>";?>
                <div class="form-group">
                <label class="control-label col-sm-2" for="txtServiceName"> Promo photo upload:</label>
                    <div class="col-sm-10" id="divCmbService"">
                        <INPUT NAME="promo" TYPE="file" enctype = "multipart/form-data">                        
                    </div>
            </div>

            <div class="form-group">    
                <div class="col-sm-offset-2 col-sm-10">
                    <INPUT TYPE="submit" class="btn btn-default" VALUE="Add Promo">
                </div>
            </div>  


            </FORM>     

以下是我将数据发送到数据库的文件的代码:

<?php
error_reporting(E_ERROR);
include('session.php');
if (false==$_SESSION['isAdmin']){ 
    header("Location:index.php");
    die();      
}
?>
<html lang="en">

<body>
<div id="wrapper">
            <?php
            include('nav_admin.php');
            ?>
        <div id="page-wrapper"> 
            <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                        <h1 class="page-header">Insert a new Promo</h1>
                    </div>
                    <div class="col-xs-6">
                    <div class="container">
                        <?php           


                                try{
                                            $dbHost = "localhost";
                                            $dbUser = "mdchadmin";
                                            $dbPass = "123456";
                                            $dbName = "mdch_new";

                                            $conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
                                            if ($conn->connect_error) {
                                                die("Connection failed: " . $conn->connect_error);
                                            } 

                                            // Escape user inputs for security
                                            $promoTitle= mysqli_real_escape_string($conn, $_POST['txtPromoName']);
                                            $serviceID = $_POST['cmbService'];
                                            $discountedCost = mysqli_real_escape_string($conn, $_POST['txtPresyo']);
                                                date_default_timezone_set('Asia/Taipei');
                                                $validFrom = $_POST['txtValidFrom'];
                                                $validUntil = $_POST['txtValidUntil'];
                                                $dateFrom = date('Y-m-d',strtotime($validFrom));
                                                $dateUntil= date('Y-m-d',strtotime($validUntil));
                                            //$total_payment = mysqli_real_escape_string($conn, $_POST['txtTotalPayment']);

                                            // attempt insert query execution
                                            $sql = "INSERT INTO PROMOS (PROMO_NAME, SERVICE_ID, DISCOUNTED_PRICE, VALID_FROM, VALID_UNTIL) VALUES ('$promoTitle', $serviceID, $discountedCost, '$dateFrom', '$dateUntil')";
                                            if ($conn->query($sql) === TRUE) {
                                                echo "Promo ";ECHO $promoTitle; echo " created. The promo is valid from "; echo $dateFrom; echo " until "; echo $dateUntil;

                                            } else {
                                                echo "Cannot insert the promo. Please fill up all of the fields.";
                                            }

                                            // close connection
                                            $conn->close();




                                }catch (Exception $e) {
                                            echo 'Error: ' . $e->getMessage();
                                        }

                        ?>  
                                <FORM ENCTYPE="multipart/form-data" action="addPromo.php" method="post">
                                <?php echo "<br>";echo "<br>";echo "<br>";?>                    
                                <div class="form-group">    
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <INPUT TYPE="submit" class="btn btn-default" VALUE="Back to Add Promo Page">
                                    </div>
                                </div>  
                    </div>
                    </form>
                    </div>
                    </div>
                    </div>
            </div>
        </div>
</div>

</body>
</html>

我已经用谷歌搜索了如何上传图片,它对我有用,但我不知道如何同时上传照片并将数据插入数据库。

1 个答案:

答案 0 :(得分:0)

<?php
  include('insertPromo.php');//this writes to the db
  include('upload.php'); //this uploads the photo
?>

然后制作表格动作=&#34; promoIncluder.php&#34;促销包含者包含上面的代码