2

Here is my full code for my problem and I will explain it:

<form name="form1" action="" method="POST">
<table>
  <tr>
    <th>ORDER TYPE</th>
    <th>ITEM CODE</th>
    <th>ITEM NAME</th>
    <th>ACTION</th>
  </tr>
  <?php
    $sql = "SELECT * FROM product";
    $i = 1; //to generate an unique id and name attr
    while($data = mysql_fetch_array($sql)){
       $itemcode = $data['cars_kd'];
       echo "<tr> <td><select name='select'><option>--Select--</option><option value='0'>Distributor</option><option value='1'>Retail</option>
       <input id='id$i' name='name$i' /></select></td>
       <td>$data['cars_kd']<input type='hidden' id='id$i' name='name$i' value='$itemcode'/></td>
       <td>$data['cars_name']</td>
       <td><input type='checkbox' name='check'/><input type='hidden' id='id$i' name='name$i'/></td> </tr> $i++";
     }
     ?>
     <input type="submit" name="save" value="Save" />
   </table>

In my PHP code above, I use $i to generate an unique id and name attr. After all of the data shown I'll choose some of that data which I want it by checked the checkbox. In my code above I have three input tag. 1. function is to hold the value of the select when checkbox is check. 2. to display $data['cars_kd'] function is to show all of the data record. the value of the input is what I take to then I show well in the third input. 3. function is as parameter of user choice. So, data which I wan to insert is order type (taken from select option value) and item code (taken from input three). I use input because I think I must have a parameter to know what the user choosing.

I hope everyone here can understand my explanation. Hope any suggest :-). Thanks.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Reza M
  • 63
  • 7
  • For array use name="name []" – Sundance.101 Jan 31 '16 at 09:29
  • As said before, consider changing your input names to `name[]`. `name$i` wouldn't work since PHP only processes code within `` tags and `name$i` clearly isn't. You can get the values of the input array on $_POST by using the name of the input array as the first key and the index of the specific input control as the second (i.e. $_POST["name"][0]). [Reference.](http://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) – Revenant Feb 01 '16 at 08:00
  • I said thanks to everybody here who have been given an answer :-) – Reza M Feb 02 '16 at 09:01

4 Answers4

3
  1. Give action in your form action.Right now its empty

  2. You need to give the name to your all text boxes name[] not name$i that means:-

Change <input type="text" id="id$i" name="name$i" />$data['data']; to <input type="text" id="id$i" name="name[]" />$data['data'];

  1. Now you will get all the names in $_POST['name'] array.

  2. I assumed that your table structure is (id,car_name) and you want to save data like (1,Volvo)(2,Dukati) etc. Also i assume that id is auto-incremented and primary key. Now you need to do stuff like below:-

    $_POST['name'] = array('Volvo','Dukati','KTR','BMW');// now your POST array will look like this
    $data = $_POST['name'];
    $values = '';
    foreach($data as $dat){
       $values .= "(".$dat."),";
    }
    $values = trim($values,",");
    
    $sql = "INSERT INTO <table name> ('car_name') VALUES '".$values."'";
    echo $sql;
    

Output:- https://eval.in/510646

Note:- Its not the complete code, its just an example to show you how can you proceed. Thanks

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Is it same $_POST['name'] = array('Volvo','Dukati','KTR','BMW'); with $_POST['name'] = array($data['data']); ? Because I get Volvo, BMW, etc from my table and show it with while(mysql_fetch_array). – Reza M Feb 01 '16 at 01:23
  • In example inserted value is `(Volvo),(Dukati),(KTR),(BMW)`, but why you need array? You store data in DB like string?! Show your table structure?! I think you have : id=1;car=volvo;id=2;car=bmw; ... And you not need to insert in same row 2 car name? – Michael Feb 01 '16 at 06:36
  • If your table scructure is : id=1;car1=bmw;car2=zaporojets... you insert need to by `insert into table (id,car1,car2)` Values ('','bmw','zaporojets') – Michael Feb 01 '16 at 06:42
  • 1
    I want to insert that in one field. Okay I'll update my code. Wait a minute. – Reza M Feb 01 '16 at 10:52
  • 1
    I said thanks to everybody here who have been given an answer :-) – Reza M Feb 02 '16 at 09:02
