Jump to content

[SOLVED] Something Simple?


oolongdavies

Recommended Posts

The following code selects checkbox values and posts them back to the page in an array.

 

 

I can get the two echo statements at the bottom of the script to display the correct results, and I can even add the checkbox values to the database.

 

However, the table 'tbl_product_risks' contains two fields, fld_risk and fld_username.

 

The username is stored in $_SESSION['username'] and I would like to add this value to  the database (fld_username) for each checkbox value (fld_risk) that is inserted - so the records would be inserted 'checkboxvalue', 'username'.

 

I am a php novice and I would appreciate any help (I have been all day on this and I seem to be going round in circles!)

 

<?include_once('dbconn.php');?>
<form name = "addmsds3" action="index3.php" method="post">
<?php

$sql = "SELECT * FROM tbl_risk_phrases";
         $result = mysql_query($sql);
         while ($row = mysql_fetch_assoc($result)) {
    echo '<input type="checkbox" name="selection[]" value="' . $row['fld_risk_phrase_id'] . '">';
        echo '' . $row['fld_risk_phrase_id'] . '';
        echo '<br>';
        }
        
    ?>    
    <input type="submit" value="submit" name="submit">
</form>
<br>

<?php
if(!isset($_POST)) exit();
$selections = (isset($_POST['selection']))?$_POST['selection']:NULL;
$count = count($selections);


$query = 'INSERT INTO tbl_product_risks (fld_risk) VALUES (';
$add = "'".implode("','",$selections)."'";

$query .= $add.')';
echo $query;
echo '<br>';
echo $count
?> 

Link to comment
Share on other sites

Thanks for that but I can't get it to work.

 

At the moment, the echo $query statement just displays "INSERT INTO table (fld_username, fld_risk) VALUES ("jupiter","Array")"

 

 

 

I'deally, here's what I'd like - if 4 boxes are checked, then I need to insert 4 records where each record has the $SESSION['username'] value and one of the checkbox values.

 

Alternatively, I could live with one row that contains the username and an array of the checkbox values. However, I have no idea how to retrieve them...

 

 

Link to comment
Share on other sites

err... oops... sorry for that, here's the updated code:

 

<?php
  foreach($_POST['selection'] as $k => $v) {
    $query = 'INSERT INTO tbl_product_risks (fld_username, fld_risk) VALUES ('.$_SESSION['username'].','.$v.')';
  }
?> 

Link to comment
Share on other sites

Thanks for that, I'm getting closer...

 

That does work but it only adds the value of the last checkbox selected.

 

Ideally, I would like to add each checkbox value (and username) to a new row in the db - so if 5 checkboxes are ticked, 5 new records are inserted.

 

I really appreciare your help.

 

TIA

 

J

Link to comment
Share on other sites

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.