I’m making an app where the user enters data, this data is inserted into a db, and displayed later for other user. The problem is that I don’t manage to insert data into the db. I’ve already made an app with a database, but now it doesn’t work. The worst is that I don’t get any errors so it connects successfully to the db, it just doesn’t insert into the db. The data is sent to the php script from an html form with a POST method.
Here is my code:
<?php $DATABASE_HOST = 'localhost'; $DATABASE_USER = 'root'; $DATABASE_PASS = ''; $DATABASE_NAME = 'mydb'; $con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME); if (mysqli_connect_errno()) { exit('Failed to connect to MySQL: ' . mysqli_connect_error()); } $stmt = $con->prepare("INSERT INTO matches (username, uid, level, timestamp) VALUES (?, ?, ?, ?)"); $timestamp = time(); $stmt->bind_param('sssi', $_POST['username'], $_POST['uid'], $_POST['level'], $timestamp); $stmt->close(); $con->close(); ?>
Here is some SQL statements to replicate my db:
-- phpMyAdmin SQL Dump -- version 5.0.2 -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1:3306 -- Generation Time: Apr 06, 2021 at 07:18 AM -- Server version: 5.7.31 -- PHP Version: 7.3.21 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; /*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; /*!40101 SET @[email protected]@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `mydb` -- -- -------------------------------------------------------- -- -- Table structure for table `matches` -- DROP TABLE IF EXISTS `matches`; CREATE TABLE IF NOT EXISTS `matches` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `uid` varchar(255) NOT NULL, `level` varchar(100) NOT NULL, `timestamp` timestamp(6) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; COMMIT; /*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; /*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; /*!40101 SET [email protected]_COLLATION_CONNECTION */;
Could someone help me, I’ve been looking for the issues for near two days and it’s driving me mad !๐
Answer
If You add
$stmt->execute();
then it should work or show You and error ๐
Basically You are preparing query but the query is not executed because You are closing the connection