0

I need a little help here with grabbing data from radio buttons selected by the user and entering the info to a table in my database.

My purpose is trying to create a dynamic website that grabs new tables entered in with data on it and posting it on the home page automatically. It will be a type of bidding/auction type of website.

First problem is I'm trying to get it to check if there are errors (entered in purposely or not purposely by the user)

If possible can the info below be entered into an array for storage as well? (not sure if I'm asking this right)

<form action="" method="post">
<ul>
<li>
Assault Ships:<br>
<input type="radio" name="item_ship" value="item_cruiser">Cruiser<br>
<input type="radio" name="item_ship" value="item_carrier">Carrier<br>
</li>
<li>
Pick a ticket:<br>
<input type="radio" name="item_tickets" value="ticket_one">One<br>
<input type="radio" name="item_tickets" value="ticket_two">Two<br>
<input type="radio" name="item_tickets" value="ticket_three">Three<br>
<input type="radio" name="item_tickets" value="ticket_four">Four<br>
<input type="radio" name="item_tickets" value="ticket_five">Five<br>
<input type="radio" name="item_tickets" value="ticket_six">Six<br>
<input type="radio" name="item_tickets" value="ticket_seven">Seven<br>
<input type="radio" name="item_tickets" value="ticket_eight">Eight<br>
</li>
<li>
<input type="submit" value="List Item">
</li>
</ul>
</form>

So what will happen when finished, is the user will select what item they want to list, and then choose what ticket they would like to purchase, this data is entered into my db_table and then my other script will display it on the home page where other people will buy the remaining tickets.

If I'm missing anything, please ask... kinda new to this. Looking forward to some great minds on these boards!!

1
  • Just unsure the syntax of radio buttons... what actually gets posted? I've recently done register pages where the user inputs data into a text field and then my script enters the data into my database... i'm not sure how to write the syntax for radio buttons. Commented Apr 3, 2013 at 4:22

1 Answer 1

1

Here is an example considering you have a radio button with a name attribute of radio_name -

HTML:

<input type="radio" checked="checked" name="radio_name" />

Get POST value:

if(isset($_POST['radio_name']) && !empty($_POST['radio_name'])){
    $radioContent= $_POST['radio_name'];
}

Insert into database (using mysqli prepared statements):

$sql = "INSERT INTO yourTable(
        rowName
        ) 
        VALUES (?)";

if($stmt = $mysqli->prepare($sql)){
    $stmt->bind_param("i", $radioContent);
    $stmt->execute();
    $stmt->close();
}
else{
    error_log($mysqli->error);
}

Resources to use:

Insert into (MYSQL)

Radio button POST

Sign up to request clarification or add additional context in comments.

5 Comments

mysqli not mysql. And no, you can modify this so it works with any method you're using. All you need is the SQL.
@user1707535 I need to get to bed, but this example is working as I modified it from a script I already have setup on my site. So, the only thing you would need to change is the SQL to make it match your database.
@user1707535 great, and i updated the answer with some resources.
Anybody else know if radio buttons can have multiple names/values? For example, if the user selects say, "carrier" and then submits his ticket, is it possible for it to post an array of data about that carrier into my table? my table consists of: id, item_name, item_desc, item_cost, item_bet
@user1707535 yes, they can have similar names considering that only one radio button can be clicked per name set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.