1

I'm trying to insert CSV file into MySql but I'm getting an error.

Warning: mysql_real_escape_string() expects parameter 1 to be string,

My database table is:

:s_no    :name    :id   :
:1       :Lim     :678  :
:2       :Mary    :623  :
:3       :Mimi    :4124 :

The number is AUTO_INCREMENT, I wanted instead the Name And ID only.

<?php
  $connect = mysql_connect("localhost","root","","skpj");

  if(isset($_POST["submit"]))
  {
    if($_FILES['file']['name'])
    {
      $filename = explode('.',$_FILES['file']['name']);
      if ($filename[1] == 'csv')
      {
        $handle = fopen($_FILES['file']['tmp_name'], "r");
        while ($data = fgetcsv($handle))
        {
          $item1 = mysql_real_escape_string($connect, $data[0]);
          $item2 = mysql_real_escape_string($connect, $data[1]);
          $sql="INSERT into student( s_no, name, id) value ('','$item1','$item2')";
          mysql_query($connect, $sql);
        }
        fclose($handle);

        print "Done";
      }
    }
  }

?>
<html>
  <head></head>
  <body>
    <form method="post" enctype="multipart/form-data">
      <div>
        <p>Upload CSV: <input type="file" name="file" /></p>
        <p><input type="submit" name="submit" value="Import" /></p>
      </div>
    </form>
  </body>
</html>
3
  • missing form action Commented Dec 8, 2016 at 12:24
  • I didn't get this line 'The "number" is AUTO_INCREMENT, so i wanted to instead to Name And ID only". Please Explain. Commented Dec 8, 2016 at 12:33
  • The "s_no" is AUTO_INCREMENT, so i wanted to instead to Name And ID only. in the database the field "s_no" is auto generate. i just need to instead in name and id field only. Commented Dec 8, 2016 at 13:49

3 Answers 3

1

The connection is the second parameter.

mysql_real_escape_string( $data[0], $connect);

Also remember it is a deprecated function

http://php.net/manual/en/function.mysql-real-escape-string.php

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

1 Comment

okey.. i did it. no error. but the data still did not instead. have any idea what can i do??
0

try this

 mysql_real_escape_string("string")

means

mysql_real_escape_string($data[0]);

1 Comment

okey.. i did it. no error. but the data still did not instead. have any idea what can i do??
0

if you are using mysqli then

mysqli_real_escape_string($connect,$data[0]);

if you are use mysql

mysql_real_escape_string( $data[0]);

1 Comment

okey.. i did it. no error. but the data still did not instead. have any idea what can i do??

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.