0

I am working on a PHP script to loop through a file to insert into the a database. Current the script is working but it is not inserting to the database. I have the connection working properly and I echo out the result just to ensure it is correct. It looks lines and loops accordingly (there is a lot more then 3 records but I did that for test purpose.

// Create connection
$conn = new mysqli($servername, $dbusername, $password, $database);

// Check connection
if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
}

$myfile = fopen("words_alpha.txt", "r") or die("Unable to open file!");

//For Each line
$index = 1;
while($index <= 3){
    $line = fgets($myfile);

    $sql = "INSERT INTO WORD (wordID, `WORD`) VALUES (NULL,'$line');";   
    echo $sql;
    $conn->query($sql); //We should be inserting here
    echo "Success <br />\n"; 
    sleep(1);
    $index++;
}

Worth noting wordID is a PK and is incremented automatically hence the NULL value.

  • "the script is working but it is not inserting to the database" --- if it's not doing the job it's intended to do it's the opposite of "working". – zerkms Sep 11 '19 at 04:43
  • You don't check for errors, you don't use prepared statements (or at least escape sql string literals), a lot could go wrong. – zerkms Sep 11 '19 at 04:44
  • Your insert is not ideal, but should be working. Is there any error message? Did you check the `WORD` table in MySQL? – Tim Biegeleisen Sep 11 '19 at 04:44
  • @TimBiegeleisen I meant compiling and running. It runs the insert statement I'm assuming because it prints out success. Yes checked the database that is how I determined it didn't work. I know the INSERT isn't ideal, but I just tried copying straight from the mySQL generator just to ensure it wasn't expecting a weird syntax because I'm running a cheap webhost for class. – user11295998 Sep 11 '19 at 04:44
  • What exactly is `$conn`? How does it handle errors? – zerkms Sep 11 '19 at 04:47
  • @zerkms added a little bit more code to the original post. – user11295998 Sep 11 '19 at 04:51
  • https://www.php.net/manual/en/mysqli-driver.report-mode.php --- check how to enable error reporting. Set it to `MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT` – zerkms Sep 11 '19 at 04:58

3 Answers3

-2

Okay try this and see is your file is readable, means stream is able to read data from filesystem ! if yes then we can write more code to save text lines in Database

 <?php
$file = fopen("test.txt","r");

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);
?> 
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
  • Such a weird issue still nothing. WORD is set out to be varchar, and is allocated to 255 so no issues there. We know we have a valid connection so no idea where the problem lies. – user11295998 Sep 11 '19 at 04:55
  • It's okay to pass null to the autoincrement column, and `fclose` in this code changes nothing. – zerkms Sep 11 '19 at 05:01
  • updations! lets test if works fine then go to next step "Save in database" – Dupinder Singh Sep 11 '19 at 05:43
  • 1
    @DupinderSingh "lets test if works" --- it makes no sense to try random irrelevant things and expect it to magically works, while you can enable error reporting and know it for sure. – zerkms Sep 11 '19 at 07:33
-2

Try This Code

$con = mysqli_connect("localhost","username","password","db name");

$sql = mysqli_query($con,"INSERT INTO WORD (`WORD`) VALUES ('$line')");
-2

I couldn't notice your file reading so I thought I should point this out:

// Create connection
$conn = new mysqli($servername, $dbusername, $password, $database);

// Check connection
if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
}

$myfile = fopen("words_alpha.txt", "r") or die("Unable to open file!");

//For Each line
while(! feof($myfile))  {
    $line = fgets($myfile);

    $sql = "INSERT INTO WORD (wordID, `WORD`) VALUES (NULL,'$line');";   
    echo $sql;
    $conn->query($sql); //We should be inserting here
    echo "Success <br />\n";
}
fclose($myfile);
Mojo Allmighty
  • 793
  • 7
  • 18