-1

actually i had some text content to include into my database the format is text. But i can save it in my database (in phpmyadmin)i'm using xammp . Can you help me to find where is the problem with my code please ?

i had a first file name text2.text in to a folder name files he look like this :

title : a subject , content : lorem ipsum dolor , autor : John Doe
title : a subject1 , content : lorem ipsum dolor , autor : Izzy Doe
title : a subject2, content : lorem ipsum dolor , autor : Pat Doe
title : a subject3, content : lorem ipsum dolor , autor : Jimmy Doe
title : a subject4, content : lorem ipsum dolor , autor : Dave Doe

i create a database name "mydatabase" and i create a table name "thetest "

here is my code :

<?php
$conn = new mysqli('localhost','root','','mydatabase');

$file= fopen("files/text2.text","r");

while (!feof($file)){
    $content = fgets($file);

    $carray = explode(",", $content);


    list($title,$content,$autor)= $carray;

  $sql=" INSERT INTO `thetest` (`id`, `title`, `content`, `autor`) VALUES ( '$title', '$content', '$autor')";


$conn-> query($sql);
   //echo "<pre>";
   //var_dump($carray);
}

fclose($file);
?>

the var_dump here helped me to see if the explode work well

i expected with this code that when i refresh my page all the data saved in my database but i had nothing save inside . I know that actually the database is not secure with this code i will work later on this but actually this database is a trainning tool to learn how to save data inside. Thank you by advance

  • It *looks* like you're just [trying to import a CSV?](https://stackoverflow.com/questions/3025648/import-csv-to-mysql) – CD001 Sep 04 '19 at 09:59
  • I don't want to import a CSV file CD001 , i just want save some text content in my database .By using a post method with the form i can put content in my database but i wanted to first of all create several content, save them directly in a database without edit them direclty in command line, if you see what i mean – yoyosonsaku Sep 04 '19 at 10:17
  • @misorude i don't understand your answer i'm new in the development and i try things to progess in that field ,i came here to find developers who can help me to find a solution. i hopping that people lead me to find a solution , your comment do not lead me at all ,maybe just lead me to a similar post that you explain again and again will be helpfull an apropriate but the "go inform yourself how to do that. (This is not a thing we should have to explain here over and over again" is not appropriete here .(if you need to be a confirmed developer so sorry about my presence here) have a nice day – yoyosonsaku Sep 04 '19 at 10:30

2 Answers2

0

Firstly you need to convert file extension from .text to .csv, if you want to save it into database. try using this code`

$filename = $_FILES["email"]["tmp_name"];       




     if($_FILES["email"]["size"] > 0)
     {
        $file = fopen($filename, "r");
        while (($getData = fgetcsv($file, 1000, ",")) !== FALSE){

     $sql = "INSERT into emails (emails,website) 
             values ('".$getData[0]."','".$getData[1]."')";
               $result = mysqli_query($connect, $sql);
            if(!isset($result))
            {
                echo "please upload csv file";      
            }
            else {
                  echo "File has been uploaded";
            }
         }

         fclose($file); 
     }

`

0

i find out where was the error in my code. It was the format of the text2.text ,i change it in a text2.txt , and it was the sql request where i put id => instead of (id, title, content, autor) i put (title, content, autor). those modification works and saves the text data in my database . here is my new code :

<?php
$conn = new mysqli('localhost','root','','mydatabase');

$file= fopen("files/text2.txt","r");

while (!feof($file)){
    $content = fgets($file);

    $carray = explode(",", $content);


    list($title,$content,$autor)= $carray;

  $sql=" INSERT INTO `thetest` (`title`, `content`, `autor`) VALUES ( '$title', '$content', '$autor')";


$conn-> query($sql);
   //echo "<pre>";
   //var_dump($carray);
}

fclose($file);
?>

i hope that will help someone else have a nice day!