0

I have a problem with inserting array values into the DB table, which I defined 4 arrays and I'm trying to Insert these arrays values using for loop, and I got this Error:

error

Here is My Code:

<?php

$servername = "localhost";
$username = "root";
$password = "12345678";
$db_name = "blooddonation";

//////////////////////*********** Blood Donation System **************//////////////////////


//Create Connection
$conn = new mysqli($servername, $username, $password, $db_name);
//Check Connection
if($conn -> connect_error){
    die("Connection falied :(". $conn -> connect_error."<br>");
}else {
    
    echo "Connection Success! :)"."<br>";
    
    //Insert patient's Info
    $Names = array("Ahmad", "Abdullah", "Hashim", "Rahaf", "Jood", "worood", "Heba", "Hamza", "Fatima", "Danea");
    
    $Hospital = array("Istishari Hospital", "Ibn AlHaytham Hospital", "Islamic Hospital", "Palestine Hospital",
                      "Istiklal Hospital", "Jordan Hospital", "Al Khalidi Hospital", "Amman Surgical Hospital", 
                      "Shmaisani Hospital", "Al-Essra Hospital");
                      
    $bType = array("AB+", "O-", "A-", "B+", "AB-", "B-", "A+", "O+", "AB+", "B+");
    
    $Status = array("Emergency", "Normal", "Normal", "Emergency", "Emergency", "Normal", "Normal", "Normal", "Emergency","Normal");
    $Pass = "12345678";

    for($i = 0; $i < 10; $i++){
        
        
        $sql1 = "INSERT INTO patients (name, password, hospital, bloodType, status) VALUES ($Names[$i], $Pass, $Hospital[$i], $bType[$i], $Status[$i])";        
        if($conn -> multi_query($sql1) == TRUE){
            
            echo "Patient's Info Inserted Successfuly :)"."<br>";
        }else{
            
            echo "Error: ".$sql1."<br>".$conn -> error."<br>";
        }
    }//End of for loop

}


//Close Connection
$conn -> close();


?>

so how can I solve this problem?

2 Answers2

2

You are having errors because your string values are not enclosed with quotation marks.

Another thing to consider is the use of prepared statements. Try following this guide: https://www.w3schools.com/php/php_mysql_prepared_statements.asp

You should also consider inserting your array values in bulk instead of 1 by 1. Here's a good article you can refer to: PDO Prepared Inserts multiple rows in single query

Algef Almocera
  • 759
  • 1
  • 6
  • 9
1

You can try prepared statements in Php, very usefull. It will be something like this:

$bdd = new PDO('mysql:host='.$servername.';dbname='.$db_name.';charset=utf8mb4', $username, $password, [
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
]);
$req = $bdd->prepare('INSERT INTO patients (name, password, hospital, bloodType, status) VALUES(:name, :password, :hospital, :bloodtype, :status)');
$req->execute(array(
    'name' => $Names[$i],
    'password' => $Pass,
    'hospital' => $Hospital[$i],
    'bloodtype' => $bType[$i],
    'status' => $Status[$i]
));
Dharman
  • 30,962
  • 25
  • 85
  • 135
Romain Dereux
  • 119
  • 2
  • 5