【问题标题】:Cannot store radio button value in table无法将单选按钮值存储在表中
【发布时间】:2013-01-02 18:40:36
【问题描述】:

我正在尝试将单选按钮的值保存在我的数据库表 choice 中。我收到消息Data saved successfully,但表中没有存储任何值。

表格:

   <form id="myForm" method="post" action="">
<div  data-role="fieldcontain">
                <fieldset data-role="controlgroup">
                    <center<legend>Choose in which category you'd like to be included</legend></center>
                    <p><input type="radio" name="choice[]" value="player" id="player" class="custom" />
                    <label for="player">Player</label>
                    <input type="radio" name="choice[]" value="coach" id="coach" class="custom" />
                    <label for="coach">Coach</label>
                    <input type="radio" name="choice[]" value="supporter" id="supporter" class="custom" />
                    <label for="supporter">Supporter</label>
                    <input type="radio" name="choice[]" value="sponsor" id="sponsor" class="custom" />
                    <label for="sponsor">Sponsor</label>
                    <input type="radio" name="choice[]" value="alumni" id="alumni" class="custom" />
                    <label for="alumni">Alumni</label>
                    <input type="radio" name="choice[]" value="other" id="o" class="custom" />
                    <label for="o">Other</label>

                </fieldset>
            </div>
<div data-role="fieldcontain">
    <label for="name">Please enter your name:</label>
    <input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
    <label for="email">Please enter your e-mail:</label>
    <input type="text" name="email" id="email" value="" class="required" autocomplete="off"  /><br />
    <label for="phone">Please enter your phone number:</label>
    <input type="number" name="phone" id="phone" value="" class="required" autocomplete="off"  />

<br><br>    
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
</body>
</html>

PHP:

<?php 
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$choice = $mysqli->real_escape_string($_POST['choice']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`,`choice`) VALUES ('".$name."','".$email."','".$phone."','".$other."','".$choice."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>

【问题讨论】:

  • 什么是mysql数据类型可供选择?另外,请在您的文件中回显 $query,并让我们知道打印到屏幕上的结果。有助于了解选择的价值(如果有的话)。显然,为执行该部分获得了正确的 save 值。
  • 我在 $query 上做了一个回显,它没有显示任何选择的价值。
  • 这里发生了其他事情,或者脚本中其他地方有更多代码正在覆盖或产生影响。我在我的服务器上运行了简单的代码,没有任何问题。您的表单正在通过邮寄提交。也许提供更多关于表单的代码。尝试在代码中通过 POST 回显选项值,以查看那里的选项值。
  • 请向我们展示您的整个form html 标记。
  • 感谢所有回复。我现在已经添加了整个表单!

标签: php sql forms


【解决方案1】:

var_dump($_POST) 看看哪些数据会涌入。

还要检查它是保存还是提交?在$_POST['save']

编辑

获得完整表格后 - 错误在于您的 center 标签

&lt;center&lt;legend&gt;更改为&lt;center&gt;&lt;legend&gt;

这个标签中的错误 - ↑

【讨论】:

  • 谢谢!我收到这条消息: { ["name"]=> string(1) "q" ["email"]=> string(1) "q" ["phone"]=> string(1) "q" ["其他"]=> string(1) "q" ["save"]=> string(11) "提交表单" }
  • 似乎没有选择的价值。我一无所知
  • @PAHemingstam 能否请您添加更多 html 表单数据。检查所有 id 是否都是唯一的,是的,它们也有值
  • @PAHemingstam 得到了解决方案..如果您早点粘贴它,您将不必等待这么长时间:) 在我的回答中添加了这个
  • 不幸的是,这不是解决方案:(。除了“选择”的值之外,所有值都被保存
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 2013-11-08
  • 2016-04-17
相关资源
最近更新 更多