2

I have

<form action="entry.php" method="post" >
<table>
<tr>
    <td>
        <input type="text" name="searchid[]" id="searchid" placeholder="Data 1" ><br />
        <input type="text" name="searchid[]" id="searchid" placeholder="Data 2" ><br />
        <input type="text" name="searchid[]" id="searchid" placeholder="Data 3" ><br />
        <input type="text" name="searchid[]" id="searchid" placeholder="Data 4" ><br />
    </td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="submit" /></td>
</tr>

</table>    
</form>

And entry.php code,

<?php
include "db.php";

if (isset($_POST["submit"]))

$data1 = mysql_real_escape_string($_POST['searchid']);

$query1 = "INSERT INTO php_test (name) VALUES ('$data1')";
$query = mysql_query($query1,$connection);

if($query){
    header ("location: index.php");
    }
else{
    echo "Something Wrong";
    }

?>

This code working with single input data but, I want to enter four data in seperate field of same column and when I submit then it insert data in seperate row, fields name are same and

1

4 Answers 4

3

$_POST['searchid'] is an array, not a single string. You need to loop over them:

if (isset($_POST["submit"]))
    foreach ($_POST['searchid'] as $searchid) {
        $data1 = mysql_real_escape_string($searchid);
        mysql_query("INSERT INTO php_test (name) VALUES ('$data1')") or die(mysql_error());
    }
}
header("location: index.php");

Also, get rid of the duplicate id="searchid" attributes in your HTML. IDs have to be unique. You probably don't need these elements to have IDs at all.

If you have multiple columns, you can do it like this:

if (isset($_POST["submit"]))
    foreach ($_POST['searchid'] as $index => $searchid) {
        $data1 = mysql_real_escape_string($searchid);
        $data2 = mysql_real_escape_string($_POST['searchid2'][$index]);
        mysql_query("INSERT INTO php_test (name, name2) VALUES ('$data1', '$data2')") or die(mysql_error());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

hello sir if I will add one more input field and one more colunm which name is "searchid 2" then how to do same thing for both column "is it possible?"
2

As you are getting value in array. So you should place your insert statement inside loop. may be something like this:-

As suggested you can't mysql_real_escape_string over array. So you should remove from top and mysql_real_escape_string inside the loop for each value.

$data1 = $_POST['searchid'];
foreach($data1 as $postValues) {
  $name = mysql_real_escape_string($postValues);
  $query1 = "INSERT INTO php_test (name) VALUES ('$name')";
  $query = mysql_query($query1,$connection);
}

1 Comment

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in F:\GoogleDrive\www\search engine\entry.php on line 6 Warning: Invalid argument supplied for foreach() in F:\GoogleDrive\www\search engine\entry.php on line 8 Notice: Undefined variable: query in F:\GoogleDrive\www\search engine\entry.php on line 12 Something Wrong
1

Please try below code :

<?php
   include "db.php";

   if (isset($_POST["submit"]))

   foreach($_POST['searchid'] as $id){          
       $searchid= mysql_real_escape_string($id);
       $query1 = "INSERT INTO php_test (name) VALUES ('$searchid')";
       $query = mysql_query($query1,$connection);
   }
   if($query){
     header ("location: index.php");
   }
   else{
     echo "Something Wrong";
   }

?>

1 Comment

you can't call mysql_real_escape_string on an array.
1

try this:

<?php
include "db.php";

if (isset($_POST["submit"]))


foreach($_POST["submit"] as $searchval) {
  $query1 = "INSERT INTO php_test (name) VALUES ('$searchval')";
  $query = mysql_query($query1,$connection);
}

if($query){
  header ("location: index.php");
}
else{
  echo "Something Wrong";
}

?>

1 Comment

Two issues: 1. You're subject to SQL injection. 2. You're only checking whether the last insert was successful.

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.