AppInventor: Put Data ke MySQL
To update (PUT) data in a MySQL server from an App Inventor 2 app, you also need to use a web service (typically a server-side script) as an intermediary. The server-side script will handle the update operation and communicate with your App Inventor app. 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 put_data.php on your server:
php Copy code <?php // Retrieve data from the POST request $id = $_POST['id']; $newName = $_POST['newName']; $newEmail = $_POST['newEmail'];
// 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);
}
// Update data in the database $sql = "UPDATE your_table_name SET name='$newName', email='$newEmail' WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "Data updated 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. Additionally, use TextBox components or similar to allow users to input data.
App Inventor Blocks:
Here's a basic set of blocks to send a PUT request with data:
Adjust the URL to point to your deployed PHP script.
Testing:
Test your app by entering data in the text boxes, providing the ID of the record to update, and clicking the "Update" button. Check your MySQL database to see if the data has been successfully updated.
As with the previous examples, remember to implement appropriate error handling, data validation, and security measures in a real-world scenario. Also, consider using HTTPS for secure data transmission.