AppInventor: Post Data ke MySQL

From OnnoWiki
Jump to navigation Jump to search

To post data to a MySQL server from an App Inventor 2 app, you need to use a web service (typically a server-side script) as an intermediary. Here's a simple example using PHP as the server-side script:

Create a PHP script on your server:

Save the following PHP script as post_data.php on your server:

<?php
// Retrieve data from the POST request
$name = $_POST['name'];
$email = $_POST['email'];

// Database connection parameters
$servername = "your_mysql_server";
$username = "your_mysql_username";
$password = "your_mysql_password";
$dbname = "your_database_name";

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

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

// Insert data into the database
$sql = "INSERT INTO your_table_name (name, email) VALUES ('$name', '$email')";

if ($conn->query($sql) === TRUE) {
    echo "Data inserted successfully";
} else {
    echo "Error: " . $sql . "
" . $conn->error; } $conn->close(); ?>

Replace placeholders like your_mysql_server, your_mysql_username, your_mysql_password, your_database_name, and your_table_name with your actual MySQL server details.

App Inventor Components:

In your App Inventor project, you'll need to use the Web component to send HTTP POST requests. Also, use TextBox components or similar to allow users to input data.

App Inventor Blocks:

Here's a basic set of blocks to send a POST request with data:


Adjust the URL to point to your deployed PHP script.

Testing:

Test your app by entering data in the text boxes and clicking the "Submit" button. Check your MySQL database to see if the data has been successfully inserted.

Remember that this is a simplified example. In a real-world scenario, you would want to add more error handling, data validation, and possibly implement security measures to prevent SQL injection. Additionally, transmitting sensitive data should be done over HTTPS for security reasons.