unknown1 Posted September 30, 2009 Share Posted September 30, 2009 Hello, I was hoping to get some help if possible. I am trying to submit information from a radio button to a mysql database but keep getting an error Column count doesn't match value count at row 1 <p> <input name="listingtype" type="radio" value="1" />some value <input name="listingtype" type="radio" value="2" />Another value </p> <?php $listing_type = $_POST['listingtype']; $list_type['1']= false; $list_type['2']= false; $list_type[$_POST['listingtype']]= true; ?> $sql = "INSERT INTO table (id_ListType) VALUES ('$listing_type')"; It's a much bigger query but I added the basics... If anyone can help me out it sure would be appreciated, thanks. I know it's not a problem with the columns in database... cause if i use something like $sql = "INSERT INTO table (id_ListType) VALUES ('1')"; it works fine Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/ Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 The error message seems to indicate that the purple bit has a different amount of entries to the orange bit. Obviously in your example code that's not true, but is it in your more complicated query? $sql = "INSERT INTO table (id_ListType) VALUES ('$listing_type'); Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927928 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 The error message seems to indicate that the purple bit has a different amount of entries to the orange bit. Obviously in your example code that's not true, but is it in your more complicated query? $sql = "INSERT INTO table (id_ListType) VALUES ('$listing_type'); I know it's not a problem with the columns in database... cause if i use something like $sql = "INSERT INTO table (id_ListType) VALUES ('1')"; it works fine Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927930 Share on other sites More sharing options...
eatfishy Posted September 30, 2009 Share Posted September 30, 2009 Try the below statement. $sql = "INSERT INTO `table` (id_ListType) VALUES (' {$listing_type} ')"; Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927931 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 I know it's not a problem with the columns in database... cause if i use something like $sql = "INSERT INTO table (id_ListType) VALUES ('1')"; it works fine At what point did I suggest it was a problem with anything todo with the database? This is the type of code that would normally cause that error message... "INSERT INTO table (id_ListType, another_field) VALUES ('$listing_type'); or "INSERT INTO table (id_ListType) VALUES ('$listing_type', '$another_field'); Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927936 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 Try the below statement. $sql = "INSERT INTO `table` (id_ListType) VALUES (' {$listing_type} ')"; Yeah that seems to work fine... the error is gone but we have another issue. When it submits to the database the value is 0 and it should be either 1 or 2 Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927937 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 I know it's not a problem with the columns in database... cause if i use something like $sql = "INSERT INTO table (id_ListType) VALUES ('1')"; it works fine At what point did I suggest it was a problem with anything todo with the database? This is the type of code that would normally cause that error message... "INSERT INTO table (id_ListType, another_field) VALUES ('$listing_type'); or "INSERT INTO table (id_ListType) VALUES ('$listing_type', '$another_field'); I assume the original error is because I have two fields with the same name and when it's submitted... it's trying to read both values. <p> <input name="listingtype" type="radio" value="1" />some text <input name="listingtype" type="radio" value="2" />More text </p> and the database only has one column for this value. That's why I tried something like <?php $listing_type = $_POST['listingtype']; $list_type['1']= false; $list_type['2']= false; $list_type[$_POST['listingtype']]= true; ?> I assume that $list_type[$_POST['listingtype']]= true; is not working for some reason cause now the value submitted is 0 and not 1 or 2 like it should be. Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927940 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 Is the logic behind what I'm doing even correct?? Must be another way to do this? Seems simple, I have a column in the database named id_listType and this value should be either 1 or 2. so I set the radio buttons <p> <input name="listingtype" type="radio" value="1" /> text <input name="listingtype" type="radio" value="2" /> text </p> the I want to submit listingtype to the database as either 1 or 2 Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927948 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 column = row Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927956 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 From what I remember, if you have multiple radio inputs on the page with the same name only one of them can be selected at once. So yes the example you have their is fine. <?php // this as your form <input name="listingtype" type="radio" value="1" /> text <input name="listingtype" type="radio" value="2" /> text // this is the value to insert into the database if(isset($_POST['listingtype'])) { $listingtype = $_POST['listingtype']; } else { // set a default value $listingtype = 1; // you could instead throw an error and make the user fill it in, // or whatever } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927961 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 From what I remember, if you have multiple radio inputs on the page with the same name only one of them can be selected at once. So yes the example you have their is fine. <?php // this as your form <input name="listingtype" type="radio" value="1" /> text <input name="listingtype" type="radio" value="2" /> text // this is the value to insert into the database if(isset($_POST['listingtype'])) { $listingtype = $_POST['listingtype']; } else { // set a default value $listingtype = 1; // you could instead throw an error and make the user fill it in, // or whatever } ?> When I try it this way the value submitted is 0 and it should be either one or two. Really odd cause if anything you would expect 1 as the value submitted. Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927974 Share on other sites More sharing options...
eatfishy Posted September 30, 2009 Share Posted September 30, 2009 do u have <form name="test" method="post" /> in your code somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927988 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 do u have <form name="test" method="post" /> in your code somewhere? Yes Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-927992 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 Do you have an input on the form with the name listingtype, other than the two listed in the exampe? A value of 0 should be impossible, as even if neither is selected I set a default value of 1. Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-928010 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 Those are the only ones with a name of listingtype in the script... <?php // this as your form <input name="listingtype" type="radio" value="1" /> text <input name="listingtype" type="radio" value="2" /> text // this is the value to insert into the database if(isset($_POST['listingtype'])) { $listingtype = $_POST['listingtype']; } else { // set a default value $listingtype = 1; // you could instead throw an error and make the user fill it in, // or whatever } $sql = "INSERT INTO table (id_ListType) VALUES ('$listingtype'); ?> If I use the above the value submitted to the database is 0 I tried $sql = "INSERT INTO table (id_ListType) VALUES ('{$listingtype}'); and still 0 Also tried $sql = "INSERT INTO table (id_ListType) VALUES ("$listingtype"); I get the error Column count doesn't match value count at row 1 once again. I have no other fields in the form with the name listingtype... just the two radio buttons. Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-928020 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 I suggest you echo out the value of $listingtype before running the query so you see what is being sent. It should be impossible for the value to be anything other than 1 or 2. What is the datatype of the column id_ListType? If the value '1', or '2' is considered an invalid entry for that datatype it will set the column to the default value of that column. If the column is of type INT, the query should really be... $sql = "INSERT INTO table (id_ListType) VALUES ($listingtype)"; Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-928037 Share on other sites More sharing options...
unknown1 Posted September 30, 2009 Author Share Posted September 30, 2009 I suggest you echo out the value of $listingtype before running the query so you see what is being sent. It should be impossible for the value to be anything other than 1 or 2. What is the datatype of the column id_ListType? If the value '1', or '2' is considered an invalid entry for that datatype it will set the column to the default value of that column. If the column is of type INT, the query should really be... $sql = "INSERT INTO table (id_ListType) VALUES ($listingtype)"; The echo value is one or two but still submits 0 to the database Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-928047 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 And with regards to the rest of my suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-928049 Share on other sites More sharing options...
unknown1 Posted October 1, 2009 Author Share Posted October 1, 2009 And with regards to the rest of my suggestion? $sql = "INSERT INTO table (id_ListType) VALUES ($listingtype)"; was the solution =) Thank you very much!! Quote Link to comment https://forums.phpfreaks.com/topic/176100-solved-submitting-values-from-radio-button-to-mysql-database/#findComment-928052 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.