Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What I want to do is to add a new record in table RECORD. This new record can be connected with multiple categories from table CATEGORY. Now, I have made the following code so I can select all categories that need to be connected with the records. However, I don’t know how I can post the selected categories into the RECORD table. So for example, if you have 5 categories in the CATEGORY table, named ‘one’, ‘two’, ‘three’, ‘four’ and ‘five’ (ID 1, 2, 3, 4, 5) and you select ‘one’ and ‘three’ to be added to the new record, it should be posted in the RECORD table into the column category_id with their ID ‘1, 3.

I already have a column ‘category_id’ in the table RECORD, but it does not add anything. I have the following code. Really hope someone can help me, I would appreciate it big time. I've searched quite a few questions but I still can't solve it :( Many thanks in advance!

record.php

<form method="post" action="<?php echo $home; ?>add_form.php">

        <div class="alert alert-info">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <strong><i class="icon-user icon-large"></i>&nbsp;Test</strong>&nbsp;
        </div>

    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>
            <td width="100">Name</td>
            <td><input name="name" type="text" id="name"></td>
        </tr>
    </table>
<br />
    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>

            <table cellpadding="0" cellspacing="0" border="0">
                    <?php 
                    $query=mysql_query("select * from category")or die(mysql_error());
                    while($row=mysql_fetch_array($query)){
                        $id=$row['id'];
                    ?>
                    <tr>
                        <td width="22%"></td>
                        <td width="5%" style="padding-bottom: 4px"><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td>
                        <td width="20%" style="padding-top:3px; padding-bottom: 1px"><?php echo $row['type'] ?></td>
                    </tr>
                    <?php  } ?>
            </table>

        </tr>
        <tr>
            <td><br /><br /></td>
        </tr>
    </table>

<input name="save" type="submit" id="save" class="btn btn-success" value="tEST">

</form>

add_form.php

<?php

require_once($_SERVER['DOCUMENT_ROOT'].'/dbconnect.php');

//specify here the Database name we are using
$name = $_POST['name'];
$category_id = isset($_POST['$id[$i]']) ? 1 : 0;

$sql = "INSERT INTO `test`.`record` (`id`, `name`, `category_id`, `date_added`) 
        VALUES (NULL, '{$name}', '{$category_id}', CURRENT_TIMESTAMP());";
//using mysql_query function. it returns a resource on true else False on error
$retval = mysql_query( $sql, $conn );    
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
?>
                    <script type="text/javascript">
                        alert("New Record is Added to the Database");
                        window.location = "record.php";
                    </script>
                    <?php
//close of connection
mysql_close($conn);
?>
share|improve this question
    
What is '$id[$i]' by the way? –  u_mulder Mar 5 at 18:46
add comment

1 Answer

Look into the SET data type. I believe it will do what you want, as long as there aren't too many category IDs.

You would have to define your category_id column to be SET(1,2,3,4,5...) and then you can insert data as follows:

$name = $_POST['name'];
$category_id = implode(',', $_POST['selector']);

INSERT INTO `test`.`record` (`id`, `name`, `category_id`, `date_added`) VALUES (NULL, '{$name}', '{$category_id}', CURRENT_TIMESTAMP()); 

Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.