Click here to Skip to main content
15,887,027 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>Jenama</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:

When I import data, the data can enter XAMPP but there will be two blank lines and an error will be displayed
Posted
Updated 4-Jul-23 0:27am

The error 'undefined array key 1' occurs when you try to access an array element using an index that does not exist. In your code, this error is most likely caused by accessing the elements of the '$medan' array without checking if they exist. You can see more on the error at - Introduction to PHP Notice Undefined Index[^]

You should always check the array size before accessing its elements to make sure that it has enough elements before accessing them, in your case 5 elements. You can add the following check -
PHP
if (count($medan) >= 5) {
    if (strtolower($namajadual) === "model") {
        // Code here...
    }
} else {
    echo "<br>Invalid array size";
}


I will also dump the values first to confirm that I get the correct array values before I will start my loop -
PHP
var_dump($medan);


This will display an output that will include information such as the data type and length of each element, as well as the array keys and values. Using this you can then check which value was not returned and fix the issue.

An example of an output using 'var_dump()' will be similar to -
array(3) {
  ["name"]=>
  string(8) "John Doe"
  ["age"]=>
  int(30)
  ["email"]=>
  string(18) "johndoe@example.com"
}


Lastly, please note OriginalGriff's description on your code security, rather use parameterized queries which you can read up on at - Prepared statements and stored procedures[^]
 
Share this answer
 
v3
That's because it's loaded from here:
PHP
$medan = explode(",", fgets($fail));
inside a loop, and if the final line is empty or contains no comma it will be an array with only a single element.

You need to check if the line has content before you start processing it rather than just assuming data exists!

[edit]
It's also a very bad idea to deal with databases like that:
1) Never assume column ordering in INSERT statements:
SQL
INSERT INTO MyTable VALUES (17, "Bicycles")
Assumes that the first column is numeric and the second is text, and that may not be the case as the first column of a table is very often a row ID column which is either GUID or an IDENTITY column you cannot change.
Always specify the columns by name:
SQL
INSERT INTO MyTable (QtyPurchased, Desc) VALUES (1, "text")
so that changes to the DB do not cause your app to crash, or worse corrupt the DB.

2) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
[/edit]
 
Share this answer
 
v2

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