I am trying to make an english vocabulary app. Reactjs and php is used in this app and currently I have problem with preventing SQL injections.
https://www.php.net/manual/en/mysqli.prepare.php in this link I found I can do
$conn = mysqli_connect('hostname','username','password','databasename');
if($stmt= mysqli_prepare($conn, SQL QUERY)){}
to make a prepared statement, my code is almost same as the example, but just SQL is different
<?php
header('Access-Control-Allow-Origin: http://localhost:3000/');
$content = file_get_contents("php://input");
$contentJSON = json_decode($content);
$addEnglish = $contentJSON->english;
$addKorean = $contentJSON->korean;
$addSwedish = $contentJSON->swedish;
$addSynonyms = $contentJSON->synonyms;
$addExample = $contentJSON->example;
if (!preg_match('/[^A-Za-z]+/', $addEnglish)&&!preg_match('/\s/', $addEnglish))
{
include 'db/db_connection.php';
$conn = mysqli_connect('localhost','root','','worddb');
if(connectDB($conn)){
if($mysqliPrepare=mysqli_prepare($conn,
"CREATE TABLE IF NOT EXISTS WORDLIST(
wordIndex INT(10) AUTO_INCREMENT,
english VARCHAR(20),
korean VARCHAR(20),
swedish VARCHAR(20),
synonyms TEXT,
example VARCHAR(100),
UNIQUE KEY wordDBIndex (wordIndex)
);
INSERT INTO WORDLIST (english, korean, swedish, synonyms, example)
VALUES (?,?,?,?,?);"
)){
echo "prepared ".$mysqliPrepare;
mysqli_smt_bind_param($mysqliPrepare,"s",$addEnglish,$addKorean,$addSwedish,$addSynonyms,$addExample);
mysqli_smt_execute($mysqliPrepare);
mysqli_stmt_bind_result($mysqliPrepare, $district);
mysqli_stmt_fetch($mysqliPrepare);
mysqli_stmt_close($mysqliPrepare);
}
print_r("</br>error ".mysqli_stmt_error($mysqliPrepare));
print_r("</br>error ".mysqli_stmt_error($conn));
print_r("</br>error ".$mysqliPrepare);
}
}else{
echo "search again";
}
connectDB function is from db_connection.php
<?php
function connectDB($conn){
$connectStatus = null;
if (!$conn) {
$connectStatus = false;
die("Connection failed: " . $conn->connect_error);
}else{
echo "Connected successfully";
$connectStatus = true;
}
return $connectStatus;
}
?>
I tried SQL query in phpmyadmin and it worked there, but in php script it seems like SQL query or mysqli_prepare function is buggy because inside if statement is just never triggered. I mean I don't see message "prepared" which php should show. The table doesn't want to be created too.
That's why I did some mysqli_stmt_error to see what error do I get, but
I get such errors, so I cannot even get mysql error message.. it seems like
if($mysqliPrepare=mysqli_prepare($conn,
"CREATE TABLE IF NOT EXISTS WORDLIST(
wordIndex INT(10) AUTO_INCREMENT,
english VARCHAR(20),
korean VARCHAR(20),
swedish VARCHAR(20),
synonyms TEXT,
example VARCHAR(100),
UNIQUE KEY wordDBIndex (wordIndex)
);
INSERT INTO WORDLIST (english, korean, swedish, synonyms, example)
VALUES (?,?,?,?,?);"
))
problem is inside in this code but I can't see any problem with it because same SQL code works perfectly in phpmyadmin. I even tried '?' instead ? but doesn't work.