0
 <tr><td><strong>Servizi Offerti</strong></td></tr>
 <tr><td><strong>Parking</strong></td></tr><tr>
 <td><input type="checkbox" name="parking[]" value="In"> In</td>
 <td><input type="checkbox" name="parking[]" value="Out"> Out</td>
 <td>Altro <input name="parking[] type="input" placeholder="say something"> </td></tr>

And I'm trying to insert the values into my DB

here the code I'm using

foreach($_POST['parking'] as $index => $val){
$sql="UPDATE lista SET parcheggio='$val' where id_user='1'"; 
$results = mysql_query($sql);
}

Now if a check both in and out I'll obtain only in db the value "OUT"

How can I solve it?

8
  • What errors are you getting? Commented Oct 22, 2014 at 10:50
  • am i rght in thinking that parcheggio should be in OR out? Commented Oct 22, 2014 at 10:51
  • It's because you're updating. First it changes into IN, and then it updates into OUT. Commented Oct 22, 2014 at 10:52
  • I'd like to have both in the filed of my DB IN and OUT if the customers selcet both. Instead I obtain only Out Commented Oct 22, 2014 at 10:52
  • Like, 2 lines with 1 line of OUT and another with IN? Also, your query isn't ok. You're updating 1 column so it will be constantly overwritten Commented Oct 22, 2014 at 10:54

5 Answers 5

1

I'd create separate fields in the DB table... one for IN, one for OUT, and one for Other, then not use the POST 'parking' array, but different var names and update from those instead of the foreach.

<tr><td><strong>Servizi Offerti</strong></td></tr>
<tr><td><strong>Parking</strong></td></tr><tr>
<td><input type="checkbox" name="parkingIn" value="In"> In</td>
<td><input type="checkbox" name="parkingOut" value="Out"> Out</td>
<td>Altro <input name="parkingOther type="input" placeholder="say something"> </td></tr>

then

$in = mysql_real_escape_string($POST['parkingIN']);
$out = mysql_real_escape_string($POST['parkingOut']);
$other = mysql_real_escape_string($POST['parkingOther']);
$sql="UPDATE lista SET parcheggioIn='$in', parcheggioOut='$out', parcheggioOther='$other' where id_user='1'";
$results = mysql_query($sql);
Sign up to request clarification or add additional context in comments.

Comments

0

Using this will give you like 'InOut'

$text="";
    foreach($_POST['parking'] as $index => $val){
    $text=$text.$val;
    }
    $sql="UPDATE lista SET parcheggio='$text' where id_user='1'"; 
    $results = mysql_query($sql);

Comments

0

You are updating same record twice. First with "In" and Second with "Out". What is datatype of your "parcheggio" field. It it is "varchar" or "text" or "set" then you can use following query:-

$sql='UPDATE lista SET parcheggio='.implode(','.$_POST["parking"]).' WHERE id=1';

Comments

0

You have used parking as an array, if you want to insert/update every data then treat $_POST['parking'] as array, & then update it in database.

Comments

0

try something like this

$sql='UPDATE lista SET parcheggio='.implode( ", " ,$_POST["parking"])'. where id_user=\'1\'"; 
$results = mysql_query($sql);

and read something about sqlinjections because that kind of code is extremely dangerous.

2 Comments

IMHO serializing data is a bad practice, it makes future searches etc harder, better two use two columns
@andrew it all depend on db design and number of options. what would you sugest column for each option? enum for all possible combinations? binary equal to binary representation of selected options? it was realy basic question from somebody who is clearly beginner so there is no need to jum into search optimalization,

Your Answer

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