【问题标题】:How to get input value and insert it into database如何获取输入值并将其插入数据库
【发布时间】:2019-03-05 03:50:42
【问题描述】:

我已经手动将一些星星值插入到数据库中,我通过此代码获取并显示相关产品,现在我想在用户点击相关产品的星星时将新的星星值插入到数据库中

我使用php作为服务器端语言和mysql数据库

 <?php
    include("sessiondestroy.php");
    include("functions.php");
    $ip = getRealIPAddr();
    include("header.php");
    include("leftside.php"); 
    ?>

                <td width="53%" style="align-left:15px;" valign="top" >
                <div class="row" align="center" >

                <?php 
                $con = mysqli_connect('localhost','root',"",'furniture_store');
                mysqli_select_db($con,'furniture_store');
                $query="select product_id, product_name, s_price, product_pick, rating from product";
                $run=mysqli_query($con,$query);
                while($row=mysqli_fetch_array($run)){
                    $id=$row['product_id'];
                    $name=$row['product_name'];
                    $image=$row['product_pick'];
                    $rating=$row['rating'];
                ?>

                <div class="col-md-3" align="center" style="border:#CCCCCC solid 1px; margin:15px; padding:8; ">
                <div class="container fill">
                <div class="modal-body">
                <form action="index.php" method = "post">
                <table>
                <tr align="centr">
                <td align="center"><?php echo $name; ?></td>
                </tr>
                <tr>
                <td> &nbsp </td>
                </tr>
                <tr>
                <td ><img width="100px" height="140px"  src="admin/AdminLTE/<?php echo $image; ?>"></td>
                </tr>
                <tr>
                <td> &nbsp </td>
                </tr>
                <tr align="left">
                <td style="padding-right:15px;"><input id="input-1" name="input-1" class="rating rating-loading" value="<?php echo $rating; ?>" data-min="0" data-max="5" data-step="0.1" data-size="" data-show-clear="false" data-show-caption="false"></td>
                </tr>
                <tr>
                <td> &nbsp </td>
                </tr>
                <tr>
                <td align="center"><a href="userside_productdetail.php?id=<?php echo $id; ?>"><input type="submit" name="detail" class="btn btn-success" value="Details"></a></td>
                </tr>
                </table>
                </form>
                </div>
                </div>
                </div>
                <?php } ?>
                </div>
                </td>

    <?php include("rightside.php"); ?>
    <?php include("footer.php"); ?>
    <script>
    $("#input-id").rating();
    </script>
    </body>
    </html>

【问题讨论】:

  • 使用ajax
  • 使用 Javascript 来动态更新类似这样的值 => document.getElementById('input-id').value
  • 这是一个重复的问题。同一个话题有很多问题。从 UI 获取数据并保存在数据库中。

标签: javascript php ajax twitter-bootstrap


【解决方案1】:

@yawar 您可以使用 jquery $.post 方法将您的评分值保存到数据库中,这很简单。但问题是你没有提到你正在使用哪个数据库,你正在使用什么服务器端语言,甚至没有提到评级参考。这个评级是针对用户、产品还是评论还是什么......

我们开始: 虽然这是获得结果的非常基本的示例,但您需要做更多的工作。给定的示例代码欢迎 XSS 和 SQL 注入攻击。这部分我不能在这里介绍,因为它需要很好地理解您的所有代码并将其应用到需要的地方,这可能需要对您的整个代码进行大量更改。我希望您将此示例代码仅视为示例,而不是解决方案。

第 1 步: 改变你的

<td style="padding-right:15px;"><input id="input-1" name="input-1" class="rating rating-loading" value="<?php echo $rating; ?>" data-min="0" data-max="5" data-step="0.1" data-size="" data-show-clear="false" data-show-caption="false"></td>

<td style="padding-right:15px;"><input id="input-1" name="input-1" class="rating rating-loading" value="<?php echo $rating; ?>" data-min="0" data-max="5" data-step="0.1" data-size="" data-show-clear="false" data-show-caption="false" data-product-id="<?php echo $id;?>"></td>

第 2 步: 改变

<script>
    $("#input-id").rating();
</script>

<script>
    $("#input-id").rating();

    $(document).on('change','.rating',function(){
        var prating = $(this).val();
        var product_id = $(this).data('product-id');
        $.post("updRating.php",{prating: prating, product_id: product_id})
        .done(function( data ) {
            if(data == 'Success'){
                alert( "Thank you for give your rating..." );
            }else{
                alert( "Opps!!! something went wrong, please try again" );
            }
        });
    })

</script>

第 3 步: 为产品的更新评级创建一个 PHP 文件。例如。 updRating.php 为

<?php 
    $con = mysqli_connect('localhost','root',"",'furniture_store');
    mysqli_select_db($con,'furniture_store');
    $rating = $_POST['prating'];
    $product_id = (int)$_POST['product_id'];
    $query = "update product set rating = '".$rating."' where product_id = ".$product_id;
    if(mysqli_query($con,$query)){
        echo 'Success';
    }else{
        echo 'Error';
    }

?>

【讨论】:

  • 先生,感谢您对我的问题感兴趣,实际上我是一个新开发人员,我对 ajax 和 larval 了解不多,我使用 php 作为服务器端语言并使用 mysql 作为数据库。此评级是针对产品先生而不是用户和 cmets
  • @yawar 明天我会给你一些示例代码,对不起,今天我有假期的心情:)。我一定会给你一些很好的示例代码来帮助你前进。
  • @yawar 我已经用示例代码更新了我的答案,希望这个示例能给你一个提示,让你朝着正确的方向前进。
猜你喜欢
  • 2017-12-06
  • 2023-04-02
  • 2019-10-26
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2023-04-10
相关资源
最近更新 更多