I am trying to insert records into an Oracle database using PHP, but for some reason, the INSERT code is not working.
--DB Table
CREATE TABLE USER (
ID NUMBER(20) GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
ROLE NUMBER(10),
NAME VARCHAR2(50),
PASSWORD VARCHAR2(50),
);
The connection string works:
#PHP: New Oracle Database Connection (db.php):
<?php
$user = "test";
$password = "test123";
$host = "localhost/XE";
$connection = oci_connect($user, $password, $host)
or die(oci_error());
if(!$connection){
echo "Failed: Verify the connection string in db.php";
}else{
//echo "CONNECTED";
}
oci_close ($connection);
?>
This is the insert in PHP that does not work, Could you help me to identify the error?
#PHP+Oracle: INSERT new user (userControllerOracle.php)
<?php
#DB Connection:
include("db.php");
#DB Oracle INSERT:
if (isset($_POST['registro'])) {
$name = $_POST['name'];
$password = $_POST['password'];
$cnn = oci_connect($user, $password, $host);
$query = "INSERT INTO USER (1, name, password) VALUES (1,'$name', '$password')";
}
?>
Previously, I used MySQL and it worked, but I don't know how to achieve the same result in Oracle
#PHP: MySQL Database Connection (db.php):
<?php
$con = mysqli_connect("localhost","test","test123","test");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function debug_to_console($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
?>
#PHP+MySQL: INSERT new user (userControllerMySQL.php)
<?php
#DB Connection:
include("php/db.php");
#DB MySQL INSERT:
if (isset($_POST['registro'])) {
if (strlen($_POST['name']) >= 1 && strlen($_POST['password']) >= 1) {
$name = trim($_POST['name']);
$password = trim($_POST['password']);
$query = "INSERT INTO USER(1, name, password) VALUES (1,'$name', MD5('$password')";
$result = mysqli_query($con,$query);
if ($result) {
?>
<h5 style="text-align: center; background-color:#28a745; color:white">User added!</h5>
<?php
} else {
?>
<h5 style="text-align: center; background-color:#dc3545; color:white">error!</h5>
<?php
}
} else {
?>
<h5 style="text-align: center; background-color:#ffc107; color:#343a40">add all fields!</h5>
<?php
}
}
?>