1

I have a HTML form which have only "value" text field.But user can generate any number of "value" test field for 1 submit button. Here is my table

  CREATE TABLE IF NOT EXISTS `insert` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `insert_operation` int(11) NOT NULL,
    `value` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
   PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Now if user generates 4 "value" text field and enter 4 value data like(a,b,c,d) it should perform 4 insert query but insert_operation track of every submit button click.

It'll look like below

 Id Insert_operation value
  1   1                 a
  2   1                 b
  3   1                 c
  4   1                 d
  5   2                 x
  6   2                 a       

my html form has value text field which name is value and submit button which name is save.

I am not sure of my php code. I know the Insert query must be inside PHP loop. But how can I perform?

9
  • where is your html or php? Commented Jul 8, 2013 at 6:57
  • 1
    I need your form code and insert query code that you are currently using. Commented Jul 8, 2013 at 6:58
  • INSERT INTO 'insert' VAULES('',$i,$value); without loop Commented Jul 8, 2013 at 7:07
  • can you please update your question with proper html and php code so that I can correct the same. Commented Jul 8, 2013 at 7:15
  • @FakhruddinUjjainwala the problem is I am unable to write html tag in question box. I have 1 text input name is "value". But it can 1 more. user generate any number of "value" text field Commented Jul 8, 2013 at 7:23

3 Answers 3

1

assuming your value fields are all named the same ie. <input name="value[]" ... />

Try something like this -

for($i=0;$i<count($_POST['value']);$i++){
   $value = mysql_real_escape_string($_POST['value'][$i]);
   if($i == 0){  // if the first increase MAX(insert_operation) by 1
       mysql_query("INSERT INTO `insert` SELECT null, IFNULL((SELECT MAX(insert_operation)+1 from `insert`), 1), '{$value}'");
   }
   else {   // if not the first use MAX(insert_operation)
      mysql_query("INSERT INTO `insert` SELECT null, IFNULL((SELECT MAX(insert_operation) from `insert`), 1), '{$value}'");
   }
}

The first one will get the MAX(insert_operation) that is already in the table, and if null will set it to 1, else it will increase by 1. All the rest will just set insert_operation to MAX(insert_operation).

See this SqlFiddle Example - http://sqlfiddle.com/#!2/afdc0/1


a few notes. insert is a mysql function/reserved word. It would be better to rename your table, but at the very least it needs to be in backticks - `. You should be escaping any user data ($_POST['value']), in this case I showed mysql_real_escape_string(). Finally, mysql_* functions are depreciated as of php5.5. You should update to mysqli_ or PDO

Sign up to request clarification or add additional context in comments.

1 Comment

awesome @Sean. I was really trouble for this. you solve this easily. I use CODEIGNITER. SO I am not use mysql_* functions. Thank you very much
1

This is wrong:

mysql_query(INSERT INTO 'insert' VALUES('','$i','$_POST['value']'));

mysql_query expects the first parameter to be a SQL string, so the correct parameter would be:

mysql_connect("
    INSERT INTO 
        `insert` (`insert_operation`, `value`) 
    VALUES 
        ($i, '{$_POST['value']}')
");

It's good to add the columns as well, because the your table may change after so time, and if this happens, your SQL won't work.

Other error is that you are using assignment instead of comparison while($_POST['value']=1) assigns the value. You have to use == to check if the value is equal to 1 like so while($_POST['value'] == 1)

PS It's not a good idea to use MySQL functions as table names and it's not a good idea to use mysql_* functions as they are deprecated as of php 5.5 and will be removed

Comments

1
<form>
<?php
for ($i=1;$i<=$no_of_text_field;$i++) // no of text fields user can add
{
?>
<input type="text" name="value<?php echo $i ?>">
<?php
}
?>
<input type="text" name="no_of_text_field<?php echo $i ?>" value="<?php echo $no_of_text_field ?>">
<input type="submit" name="submit" value="">
</form>

<?php
if(isset($_REQUEST['submit']))
{
    for ($k=1;$k<=$_REQUEST['no_of_text_field'];$k++)  //getting values of all text fields
    {
       mysql_query("INSERT INTO `insert` VALUES('','$i','$_REQUEST[value].$k')");
    }
}
?>

Check the above code this should give you some idea

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.