I am having issues using a prepared insert statement. The script is supposed to read the contents of the .txt file and insert the data into the corresponding columns in my database. Here is the error I keep getting when trying to insert.
Fatal error: Uncaught mysqli_sql_exception: Column 'propertyName' cannot be null in C:\xamppp\htdocs\php7\propertyUpload.php:19 Stack trace: #0 C:\xamppp\htdocs\php7\propertyUpload.php(19): mysqli_stmt_execute(Object(mysqli_stmt)) #1 {main} thrown in C:\xamppp\htdocs\php7\propertyUpload.php on line 19
BTW: The contents of the .txt are in an array like so:
TEST1000, PROPERTY1
TEST2000, PROPERTY2
TEST3000, PROPERTY3
<?php
include 'config.php';
if (empty($_SERVER['HTTP_REFERER']))
{
header('Location: ""');
exit;
}
ini_set("auto_detect_line_endings", true);
$open = fopen('test22-7-18.txt','r');
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode(",",$getTextLine);
list($accountCode,$propertyName) = $explodeLine;
$sql = "INSERT INTO properties (accountCode, propertyName) values(?, ?)";
if ($stmt = mysqli_prepare($link, $sql))
{
mysqli_stmt_bind_param($stmt,"ss",$accountCode, $propertyName);
(mysqli_stmt_execute($stmt));
}
}
fclose($open);
?>
However, when I change my code to this the contents are inserted correctly:
<?php
include 'config.php';
if (empty($_SERVER['HTTP_REFERER']))
{
header('Location: ""');
exit;
}
ini_set("auto_detect_line_endings", true);
$open = fopen('test22-7-18.txt','r');
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode(",",$getTextLine);
list($accountCode,$propertyName) = $explodeLine;
$sql = "INSERT INTO properties (accountCode, propertyName) values(? , '$propertyName')";
if ($stmt = mysqli_prepare($link, $sql))
{
mysqli_stmt_bind_param($stmt,"s",$accountCode);
(mysqli_stmt_execute($stmt));
}
}
fclose($open);
?>
Is the second script open to SQL injection, if not could I leave it as is? Does anyone know why I can't get the first statement to work?