0

I am unable to get my text file data.txt to read correctly into my database.
This is what my text file looks like

9889483782,6068378394,6069437364... and so on for like 50 numbers

I have to get each of the comma seperated values to show up in the database, id like them to be placed in their own respective row in my table numdata where the values will be inserted in the field, Numb.

My problem is that im not getting any errors? but my code produces no results. What do i do to fix this?
Also please try to elaborate and use code examples, i learn better by seeing it work as well as understaing how it works. Thank You.

Another quick note, the data in the txt file has nothing else in it, no spaces or anything just comma seperated vlaues.

<?php

    require ('dbconnect.php'); // connect to database

    // initial database stuff

        $file = "data.txt";
        $file_handle = fopen($file, "r");


        fclose($file_handle);

            while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) 
            {  

                $i = 1;
                foreach($file as $row)
                {
                    $data = explode(',',$row);
                    $query .= $i < $count ? ',':'';
                    $i++;
                }
                $query="INSERT into Numbers(Numb) values('$data')";
                mysql_query("TRUNCATE TABLE Numbers") or die("MySQL Error: " . mysql_error()); //Delete the existing rows
                mysql_query($query) or die(mysql_error());  
            }  




echo "Done!";
?>
3
Contributors
18
Replies
42
Views
3 Years
Discussion Span
Last Post by redalert19
Featured Replies
  • So your table name is numbdata and not numbers, so the query should be: `$query="INSERT into numbdata(Numb) values($row)";` and the truncate query: `mysql_query("TRUNCATE TABLE numbdata") or die("MySQL Error: " . mysql_error());` Update the query in my code and you are all set. By the way, are you sure you want … Read More

  • Paste your full code now. You can truncate if you don't need that data now. I was just confirming if you really need to. Read More

0

Your code should be:

<?php
require ('dbconnect.php'); // connect to database
// initial database stuff
$file = "data.txt";
$file_handle = fopen($file, "r");

$file = "data.txt";
$file_handle = fopen($file, "r");
mysql_query("TRUNCATE TABLE Numbers") or die("MySQL Error: " . mysql_error()); //Delete the existing rows
while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE)
{
    foreach($data as $row)
    {
        $query="INSERT into Numbers(Numb) values($row)";
        mysql_query($query) or die(mysql_error()); 
    }
}
fclose($file_handle);

echo "DONE!";
?>
0

TYSM now im actually getting some errors, however its saying now that my table doesnt exist?
here is the error.

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload/upload.php on line 14

Notice: Array to string conversion in /opt/lampp/htdocs/upload/upload.php on line 20
MySQL Error: Table 'Numbers.Numbers' doesn't exist

I dont understand? i mean the only line i changed was just that one and even though i know the table does exist my code says it doesnt?

sigh if it aint one thing its anohter :/

0

See the code I posted. Compare my foreach loop with yours. $data is an array, so you can't use explode on it. Plus your want to loop for each element of the array so the correct loop is what I wrote.
Also your Table does exist, but you are using the wrong database name. Post a screenshot of your table in the databse. And also post your dbconnect.php file(without the passwords of course ;) ).

0

well i checked the database issue but i didnt get far :( here is the dbconnect.php

<?php
    $host = "*****";        // the host your database server is on, probably localhost
    $username = "*****";        // Database Username, if you are doing this locally and haven't set one it will probably be "root"
    $password = ******;            // Database Password, if you are doing this locally and haven't set one it will probably be NULL 
    $database = "Numbers";    // The name of the database you want to use.

    // Try to connect to mysql
    mysql_connect($host, $username, $password) or die ("Could not connect to database");  

    // Try to select the database 
    mysql_select_db($database) or die ("Could not select database");    
?>

and here is the screen shot of my table, at least i hope this is the part needed, and also ty so so much for all this, ive been working on this for a while but with little to show for it.

Edited by berserk

Attachments Screenshot_from_2014-06-13_00:54:17.png 27.87 KB
0

No, I wanted to see the name of your database and the table. I think you are confusing between the database name and the tablename.

1

So your table name is numbdata and not numbers, so the query should be:
$query="INSERT into numbdata(Numb) values($row)";
and the truncate query: mysql_query("TRUNCATE TABLE numbdata") or die("MySQL Error: " . mysql_error());
Update the query in my code and you are all set.
By the way, are you sure you want to truncate the table data??

Edited by tapananand

0

i mean i dont really need to since there is only comma seperated data in my file but someone i know told me it was good practice to clean my data? not sure but i tried your solution and its still giving me this error
MySQL Error: Table 'Numbers.Numbers' doesn't exist

1

Paste your full code now.
You can truncate if you don't need that data now. I was just confirming if you really need to.

-1

here is the upload.php

?php

    require ('dbconnect.php'); // connect to database

    // initial database stuff

        $file = "data.txt";
        $file_handle = fopen($file, "r");

        mysql_query("TRUNCATE TABLE Numbers") or die("MySQL Error: " . mysql_error()); //Delete the existing rows
        while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE)
        {
            foreach($data as $row)
            {
                $query="INSERT into numbdata(Numb) values($row)";
                mysql_query($query) or die(mysql_error()); 
            }
        }

