Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
     include("keselamatan.php");
     include("sambungan.php");
     include("penjual_menu.php");

     if(isset($_POST["submit"])) {
     	    $namajadual = $_POST["namajadual"]; 
     	    $namafail = $_FILES["namafail"]["name"];
     	    $sementara = $_FILES["namafail"]["tmp_name"];
     	    move_uploaded_file($sementara, $namafail);

     	    $fail = fopen($namafail, "r");

     	    while (!feof($fail)) {
     	    	  $medan = explode(",", fgets($fail));
     	    	  $berjaya = false;

     	    	  if (strtolower($namajadual) === "model") {
     	    	  	 
                      $IDModel = $medan[0];
     	    	  	  $NamaModel = $medan[1];
                      $Processor = $medan[2];
                      $RAM = $medan[3];
                      $Storage = $medan[4];

                      
     	    	  	    $sql = "insert into model values('$IDModel','$NamaModel','$Processor','$RAM','$Storage')";
     	    	  	    if (mysqli_query($sambungan, $sql))
     	    	  	    	 $berjaya = true;
     	    	  	    else
     	    	  	    	 echo"<br><center>Ralat: $sql<br>".mysqli_error($sambungan)."</center>";
     	    	  }
                   if (strtolower($namajadual) === "jenama") {
                        $IDJenama = $medan[0];
                        $NamaJenama = $medan[1];
                        $sql = "insert into jenama values('$IDJenama','$NamaJenama')";
                        if (mysqli_query($sambungan, $sql))
                             $berjaya = true;
                        else
                             echo"<br><center>Ralat: $sql<br>".mysqli_error($sambungan)."</center>";
     	    }
                }

     if ($berjaya == true)
     	  echo "<script>alert('Record successfully imported');</script>";
     else
     	  echo "<script>alert('Record was not successfully imported');</script>";
     }
?>

<link rel="stylesheet" href="aborang.css">
<link rel="stylesheet" href="abutton.css">

<h3 class="panjang">IMPORT DATA</h3>
<form class="panjang" action="import_model.php" method="post" enctype="multipart/form-data"
      class="import">
      <table>
      	    <tr>
      	    	<td>Table</td>
      	    	<td>
      	    		<select name="namajadual">
                                <option>Model</option>
                                <option>Brand</option>
      	    		</select>
      	        </td>
      	    </tr>
            <tr>
            	<td>Fail Name</td>
            	<td><input type="file" name="namafail" accept=".txt"></td>
            </tr>
      </table>
      <button class="import" type="submit" name="submit">Import</button>
</form>


What I have tried:

Cannot import the data
Don't know how to change it to be right
Posted
Updated 3-Jul-23 3:43am

1 solution

PHP
while (!feof($fail)) {
    $medan = explode(",", fgets($fail));
    $berjaya = false;

// remainder of the while block
}

if ($berjaya == true)

The variable $berjaya is defined in the while block, so as soon as the loop ends it no longer exists. So move its definition above the loop and it should work:
PHP
$berjaya = false; // ensure that this variable is not confined to th while block
while (!feof($fail)) {
    $medan = explode(",", fgets($fail));
    $berjaya = false;

// remainder of the while block
}

if ($berjaya == true)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900