0

I have been trying to post a value from a radiobutton selection into my database. My datatype in mysql is int. I want value to be 1 or 0. But I believe it takes the value from the radio input type as a string. I need help in posting a value from a radiobutton into my database.

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by wrivera
0
The form

<form>

<input type="radio" value="1" name="ys">

</form>

<?php
$radiobutton=$_POST['ys'];

insert into tblvalues(op)values('{$radiobutton}')
?>
0

If you don't mention form posting method [POST/GET], it will take the default value, ie., GET. So, $_POST will be empty.

Apart from that, It should work.

0

Then it *should* work. Unless you post your complete code, we won't be able to pin-point where exactly the problem is.

0

Are there sample codes where I can use a radiobutton to submit and int value all the ones I have seen have been for string values.

0

:) I used your code to insert radio button value to column age with datatype int.

<html>
<body>
<form method='post'>
<input type="radio" value="1" name="ys">
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
	$con = mysql_connect("localhost","root");
	mysql_select_db("test2");
	$radiobutton=$_POST['ys'];
	mysql_query("insert into a (age) values ('{$radiobutton}')");
}
?>

If this *isn't working*, you should explain what is not working and post errors if you are getting any.

1

:) I used your code to insert radio button value to column age with datatype int.

<html>
<body>
<form method='post'>
<input type="radio" value="1" name="ys">
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
	$con = mysql_connect("localhost","root");
	mysql_select_db("test2");
	$radiobutton=$_POST['ys'];
	mysql_query("insert into a (age) values ('{$radiobutton}')");
}
?>

If this *isn't working*, you should explain what is not working and post errors if you are getting any.

you can try type casting your post
$string = (int) $string; this will ensure that it is an integer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.