Hello everyone, good night.
I'm making a database for ecommerce called "ecommerce".
But when I was connecting the database (msqli_connect.php) with the add_artist.php file, I get errors.
My question is: How can I fix them?
I greatly appreciate your help.
Ps.: The source code and the image are attached.
add_artist.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add an Artist</title>
<meta charset="UTF-8">
<meta name="description" content="Home page">
<meta name="keywords" content="ecommerce">
<meta name="author" content="José Moreira">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php # Script 19.1 - add_artist.php
// This page allows the administrator to
add an artist.
if ($_SERVER['REQUEST_METHOD'] = = 'POST')
{ // Handle the form.
// Validate the first and middle
names (neither required):
$fn = (!empty($_POST['first_name'])) ?
trim($_POST['first_name']) : NULL;
$mn = (!empty($_POST['middle_name'])) ?
trim($_POST['middle_name']) : NULL;
// Check for a last_name...
if (!empty($_POST['last_name'])) {
$ln = trim($_POST['last_name']);
// Add the artist to the database:
require ('../../mysqli_connect.
php');
$q = 'INSERT INTO artists (first_
name, middle_name, last_name)
VALUES (?, ?, ?)';
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt,
'sss', $fn, $mn, $ln);
mysqli_stmt_execute($stmt);
// Check the results....
if (mysqli_stmt_affected_
rows($stmt) = = 1) {
echo '<p>The artist has been
added.</p>';
$_POST = array( );
} else { // Error!
$error = 'The new artist could not be added to the database!';
}
// Close this prepared statement:
mysqli_stmt_close($stmt);
mysqli_close($dbc); // Close the database connection.
} else { // No last name value.
$error = 'Please enter the artist\'s name!';
}
} // End of the submission IF.
// Check for an error and print it:
if (isset($error)) {
echo '<h1>Error!</h1>
<p style="font-weight: bold; color: #C00">' . $error . ' Please try again.</p>';
}
// Display the form...
?>
<h1>Add a Print</h1>
<form action="add_artist.php" method="post">
<fieldset><legend>Fill out the form to add an artist:</legend>
<p><b>First Name:</b> <input type="text" name="first_name" size="10" maxlength="20"
value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p><b>Middle Name:</b> <input type="text" name="middle_name" size="10" maxlength="20"
value="<?php if (isset($_POST['middle_name'])) echo $_POST['middle_name']; ?>" /></p>
<p><b>Last Name:</b> <input type="text" name="last_name" size="10" maxlength="40" value="<?php
if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
</form>
</body>
</html>
msqli_connect.php
<?php # Script 9.2 - mysqli_connect.php
// This file contains the database access information.
// This file also establishes a connection to MySQL,
// selects the database, and sets the encoding.
// Set the database access information as constants:
DEFINE ('DB_USER', 'username'); DEFINE ('DB_PASSWORD', 'Quencyjones79');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'ecommerce');
// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to
MySQL: ' . mysqli_connect_error( ) );
// Set the encoding...
mysqli_set_charset($dbc, 'utf8');