duanjia1870 2014-03-14 22:17 采纳率: 0%
浏览 74
已采纳

将单选按钮值插入SQL

Working out a simple star ratings script. Data not inserting into SQL. I am passing a 'id' in the url from the form action to coincide with the rating. I'm using GET to retreive that value. I'm using POST to insert the radio button value. I've tested and the url is passing the id. The script runs to completion. SQL is connecting.

index.html

    <form method="POST" action="rating.php?id=<?php echo $id; ?>">
      <fieldset class="rating">
          <legend>. . .</legend>
              <input type="radio" id="star3" name="starno" value="3" onclick="this.form.submit()"/>
                  <label for="star3" title="Meh">3 stars</label>
              <input type="radio" id="star2" name="starno" value="2" onclick="this.form.submit()"/>
                  <label for="star2" title="Kinda bad">2 stars</label>
              <input type="radio" id="star1" name="starno" value="1" onclick="this.form.submit()"/>
                  <label for="star1" title="Sucks big time">1 star</label>
      </fieldset>
    </form>

ratings.php

    <?php
    isset($_GET['id']);
    $con=mysqli_connect ("","","","");
    mysqli_query($con,"INSERT INTO ratings (storyidr, rank, entry_date) VALUES ('$_GET[id]','$_POST[starno]',now())");
    mysqli_close($con);
    header('Location: http://www.website.com/'); 
    ?>

SQL

    CREATE TABLE IF NOT EXISTS `ratings` 
    (
      `ratingid` int(11) NOT NULL AUTO_INCREMENT,
      `storyidr` varchar(11) NOT NULL,
      `rank` int(11) NOT NULL,
      `entry_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`ratingid`),
    UNIQUE KEY `ratingid` (`ratingid`)
    ) 
    ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='rating for stories' AUTO_INCREMENT=1 
  • 写回答

2条回答 默认 最新

  • douliaodan2738 2014-03-15 01:36
    关注

    You are setting:

    $storyidr = $_POST['id']; 
    

    but it is actually passed in the URL. therefore you need to use:

    $storyidr = $_GET['id'];
    

    The form 'method' does not need to be changed.

    I have looked at the generated sql and it now contains all the input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?