简体   繁体   中英

How to Insert Value From Radio Button in MySQL in PHP

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"; } } } ?> 

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.

Here's a little example:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo "<pre>";
    var_dump($_POST);
    exit;
}
?>

<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="radio" name="radiobutton" value="boom" />
    <input type="radio" name="radiobutton" value="bam" />
    <input type="radio" name="radiobuttontoo" value="boom" />
    <button type="submit">Go</button>
</form>

In the above example you can POST two radio buttons. radiobutton and radiobuttontoo . For radiobutton it depends which of the two is selected. The value of the selected one will be submitted. Now, to get this into mysql, simply do an INSERT .

$stmt = "INSERT INTO table SET
value='".$_POST['radiobutton']."'
";

There really is nothing more to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM