Jump to content

Php mySQL - insert as many same named record to database as many category (check


lasha

Recommended Posts

Hello,

 

I have problem and i dont know how to solve it... may be you can help me :)

 

Okey i have form which include one textfield named "name" in which user must enter his/her name and 5 category checkbox cat1, cat2, cat3, cat4, cat5,.

 

now I want, how many categories (checkbox) are selected so many same name records to be inserted in my database...

 

Sorry for my english, i think i explained everithing normally...

 

Thank you... !

 

With respect, Lasha

post-136069-13482403740417_thumb.jpg

In your form, name the checkboxes "category[]". Those that are checked by the user will be POSTed.

 

To process

foreach ($_POST['category') as $category)
{
    // insert in to table
}

 

Instead of storing the user names and category names as your example shows, you should store the user and category ids in that table

Heeey :D

 

Thank you for reply... I asked for this in many forum and only you answered :) i'm very pleased :)

 

Okey, I'm new with php, i wrote this code and dont know how to realise plz help me with this too...

 

here is a code:

 

<?php 
if (isset($name) && isset($cat1) && isset($cat2) && isset($cat3) && isset($cat4) && isset($cat5))
{
$result = mysql_query ("INSERT INTO info (name,cat) VALUES ('$name','$cat')");

if ($result == 'true') {echo "<p class='close'>information added... <a href=" . "javascript:CloseWindow()" . ">Close This Window</a></p>";}
else {echo "<p>added! :|</p>";}
}		 
else 
{
echo "<p>Enter more information...</p>";
}

?>

 

i don't know... ma be i am going on wrong way...

First, have a "category" table.

cat_id  |  cat_name

--------|--------------

  1    |  Category 1

  2    |  Category 2

  3    |  Category 3

  4    |  Category 4

  5    |  Category 5

 

And this is your basic code

 

form.php

<?php
    // connect to your db here
    $sql = "SELECT cat_id, cat_name FROM category";
    $res = mysql_query($sql) or die(mysql_error());
    
    $checkboxes = '';
    while (list($id, $cat) = mysql_fetch_row($res)) {
        $checkboxes .= "<input type='checkbox' name='category[]' value='$id' /> $cat<br />";
    }
?>

<form method = 'post' action = 'process.php' >
    Name: <input type='text' name='name' size='50' /><br />
    Categories:<br />
    <?php echo $checkboxes ?>
    <input type='submit' name='btnSubmit' value='Submit' />
</form>

 

process.php (you will need to add validation of data etc)

<?php
    // connect to db here
    if (isset($_POST['category']) && !empty($_POST['name'])) {
        
        foreach ($_POST['category'] as $cat) {
            $values[]  = sprintf ("(%d, '%s')", intval($cat), mysql_real_escape_string($_POST['name']));
        }
        $sql = "INSERT INTO info (cat, name) VALUES " . join(',', $values);
        mysql_query($sql); // insert the rows
    }
    header("location: form.php");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.