0

I am new to Php and what I would like to do is input multiple lines of data into a database through a Php form textfield. The code I have used now is.

 for($n=0;$n<5;$n++) {
      if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
         $insertSQL = sprintf("INSERT INTO cmsd1 (Population_Name) VALUES (%s)",
                           GetSQLValueString($_POST['Form1'], "text"));

         mysql_select_db($database_cmsdtest, $cmsdtest);
         $Result1 = mysql_query($insertSQL, $cmsdtest) or die(mysql_error());

         $insertGoTo = "ttt.php"; 
     }
 }

However as you can see, all it does is loop the data I have given 5 times. I got a suggestion to use 'implode' function but as I am new, I have no clue on how to do it. I use Dreamweaver for help with my PhP. I would ideally like this code to loop as many times as the data entered is and not just 5.. Each data point is separated by a space. It could be 100 too. Please advice.

Tried imploding this way but doesn't work..

 GetSQLValueString(implode(" ",$_POST['form1']), "text"));

2 Answers 2

0

if the below code is not helping then put your code in pastebin then only i can figure out where are you wrong.

//let the values are like $valesstring = " val1 val2 val3 val4"; //convert them into an array of values with $valuearray = explode(" ",$valesstring); //then i think u know how to loop the array and put them in text field.

//if your form name  is = 'frm1'
//if textfield name is ='textfieldname'
// when u submit the form then in php you have the form values in 
// $_REQUEST['textfieldname'] 

//do var_dump($_REQUEST['textfieldname']) and check if it got textfield values array
    foreach($_REQUEST['textfieldname']  as $key => $value){
      //print($key."--".$value); to check if u got the values
    //put your code here 
     $insertSQL = sprintf("INSERT INTO cmsd1 (Population_Name) VALUES (%s)",
                           GetSQLValueString($value, "text"));

    //put your code here
    }
Sign up to request clarification or add additional context in comments.

1 Comment

0

Hope this help and i hope this is what you need: Else please clarify your question further

//if your form name  is = 'frm1'
//if textfield name is ='textfieldname'
// when u submit the form then in php you have the form values in 
// $_REQUEST['textfieldname'] 
    foreach($_REQUEST['textfieldname']  as $value){
    //put your code here 
     $insertSQL = sprintf("INSERT INTO cmsd1 (Population_Name) VALUES (%s)",
                           GetSQLValueString($value, "text"));

    //put your code here
    }

1 Comment

I have like a 100 values separated by a space which I would like to copy and paste in the textfield so that it is accepted by the form and put in multiple rows in the database. As of now, it doesn't happen. The answer you have written gives me an error that the query is empty.

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.