Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

PHP code not connecting to mysql. [closed]

+0
−4

Closed as unclear by Alexei‭ on Sep 24, 2022 at 04:48

This question cannot be answered in its current form, because critical information is missing.

This question was closed; new answers can no longer be added. Users with the reopen privilege may vote to reopen this question if it has been improved or closed incorrectly.

I am working on some php for a class and I have never really worked with php before. I am trying to connect to a database that I'm hosting on aws and it seem that the mysqli object isn't being initialized properly since nothing is being echoed after that line.

The goal is to send the field values into the database table hosted on aws but it just gives a blank page when pressing submit instead of echoing anything useful. There no exceptions being caught either and php -l fileName shows no errors.

All/most of this code is from google so I am not sure if this is completely correct (obviously something is not right)

<html>
<body>

<?php

function exceptions_error_handler($severity, $message, $filename, $lineno) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
}

set_error_handler('exceptions_error_handler');



$servername = "ec2-44-211-67-109.compute-1.amazonaws.com";
$username = "matt";
$password = "notMyRealPassword";
$dbname = "mydb";

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

    // collect value of input field
    $title = $_REQUEST['title'];
    $author = $_REQUEST['author'];
    $genre = $_REQUEST['genre'];
    $isbn = $_REQUEST['isbn'];
    $summary = $_REQUEST['summary'];
}
echo $title;

try {
    $conn = new mysqli($servername, $username, $password, $dbname);
    echo $title;
    echo "testecho";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Check connection
if ($conn->connect_error) {
echo "conncet er ";
    die("Connection failed: "
        . $conn->connect_error);
}
 
echo "test";
$sql = "INSERT INTO books (title, author, genre, isbn, summary) VALUES ({$title}, {$author}, {$genre}, {$isbn}, {$summary});";
if ($conn->query($sql) === TRUE) {
    echo "record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
 
// Closing the connection.
$conn->close();
?>

</html>
</body>

This is the corresponding html

<form action="welcome.php" method="post">
  <div class="container">
    <label for="title"><b>Book Title</b></label>
    <input type="text" placeholder="Enter Title" name="title" required>

    <br></br>

    <label for="author"><b>Author</b></label>
    <input type="text" placeholder="Enter Author" name="author" required>

    <br></br>

    <label for="genre"><b>Genre<b></label>
    <input type="text" placeholder="Enter Genre" name="genre" required>
    
    <br></br>

    <label for="isbn"><b>ISBN</b></label>
    <input type="number" placeholder="Enter ISBN" name="isbn" required>

    <br></br>

    <label for="summary"><b>Summary</b></label>
    <input type="text" placeholder="Enter Summary" name="summary" required>

    <br></br>

    <button type="submit">Submit</button>
  </div>
</form>
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

Error reporting and reachability (4 comments)
Security (4 comments)
Reduce to a minimal (non)working example (2 comments)

0 answers