Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. I'm kinda new into MySQL and PHP. but im in this challenge and im slowly getting things done.

I was about to test the website and I get this error:
Fatal error: Uncaught TypeError: mysqli_connect(): Argument #1 ($hostname) must be of type ?string, array given in C:\xampp\htdocs\scandiweb-test-assignment\connect.php:8 Stack trace: #0 C:\xampp\htdocs\scandiweb-test-assignment\connect.php(8): mysqli_connect(Array, Array, Array, Array) #1 C:\xampp\htdocs\scandiweb-test-assignment\index.php(3): include('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\scandiweb-test-assignment\connect.php on line 8


connect.php
<?php

$host =['localhost']; // name of the host
$dbusername =['root'];   // database user name
$password =[''];   // database password // I added password
$dbname =['test']; // database name
$tblname =['products']; // table of products

$conn = mysqli_connect('$host','$dbusername','$password','$dbname'); // connection 

if(!$conn->connect_error){
    die('Could not Connect MySql Server: '.$conn->connect_error );
}
?>


add.php
<?php
// declare 
$sku = $_POST['sku'];
$name = $_POST['name'];
$price = $_POST['price'];
$productType = $_POST['productType'];
$size = $_POST['size'];
$weight = $_POST['weight'];
$height = $_POST['height'];
$width = $_POST['width'];
$length = $_POST['length'];

// connect to database
include "connect.php";
//insert items to db
$sql = "INSERT INTO products(sku, name, price, productType, size, weight, height, width, length) values('$sku','$name','$price','$productType','$size','$weight','$height','$width','$length')";

if ($conn->query($sql) === TRUE) {
	//echo "ADDED: " . $sku . ", " . $name . ", " . $price, ".$productType.", ".$size.", ".$weight.", ".$height.", ".$width.", ".$length.";		
} else {
	echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
header("Location: ./index.php", TRUE, 301);
exit();


What I have tried:

I've tried changing the code several times, tried a lot of things i got from google and it didn't work (yet)
I know i need to keep reading the manual but it's basic chinese to me. It helps me a lot when i follow tutorials from youtube, but sometimes i dont find my specific error so i don't get an explanation of what's happening (a translated explanation, to me, a beginner)
Posted
Updated 22-Apr-22 5:20am
v2
Comments
CHill60 22-Apr-22 5:20am    
I am not going to follow that link off-site. Post your code here, as code not a photo
Chris Copeland 22-Apr-22 6:33am    
I agree, I won't be following the link either. A picture is no good if we want to copy + paste problematic parts of the code to provide guidance. That being said, I think the error explains itself, you're passing an array into the mysqli_connect parameters when that function does not expect any array values at all.
22bears 22-Apr-22 11:17am    
Hello Chris Copeland. Please take a look, I just posted 2 parts of the code
22bears 22-Apr-22 11:20am    
Okay so I just updated the post (sorry about the image, now i understand how this website works)
Chris Copeland told me mysqli_connect is a function that doesn't expect any array values..
So which parameter should I use? I tried with $conn = new mysqli" but it didn't work either... So i updated the question above and posted the codes

1 solution

PHP
$host =['localhost']; // name of the host

You are declaring $host to be an array containing a single string. Remove the square brackets.
 
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