Pregunta

I'm simply writing some values to a MySQL database using the following snippets of code.

Please note, when I run SELECT queries, information is correctly displayed using echo etc.

However when I try to write to the database using INSERT, the code executes with no errors, but when I check my database there is nothing written to it.

 private $db;

 function __construct() {
 $this->db = new mysqli('localhost', 'xyz', '123', 'testdb');
 $this->db->autocommit(FALSE);

if (isset($_POST["ID"]) && isset($_POST["Number"]) && isset($_POST["Address"]) && isset($_POST["Ticket"])) {

$id= $_POST["ID"];
$number= $_POST["Number"];
$address= $_POST["Address"];
$ticket= $_POST["Ticket"];

$stmt = $this->db->prepare("INSERT INTO tickets (ID, Number, Address, Ticket) VALUES (?,?,?,?)");
$stmt->bind_param("iiss", $id, $number, $address, $ticket);
$stmt->execute(); 
$stmt->close()

return true
}

Any help would be greatly appreciated.

Edit

Question - Could it be something to do with write access to the database? I've checked the user account privileges and it has all privileges.

¿Fue útil?

Solución

When using the insert auto_commit must be TRUE.

$this->db->autocommit(TRUE);

Otros consejos

You've disabled autocommitting ($this->db->autocommit(FALSE);), and you're not committing anywhere explicitly. Once you close the connection, anything you inserted will be rollbacked, as if you never changed the database. Once you're done inserting, you should add a call to $this->db->commit().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top