fclose($file_handle);


echo "Done!";
/*


    // Form to upload a new file, make sure you get the "enctype" or it won't work
    echo "
        <form  enctype='multipart/form-data' name='fileupload' action='upload.php' method='POST'>

            <input type='file' name='uploadedfile'> 
            <input type='submit' name='upload' value='Upload File'>

        </form>
    ";
*/
?>

and here is the dbconnect.php

<?php
    $host = "";        // the host your database server is on, probably localhost
    $username = "";        // Database Username, if you are doing this locally and haven't set one it will probably be "root"
    $password = ;            // Database Password, if you are doing this locally and haven't set one it will probably be NULL 
    $database = "Numbers";    // The name of the database you want to use.

    // Try to connect to mysql
    mysql_connect($host, $username, $password) or die ("Could not connect to database");  

    // Try to select the database 
    mysql_select_db($database) or die ("Could not select database");    
?>
Votes + Comments
Check if you did completely what was asked before posting here. You updated only one line and not the other.
0

See, you didn't update the truncate line(line 10) see my previous post.
It should be: mysql_query("TRUNCATE TABLE numbdata") or die("MySQL Error: " . mysql_error());

0

OMG it works :D now there is one more little part id like help with if you would be so kind. The part of my file that is in html that is an upload form, how would i go about combining that with my current setup where the file would be uploaded by a user and automatically read into the data base? like they would upload from where ever and the result would all the info in the file would be read into the database

and also seriously thank you so much, i am forever grateful :)

0

The upload form:

 <form enctype='multipart/form-data' name='fileupload' action='upload.php' method='POST'>
<input type='file' name='uploadedfile'>
<input type='submit' name='upload' value='Upload File'>
</form>

Should be in a separate file by any name. You need to have an input of type file in the form as well. The uploaded file should be processed and inserted into db like above in upload.php. To read more about file upload refer this.
And don't forget to mark the question solved and up-vote and comment the answer.
If you have any doubt in the upload thing, make another post for that.

Edited by tapananand

0

ok now here is something interesting, im getting the strangest error with my completed program, i admit my test file data.txt was for testing purposes but now im using the main file which is MUCH MUCH larger and its giving me this

Warning: fopen(606.txt): failed to open stream: Permission denied in /opt/lampp/htdocs/upload.php on line 8

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload.php on line 11

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload.php on line 13

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload.php on line 11

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload.php on line 13

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload.php on line 11

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload.php on line 13

note the 11 and 13 line errors repeat forever, any idea why this is happening when it was JUST working??

0

do this in the htdocs directory and paste its output here: ls -l 606.txt
You may not have read permission for the file.

0

nvm it was a linux issue, its working fine now but its slow as christmas lol ty u so much for all the help

i ended up changing the permission of the folder with these commands

"ps aux | grep httpd"
this command was to see what folders had owner ship, in this case it was daemon

"sudo chown daemon /opt/lampp/htdocs"
this was to grant access to the folder

"sudo chmod -R 0755 /opt/lampp/htdocs"
this one allowed me to write to the directory

all were needed to fix my issue but now its working fine :D thank you for all your wisdom, it was all very much appreciated.

0

"sudo chmod -R 0755 /opt/lampp/htdocs"
this one allowed me to write to the directory

all were needed to fix my issue but now its working fine :D thank you for all your wisdom, it was all very much appreciated.

Edited by happygeek: spam deleted

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.