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?

  • What errors are you getting? –  Oct 22 '14 at 10:50
  • am i rght in thinking that `parcheggio` should be in *OR* out? – andrew Oct 22 '14 at 10:51
  • It's because you're updating. First it changes into IN, and then it updates into OUT. – Matheno Oct 22 '14 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 –  Oct 22 '14 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 – Matheno Oct 22 '14 at 10:54
  • then it seems that you should have two db fields, `in` and `out` and have separate names for the checkboxes instead of using an array also see http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – andrew Oct 22 '14 at 10:55
  • iF IT'S POSSIBLE IN THE FIELD PARKING OF MY DB I WANT THIS RESULT: "iN,OUT" –  Oct 22 '14 at 10:56
  • do you want like 'InOut' when both are checked? – Rasel Oct 22 '14 at 11:00

5 Answers5

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);
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.

StormRideR
  • 1,738
  • 1
  • 14
  • 13
  • IMHO serializing data is a bad practice, it makes future searches etc harder, better two use two columns – andrew Oct 22 '14 at 10:57
  • @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, – StormRideR Oct 22 '14 at 11:02
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';
Rohit Iws
  • 21
  • 1
  • 3
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.

Jobayer
  • 1,221
  • 1
  • 14
  • 22
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);
Rasel
  • 5,488
  • 3
  • 30
  • 39