用php模式表单添加数据mySql

时间:2017-03-15 04:08:05

标签: javascript php mysql pdo bootstrap-modal

如何解决这个问题,我试图用pdo方法从模态形式向mysql数据库插入数据,但那里有些不对劲,我无法找到,请帮助....

这是php代码

<?php

    $servername = "localhost";
        $username ="root";
        $password = "";
        $dbname ="la_review";
        $cardTable = "product";

        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if (isset($_POST['addProductRev'])) {
        echo "<script type='text/javascript'>
        $(document).ready(function(){
        $('#view-modal').modal('show');
        });
        </script>";
    }


    if (isset($_POST['addSubmit'])) {
        $addNamaP = $_POST['namaP'];
        $addHargaP = $_POST['hargaP'];
        $addImageP = $_POST['imageP'];
        $addPocessorP = $_POST['processorP'];
        $addUkuranResolusiP = $_POST['ukuranResolusiP
        '];
        $addKapasitasP = $_POST['kapasitasP'];
        $addKategoriP = $_POST['kategoriP'];
        $addDeskripsiP = $_POST['deskripsiP'];  
        $addInterfaceP = $_POST['interfaceP'];  

        $addStmt = $conn->query("INSERT INTO product(name, image, price, processor, ukuranResolusi, kapasitasPenyimpanan, interface, kategori, description) VALUES ('$addNamaP','$addImageP','$addHargaP','$processorP','$addUkuranResolusiP','$addKapasitasP','$addInterfaceP','$addKategoriP','$addDeskripsiP')");

    }
?>

这是我的模态代码

<div id="view-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog"> 
     <div class="modal-content">  

        <div class="modal-header"> 
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
           <h4 class="modal-title">
           <i class="glyphicon glyphicon-plus-sign"></i> Add Product Review
           </h4> 
        </div> 

        <div class="modal-body">                     
           <div id="modal-loader" style=" text-align: center; margin: 20px;">
                <form class="row form-signin" action="index.php" method="post">
                    <div class="form-group">
                        <input class="form-control"  type="text" name="namaP" class="form-control" placeholder="Nama Produk" required>
                    </div>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="hargaP" class="form-control" placeholder="Harga Produk" required>
                    </div>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="kategoriP" class="form-control" placeholder="kategori Produk" required>
                    </div>
                    <div class="form-group">
                        <label style="float: left;" for="exampleTextarea">Deskripsi Produk</label>
                        <textarea name="deskripsiP" class="form-control" id="exampleTextarea" rows="3"></textarea>
                    </div>
                    <h4 style="font-weight: bold;">Tabel Spesifikasi</h4>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="processorP" class="form-control" placeholder="Processor">
                    </div>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="ukuranResolusiP" class="form-control" placeholder="Ukuran Produk & Resolusi Layar">
                    </div>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="kapasitasP" class="form-control" placeholder="Kapasitas Penyimpanan">
                    </div>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="interfaceP" class="form-control" placeholder="Interface Produk">
                    </div>
                    <div class="form-group">
                        <input class="form-control"  type="text" name="namaP" class="form-control" placeholder="Nama Produk" required>
                    </div>
                    <div class="form-group">
                        <label style="float: left;" for="exampleInputFile">Image</label>
                        <input type="file" name="imageP" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
                        <small id="fileHelp" class="form-text text-muted">Format .png only</small>
                      </div>
                     <button class="btn btn-info " type="button" value="submit" name="addSubmit">Submit Produk</button></a>
                </form>
            </div> 

        <div class="modal-footer"> 
            <div class="row">
                <div col-md-4>
                    <?php 
                        if ($addStmt) {
                            echo "success";               
                        }else{
                            echo "fail";
                     }
                    ?>
                </div>
            </div> 
        </div> 

    </div> 
  </div>
</div>

<script type='text/javascript'>
                    $(document).ready(function(){
                        $('#view-modal').modal('hide');
                    });
</script>

1 个答案:

答案 0 :(得分:0)

尝试在表单中将按钮的类型设置为submit,如果设置button值,则需要在jquery中实现函数onclick并将ajax post请求发送到后端php函数。只有button类型的按钮才会提交您的表单。愿这有用。