//connect to the database $conn = mysqli_connect($servername, $username, $password, $dbname); //get the form data $category_name = $_POST['category_name']; $category_description = $_POST['category_description']; //SQL query to insert or update data $sql = "INSERT INTO categories (category_name, category_description) VALUES ('$category_name', '$category_description') ON DUPLICATE KEY UPDATE category_description = '$category_description'"; //execute the query if(mysqli_query($conn, $sql)) { echo "Category saved successfully!"; } else { echo "Error: " . mysqli_error($conn); } //close the database connection mysqli_close($conn);
//connect to the database $client = new MongoDB\Client($uri); //get the form data $category_name = $_POST['category_name']; $category_description = $_POST['category_description']; //select the collection $collection = $client->database->categories; //create the document $category = [ 'category_name' => $category_name, 'category_description' => $category_description ]; //insert or update the document $result = $collection->updateOne( ['category_name' => $category_name], ['$set' => $category], ['upsert' => true] ); if($result->getUpsertedCount()) { echo "Category saved successfully!"; } else { echo "Error: " . $result->getWriteErrors()[0]->getMessage(); }This example uses MongoDB database to save or update category data. The code connects to the database, gets the form data, and then inserts or updates the document in the "categories" collection. The `upsert` option is used to update the document if it already exists in the collection. Package/library used: mongodb-php-library