-1

I have an array of text boxes which I Have named using a variable(each text box has an unique name). I want to get the data in these text boxes with it's ID and store it in a database. I tried this using the following code but it didn't work.

if($_GET['btn1']=="Submit"){
 for ($p=0;$p<12;$p++){
   $qty=$_GET['text$p'];
   mysql_query("UPDATE model SET column$m='$qty' WHERE `ModelName`='$rt'"); 
   }      
}

I'm a newbie to PHP. Any idea would be great. Thanks.

HaK
  • 141
  • 2
  • 4
  • 13

3 Answers3

0

If you have generated text boxes using an iterator you probably want to do something like,

$qty=$_GET['text'.$p];

Instead of saving to the db with column names like c1, c2, c3, c4 try one column lets call it col and set your text boxes to save as an array.

<input name="col[]" value="apple">
<input name="col[]" value="pear">
<input name="col[]" value="banana">

Now if I tick apple and banana and submit the form to php I will get an array of values,

var_dump( $_GET['col'] ); 

will return something like,

array(2) {
  [0] =>  string(5) "apple"
  [1] =>  string(6) "banana"
}

to store in your database

mysql_query("UPDATE model SET col='".json_encode($_GET['col'])."' WHERE `ModelName`='$rt'");

When you retrieve the columns you'll need to do json_decode. When your outputting your inputs you can now do something like,

<input name="col[]" value="apple" <? if (in_array('apple', $col)) echo "CHECKED='CHECKED'"; ?> >
shapeshifter
  • 2,967
  • 2
  • 25
  • 39
  • I tried to output the values inside the textboxes, and it worked. So it must be the sql code. mysql_query("UPDATE model SET column$m='$qty' WHERE `ModelName`='$rt'"); I'm incrementing column no to insert data to defferent columns inside the database – HaK Feb 11 '13 at 08:29
  • I have a table named model where I have to insert the values of the textboxes.The column numbers are varies from 2-12. How should I enter these values using a loop. Thanks – HaK Feb 11 '13 at 09:54
  • Thats not a great way of handling saving the text box data. You'd better off with one column and saving an array or something to there. See my answer for some info. – shapeshifter Feb 11 '13 at 23:48
0

try like this:

Your textbox names are not letter, as you're using them - they are letter_0, letter_1, etc.

Also, interval is a MySQL reserved-word and you'll need to escape it using backticks: interval.

To retrieve all of them, you can hardcode all of their names such as $letter1 = $_POST['letter_1'];, or you can do it in another loop:

for ($i=0; $i<5; $i++) {
    $letter = $_POST['letter_' . $i]; 
    $interval = $_POST['interval' . $i]; 
    $gt = $_POST['gt' . $i]; 
    $s = mysql_query("INSERT INTO `grad_table` (`letter`, `interval`, `gt`) VALUES ('$letter',$interval,$gt)");
}
user2001117
  • 3,727
  • 1
  • 18
  • 18
0

$qty=$_GET['text$p']; Change it to $qty=$_GET['text'.$p]; and try

Assuming your text names are like : text1, text2, ... etc

NOTE : You used $_GET, so method in your form should be get ie: method = "GET"

Better you print_r($_REQUEST); on top of the page and do other things accordingly.

Please refer this link for diff between single and double quotes : What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73