Jump to content

[SOLVED] Submitting values from radio button to mysql database


Recommended Posts

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

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');

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

 

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');

 

 

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

 

 

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.

 

 

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

 

 

 

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

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.

 

 

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.

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.

 

 

 

 

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)";

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.