Php 如何在传递URL GET vs POST中的值的表单中使用单选按钮

Php 如何在传递URL GET vs POST中的值的表单中使用单选按钮,php,sql,forms,radio,Php,Sql,Forms,Radio,制定一个简单的明星评级脚本。数据未插入SQL。我从表单操作中传递url中的“id”,以与评级一致。我正在使用GET检索该值。我正在使用POST插入单选按钮值。我已经测试过了,url正在传递id。脚本运行到完成。SQL正在连接 index.html <form method="POST" action="rating.php?id=<?php echo $id; ?>"> <fieldset class="rating">

制定一个简单的明星评级脚本。数据未插入SQL。我从表单操作中传递url中的“id”,以与评级一致。我正在使用GET检索该值。我正在使用POST插入单选按钮值。我已经测试过了,url正在传递id。脚本运行到完成。SQL正在连接

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>
错误报告


([受影响的行]=>[客户端信息]=>[客户端版本]=>50532[连接错误]=>0[连接错误]=>[错误]=>[错误列表]=>[字段计数]=>[主机信息]=>[插入id]=>[服务器信息]=>[服务器版本]=>[状态]=>[协议版本]=>[线程id]=>[警告计数]=>)插入评级中(storyidr,rank,entry_date)值(“198”,“3”,now())

如果您连接到DB,其中有评级表,表存在,php工作正常,id正在传递,mysqli_connect已连接,则可能在值之前有一个空格,如下所示:

 $sql .= ' VALUES("'.$storyidr.'","'.$star.'",now())';
我知道你现在的代码是无效的

INSERT INTO ratings (storyidr,rank,entry_date)VALUES("'.$storyidr.'","'.$star.'",now())
///编辑以添加一些安全性;)

始终使用PDO处理MySQL:

try {
$pdo = new PDO('mysql:host=local;dbname=db','user','pass');
$pdo->setAsttribute(PDO::ERRMODE_EXCEPTION);
$sql = 'INSERT INTO ratings SET name = :name, surname = :surname';
$oo_db = $pdo->prepare($sql);
$oo_db->bindValue(':surname', $surname);
$oo_db->execute();
  }
catch (PDOException $e) { $error = "fail"; }

更多信息请访问->

危险:您很容易受到您需要保护的信息的攻击。为什么不呢?添加了错误测试,但没有提供信息。SQL正在连接,数据只是不存在。您不执行查询。需要执行。谢谢秋葵
INSERT INTO ratings (storyidr,rank,entry_date)VALUES("'.$storyidr.'","'.$star.'",now())
try {
$pdo = new PDO('mysql:host=local;dbname=db','user','pass');
$pdo->setAsttribute(PDO::ERRMODE_EXCEPTION);
$sql = 'INSERT INTO ratings SET name = :name, surname = :surname';
$oo_db = $pdo->prepare($sql);
$oo_db->bindValue(':surname', $surname);
$oo_db->execute();
  }
catch (PDOException $e) { $error = "fail"; }