dongquexi1990 2017-01-10 07:27
浏览 88

如何在PHP中从MySQL中的单选按钮插入值

I'm making online voting system. This system has limit for people (population of the area) who can vote (example-10) & then Their votes add to the database. I want to insert values from radio buttons in the database. I tried some code. But it has some error.The vote form data doesn't insert DB & I don't know this is really suitable code for my case. Please If anyone has an idea, please let me know. Thank-you

<html>
    <head>

        <title>Election</title>
        
    <link rel="stylesheet" href="bootstrap-3.3.4-dist/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="css/presidential.css" type="text/css">
        <link rel="stylesheet" href="css/login.css" type="text/css">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        </head>
    <body>
         <div class="head">
    
       <div class="container-fluid">
        <div class="navbar-header">
                   <b> Online voting System </b>
    </div>
        
<input type="submit" class="btn btn-primary nextBtn btn-lg pull-right"  name="submit" value="Cast Your Vote" />
</div>
</div>
             
<div class="container">
<form name="vote" method="POST" action="">
<div class="one">
     <h2><b>Election</b></h2>
     <br>
     
    <div class="funkyradio">
        <div class="funkyradio-default">

            <input type="radio" name="radio" id="radio1" value="radio1" />
            <label for="radio1"><b>A Party</b> <div class="img"><img src="img\A.png" height="75.8em" width="50.3em"  ></img> </div> </label>
        </div>
        <div class="funkyradio-default">
            <input type="radio" name="radio" id="radio2" value="radio2" />
            <label for="radio2"><b>B Party</b><div class="img"><img src="img\B.jpg" height="75.8em" width="50.3em"  ></img> </div></label>
        </div>
        <div class="funkyradio-default">
            <input type="radio" name="radio" id="radio3" />
            <label for="radio3"><b>C Party</b><div class="img"><img src="img\C.jpg" height="75.8em" width="50.3em"  ></img> </div></label>
        </div>
        <div class="funkyradio-default">
            <input type="radio" name="radio" id="radio4" />
            <label for="radio4"><b>D Party</b><div class="img"><img src="img\D.jpg" height="75.8em" width="50.3em"  ></img>  </div></label>
        </div>
        <div class="funkyradio-default">
            <input type="radio" name="radio" id="radio5" />
            <label for="radio5"><b>E Party</b><div class="img"><img src="img\E.jpg" height="75.8em" width="50.3em"  ></img>   </div></label>
        </div>
        <div class="funkyradio-default">
            <input type="radio" name="radio" id="radio6" />
            <label for="radio6"><b> F Party</b><div class="img"><img src="img\F.jpg" height="75.8em" width="50.3em"  ></img>   </div></label>
        </div>
    </div>
</div>
</form>
</div>
</body>
</html>

<?php
require'database.php';
if (isset($_POST['submit']))
{$radio=$_POST['radio'];
     if($radio!="")
     {
     $query=mysql_query("INSERT INTO `election`(`candidate_name`) VALUES ('$radio')");
        if ($query)
        {
            echo"Cast your vote successfully";
        }
         else
         {
             echo"There is a Some Problem in database";
         }
     }
     
}
?>

</div>
  • 写回答

2条回答 默认 最新

  • dongzhong2018 2017-01-10 07:37
    关注

    Your query won't work because you call your variable in a string. You should close your string first, then call your variable and then end the query in a string again.

    Something like this

    VALUES ('" . $radio . "');"
    

    The single quotes are for in the query, then you close your query and put some php code in, and then you open the string again with the double quoutes. In that string you finish the query.

    Look into sql injection, because you're vulnerable if you use this method. But that is something for another question.

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统