如何在数据库中的RATING TABLE中获取并插入特定用户(卖方)的当前ID

时间:2017-09-30 21:24:17

标签: php jquery mysql

我想获取当前卖家的ID并插入名为“rating”的特定数据库表而不输入任何值,我想自动获取当前卖家的ID(我不是指SESSION'S)< / p>

我的数据库结构

|rating_id|seller_id|rate|customer_id|
--------------------------------------
|         |         |    |           |
|         |         |    |           |
--------------------------------------

当客户对卖家进行评级时,db中的结果为:

|rating_id|seller_id|rate|customer_id|
--------------------------------------
| 1       |   0     | 4  |   29      |
|         |         |    |           |
--------------------------------------

seller_id的值返回0,这不是我想要的。

我的 HTML代码:

    <fieldset id="seller_rate" class="rating">
    <input type="hidden" name="seller_id" id="<?php echo $_GET['seller_id']; ?>" value="<?php echo $_GET['seller_id']; ?>"/>
    <input class="stars" type="radio" id="star5" name="rating" value="5" />
    <label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input class="stars" type="radio" id="star4half" name="rating" value="4.5" />
    <label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
    <input class="stars" type="radio" id="star4" name="rating" value="4" />
    <label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input class="stars" type="radio" id="star3half" name="rating" value="3.5" />
   <label class="half" for="star3half" title="Meh - 3.5 stars"></label>
   <input class="stars" type="radio" id="star3" name="rating" value="3" />
   <label class = "full" for="star3" title="Meh - 3 stars"></label>
   <input class="stars" type="radio" id="star2half" name="rating" value="2.5" />
   <label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
   <input class="stars" type="radio" id="star2" name="rating" value="2" />
   <label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
   <input class="stars" type="radio" id="star1half" name="rating" value="1.5" />
   <label class="half" for="star1half" title="Meh - 1.5 stars"></label>
   <input class="stars" type="radio" id="star1" name="rating" value="1" />
   <label class = "full" for="star1" title="Sucks big time - 1 star"></label>
   <input class="stars" type="radio" id="starhalf" name="rating" value="0.5" />
   <label class="half" for="starhalf" title="Sucks big time - 0.5 stars">
   </label>
   </fieldset>

我的 JQuery代码:

<script>
    $(document).ready(function () {
      $("#seller_rate .stars").click(function () {            
        $.post('db_rating.php',{rate:$(this).val()},function(d){
         if(d>0)
            {
             alert('You have already rated');
            }else{
              alert('Thanks For Rating');
             }
           });
         $(this).attr("checked");
      });
   });

这是我的 PHP代码

<?php
session_start();

//$seller_id = $_POST['seller_id'];
$customer_id = $_SESSION['customer_id'];
$servername = "localhost"; 
$username = "root";
$password = "";
$dbname = "odsy";


$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Unable to connect Server: " . $conn->connect_error);
}

if (isset($_POST['rate']) && !empty($_POST['rate'])) {

    $rate = $conn->real_escape_string($_POST['rate']);
    //$seller_id = $conn->real_escape_string($_POST['seller_id']);
// check if user has already rated
    $sql = "SELECT `rating_id` FROM `rating` WHERE `customer_id`='" . $customer_id. "'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    if ($result->num_rows > 0) {
        echo $row['rating_id'];
    } else {

        $sql = "INSERT INTO `rating` (`seller_id`, `rate`, `customer_id`) VALUES ('" . $seller_id . "', '" . $rate . "', '" . $customer_id . "'); ";
        if (mysqli_query($conn, $sql)) {
            echo "0";
        }
    }
}
$conn->close();
?>

0 个答案:

没有答案