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.

    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备