0

I am trying to create a PHP function where I can upload one CSV file into MySQL and then export another CSV file with certain changes in data and tables.

Example

CSV-1:

Table name = Photolink

Table content = http:// www.sitename.com/image1.jpg

Convert it to:

CSV-2:

Table name = img_URL

Table content = product/image1.jpg

I need your help in modifying the code to make this work;

I tried this code still it do not work, friends can u help me fix it please!

        $data1 = $mysql->prepare("INSERT INTO products (name, model, quantity, image_url) VALUES (:name, :model, :quantity, :image_url)");
    $data1->bindParam(':name', $name);
    $data1->bindParam(':model', $model);
    $data1->bindParam(':quantity', $quantity);
    $data1->bindParam(':image_url', $image_url);



    if ($_FILES[csv][size] > 0) {

        //get the csv file
        $file = $_FILES[csv][tmp_name];
        $handle = fopen($file,"r");

    while ($data = fgetcsv($handle,1000,",","'")) {
        if ($data[0]) {

        $name = $data[0];
        $model = $data[1];
        $quantity = 100;
        $image_url = $data[2];
        $data1->execute();

        }
    }
2
  • 1
    first you stop using addslashes(). it is absolutely pathetically useless GARBAGE and should not even exist. then you stop using the mysql_*() functions and switch to PDO or mysqli. Then you go read the PHP docs about loops and learn why your do() loop will NEVER work as written. Commented Mar 31, 2013 at 19:45
  • And you should stop using not quoted strings until they are constants names :) Commented Mar 31, 2013 at 21:14

1 Answer 1

1

Flip your loop around:

while ($data = fgetcsv($handle,1000,",","'")) {
    if ($data[0]) {
        mysql_query("INSERT INTO products (name, model, quantity, image_url) VALUES
            (
                '".addslashes($data[0])."',
                '".addslashes($data[1])."',
                //quantity is 100 for all
                '100',
                '".addslashes($data[2])."'
            )
        ");
    }
}

Because in your original code, $data doesn't even exist yet until you define it in the while statement.

There are many other issues, but this should get you going.

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

6 Comments

Thanks Joe, I tried your code still not data inserted in mysql ... can u guide me a little more please .... this is the first time i am trying to do this and have no idea
Switch to prepared statements with PDO, then we'll talk. php.net/manual/en/pdo.prepared-statements.php
thanks alot my friend, i will study the link and hope this would solve the issue :)
Dear joe, I tried this code still it do not work, friends can u help me fix it please! ---> codes updated in question
Much better. Move the calls to bind_param to inside the loop, before the call to execute
|

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.