skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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!


  2. 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); 
         }
    

    `

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search