简体   繁体   中英

Where should my PHP file live if I'm using AWS RDS to store user input into database?

Before using Amazon RDS, I'd test my user input locally and my endpoint ( http://localhost/emails.php ) that of which would live inside my htdocs folder (I'm using MAMP) so I'd be able to store user input in my SQL db via text field without problems.

But now that I've moved onto Amazon RDS, I've gotten a new endpoint that successfully connects to MySQL Workbench.

My question is - Where am I supposed to store this php file that contains all the information to connect to my database, sql table, etc? I want it to be able to work like how it worked locally before using RDS

I have code below for everything I just mentioned:

Here's my js file:

import React, {Component} from 'react';
import axios from 'axios';

export default class ProductList extends Component {
    constructor(props) {
        this.addFormData = this.addFormData.bind(this);
    }

    addFormData(evt) {
        evt.preventDefault();
        const fd = new FormData();
        fd.append('myEmail', this.refs.myEmail.value);
        var headers = {
            'Content-Type': 'application/json;charset=UTF-8',
            // 'Host' : 'sdb.amazonaws.com'
            "Access-Control-Allow-Origin": "*"
        };
        axios.post("My AWS RDS Endpoint", fd, headers)
        .then(res => {
            alert(res);
        }).catch(err => {
            alert(err);
    });
    }

    render() {
        return (
            <React.Fragment>
                    <form>
                    <div className="col-xs-2 text-center">
                    <input type="email" id="Email" aria-describedby="emailHelp" placeholder="Enter email" ref="myEmail" />
                </div>
                    <button type="submit" className="submitEmail btn btn-primary" onClick={this.addFormData}>Submit</button>
                </form>
                </div>
            </React.Fragment>
        );
    }
}

here's my php file:

<?php
header("Access-Control-Allow-Origin: *");

$servername = "my host";
$username   = "my name";
$password   = "my pw";
$dbname     = "mydb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO mydb.emails
VALUES ('".$_POST['myEmail']."')";

echo $sql;

if (mysqli_query($conn,$sql)) {
    $data = array("data" => "You Data added successfully");
    echo json_encode($data);
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

?>

Here's my SQL query to create the table that holds emails:

CREATE TABLE mydb.emails(emails VARCHAR(200)); 
SELECT * FROM mydb.emails;

http://localhost/ *.php on your local system would be in your root folder on your host -- either htdocs, public or www.

axion.post() should be pointing to that .php file, not to the AWS endpoint.

It might be safer to put your RDS credentials in a separate file in a folder below your public folder in case for some reason there's an error that displays your PHP in plaintext. At least if you 'require' the file, you won't accidentally expose your credentials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM