使用php将单选按钮选择发布到网站上的数据库中

时间:2016-08-31 04:57:22

标签: php forms radio-button

我搜索了大约50个网站,提到向数据库发布单选按钮。我想做的事情我认为很简单。我想让用户在整个NFL赛季中为每场比赛挑选一名获胜者。我已经搜索了几天的堆栈溢出,并搜索了我正在寻找的几种变体。 W3Schools这次没有任何帮助,因为他们的帖子没有显示如何将它连接到数据库。至少没有一个工作,我在网上找到的大多数代码要么不完整,要么不起作用。

landingpage.php
<?php
ob_start();
session_start();
require_once 'dbconnect.php';

// select loggedin users detail
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $userRow['userName']; ?>@mywebsite</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type=
"text/css"  />
<link rel="stylesheet" href="home.css" type="text/css" />
    <script src="assets/jquery-1.11.3-jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
</head>
<body>
    <!-- Navbar Beginning -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed"  
data-toggle="collapse" data-target="#navbar" aria-expanded="false" 
aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="mywebsite">mywebsite</a>
        </div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="mywebsite">Message boards</a>
</li>&nbsp;
<li class="active"><a href="SBPicks.html">Superbowl Picks</a>
</li>
            </ul>

        <ul class="nav navbar-nav navbar-right">

            <li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" 
aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-user"></span>&nbsp;Hello  <?php echo 
$userRow['userName']; ?>&nbsp;<span class="caret"></span></a>
              <ul class="dropdown-menu">
<li><a href="logout.php?logout"><span class="glyphicon glyphicon-
log-out"></span>&nbsp;Sign Out</a></li>
              </ul>
            </li>
          </ul>
        </div><!--/ navbar collapse -->
      </div>
    </nav> 

    <div class="picks"
        <br><br>
        <br><br><br><br>
        <form action="weekly.php" method="post" enctype="text/plain">
            Game 1 <br> 
<input type="radio" name="game1" <?php if (isset($game1) && 

$game1=="Panthers") echo "checked";?> value="Panthers">Panthers
@ <input type="radio" name="game1" <?php if (isset($game1) && 
$game1=="Broncos") echo "checked";?> value="Broncos">Broncos
            <span class="error">* <?php echo $game1err;?></span>
            <br><br>
            <input type="submit" name="Submit" value="Submit Picks" />
        </form>
    </div>
</body>
</html>


pick.php
    <?php
    ob_start();
    session_start();
    require_once 'dbconnect.php';

    // select loggedin users detail
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
    $userRow=mysql_fetch_array($res);

$user =$_SESSION['user'];
$game1 = $_POST['game1'];

$query = "INSERT INTO fbtest(usersid,game1)Values ('$user','$game1')";

if(mysql_query($query)){
echo "your picks have been submitted";}
else{
echo "fail";}
?>
<br><br>
<a href="home.php">back to landing page</a>

1 个答案:

答案 0 :(得分:0)

你问自己这条线路有问题:

$query = "INSERT INTO fbtest(usersid,game1)Values ('$user','$game1')";

通过将变量直接注入查询,您会产生巨大的漏洞。

What is SQL Injection

How can I prevent SQL Injection?

但让我们回到你的问题。

您的问题是表单的enctype。您提供了 text / plain 不是表单的有效值,使用POST方法提交

根据this错误报告:

  

标记中enctype的有效值为:

     

应用程序/ x-WWW窗体-urlencoded   多部分/格式数据

要以正确的方式处理表单并填充superglobals,表单的enctype必须是 application / x-www-form-urlencoded - 这是默认值 - 或者multipart / form -data - 如果你想上传文件。