1

Put to action="" path to script like this /php/script.php

AND add <? echo ;?> for print variable $i value

**PHP **

 <form action="/script.php" method="POST">
   <input type="text" id="id<? echo $i; ?>" name="name<? echo $i; ?>" /><? echo $data['data'];?>

  <input type="submit" name="submit" value="Save"/>

Michael
  • 1,089
  • 1
  • 11
  • 28
  • I think this : echo $data['data'];?>. is same with mine: $data['data']; because I make this tag in php array. But my problem with that such code is in my PHP code can't run it as $_POST["name$i"] even I've put it in array like @fusion3k suggest. Hope another suggest :-) – Reza M Feb 01 '16 at 01:21
  • The example of @A-2-A is good. If you don't know how to use it close this site and take a book about PHP in hand and read it. you dont need `$_POST["name$i"]` use `$_POST["name"]` and fet all values `name[]` from form in `one array` and work with `foreach` to get all values – Michael Feb 01 '16 at 05:53
1

If I understand your question, you can try in this way:

foreach( $_POST as $key => $val )
{
    if( preg_match( '/^name(\d+)$/',$key,$matches ) )
    {
        $name = $val;
    }
}

the id number is catched in $matched[1], so - if you want use it as array index:

       $name[$matches[1]] = $val;

Edit:

If you known the exact number of name postfields, you can also try in another way.
Supposing total field number is 5:

for( $i=1; $i<6; $i++ )
{
    $name = $_POST["name$i"];
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • I've already try this : for( $i=1; $i<6; $i++ ) { $name = $_POST["name$i"]; } But it's not works @fusion3k – Reza M Feb 01 '16 at 01:10
  • @RezaM I have tried and for me it works fine. BTW, one of the answers works or you are even now w/out a solution? – fusion3k Feb 01 '16 at 01:26
  • I said thanks to everybody here who have been given an answer :-) – Reza M Feb 02 '16 at 09:01
1
<?php 
include('conect_to_db.php');
?>
<form name="form1" action="/a.php" method="POST">  <? /* action= the path + filename of script witch catch post data and work with it*/ ?>
<? for($i=0;$i<3;$i++) { ?>
<input type="text" id="id<? echo $i; ?>" name="name[]" value="<? echo $data  ['data'];?>"\>
<? 
}
?>
<input type="submit" name="submit" value="Save"/>
</form>

<?
if(isset($_POST['name'])) {
$data =$_POST['name'];
foreach($data as $value) {
/*may be like this to see you your darling name1,name2,name3, but you can do simply*/
if(!isset($name1)) $name1=mysql_real_escape_string($value); else
if(!isset($name2)) $name2=mysql_real_escape_string($value); else
if(!isset($name3)) $name3=mysql_real_escape_string($value); else 
return;
}

$sql = "INSERT INTO table_namex (name1,name2,name3) VALUES (`$name1`,`$name2`,`$name3`)";
$res=mysql_query($sql);
echo 'result(1=good): '.$res.' \   query: '.$sql;

}
?>
Michael
  • 1,089
  • 1
  • 11
  • 28
  • +10 to @A-2-A because I used some code from he! This `Is it same $_POST['name'] = array('Volvo','Dukati','KTR','BMW'); with $_POST['name'] = array($data['data']); ?` I ommit, do it you – Michael Feb 01 '16 at 06:27
  • @Reza M `I want to insert that in one field` - when you insert need to be `$value=$name1.','.$name2.','.$name3;` `INSERT INTO table_name (name) VALUES ('$value')` for example – Michael Feb 02 '16 at 10:24