Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

mysql.php DEFINE(\'DB_USER\', \'wp_eatery\'); DEFINE(\'DB_PSWD\', \'password\');

ID: 3874518 • Letter: M

Question

mysql.php

DEFINE('DB_USER', 'wp_eatery');

DEFINE('DB_PSWD', 'password');

DEFINE('DB_HOST', 'localhost');

DEFINE('DB_NAME', 'wp_eatery');

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);

if(!$dbcon){

die('error connecting to database');

}

echo 'you have connected successfully'

?>

contract.php

include('mysql.php');

if($_SERVER["REQUEST_METHOD"] == "POST"){

$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$phoneNumber = $_POST['phoneNumber'];

$emailAddress = $_POST['emailAddress'];

$username = $_POST['username'];

$referral = $_POST['referral'];

$query = "INSERT INTO mailingList(firstName, lastName, phoneNumber, emailAddress, username, referral) VALUES ('$firstName', '$lastName', '$phoneNumber', '$emailAddress', '$username', '$referral')";

if( !mysqli_query($dbcon,$query)){

die('error');

}

}

I get you have connected successfully error

why is that, what is wrong in code, make it right

Explanation / Answer

READ THE FOLLOWING LINES CAREFULLY......

There is nothing wrong with your code, just make sure your html form that is connecting to the connect.php file, is like this.

The reason for error may be that, the query was not executing properly.

the data that you were getting from the html document, could be of length more than the attribute domain size in the DATABASE.

Another reason could be, in mySql database all the table names are of lowecase implicitly. But in the code you had table name mailingList. In mysql, even if you name a table in uppercase, by default it will convert that into lower case.

The following code will work surely.

I have highlighted where i have changed the table name.

connect.html

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<form action = "connect.php" method="POST">

firstName <input type="text" name="firstName"><br/>

lastName <input type="text" name="lastName"><br/>

phoneNumber <input type="number" name="phoneNumber"><br/>

emailAddress <input type="email" name="emailAddress"><br/>

username <input type="text" name="username"><br/>

referral <input type="text" name="referral"><br/>

<input type="submit">

</form>

</body>

</html>

mysql.php

<?php

DEFINE('DB_USER', 'wp_eatery');

DEFINE('DB_PSWD', 'password');

DEFINE('DB_HOST', 'localhost');

DEFINE('DB_NAME', 'wp_eatery');

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);

if(!$dbcon){

die('error connecting to database');

}

echo 'you have connected successfully'

?>

Connect.php

<?php

include('mysql.php');

if($_SERVER["REQUEST_METHOD"] == "POST"){

$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$phoneNumber = $_POST['phoneNumber'];

$emailAddress = $_POST['emailAddress'];

$username = $_POST['username'];

$referral = $_POST['referral'];

$query = "INSERT INTO mailinglist(firstName, lastName, phoneNumber, emailAddress, username, referral) VALUES ('$firstName', '$lastName', '$phoneNumber', '$emailAddress', '$username', '$referral')"; //Table name changed.. just replaced 'mailingList' by 'mailinglist'

if(!mysqli_query($dbcon,$query)){

die('error');

}

}

?>