Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Oracle, PHP, insert problem

Hi,
Environment: Windows 7, Oracle 11gR2 DB, PHP version 5.4
I am trying to insert record into the database via html form and php, but no record is inserted. Here is the code:
html form:
...
<script type="text/javascript">
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var title = $('#title').attr('value');
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "title="+title + "&fname="+ fname + "&lname=" + lname,
success: function(){
$('form#submit').hide();
//$('form#submit :input').val("");
$('div.success').fadeIn();
}
});
return false;
});
});
</script>
</head>
<body>
<div id="wrap">
<h2>Fill out the form below, the form will submit via Ajax</h2>
<h3>This method can be used on various projects for clients</h3>
<br />
<form id="submit" method="post" name="submit" action="">
<fieldset>
<legend>Enter Information</legend>
<label for="title">Title:</label>
<br />
<input type="text" name="p_title" id="title" class="text" size="20" />
<br />
<label for="fname">Client First Name:</label>
<br />
<input type="text" name="p_fname" id="fname" class="text" size="20" />
<br />
<label for="lname">Client Last Name:</label>
<br />
<input type="text" name="p_lname" id="lname" class="text" size="20" />
<br /><br />
<button type="submit" class="button positive"> <img src="icons/tick.png" alt=""/> Add Client </button>
</fieldset>
</form>
<div class="success" style="display:none;">
<p>Client has been added.</p>
</div>
</div>
</body>
</html>
php:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
include_once('includes/db2.php');
// CLIENT INFORMATION
$title = htmlspecialchars(trim($_POST['p_title']));
$fname = htmlspecialchars(trim($_POST['p_fname']));
$lname = htmlspecialchars(trim($_POST['p_lname']));
// Bind the value into the parsed statement.
oci_bind_by_name($stmt2, ":p_title", $title);
oci_bind_by_name($stmt2, ":p_fname", $fname);
oci_bind_by_name($stmt2, ":p_lname", $lname);
$stmt = oci_parse($conn, "select new_cus_seq.NEXTVAL from dual");
oci_execute($stmt, OCI_DEFAULT);
$val = oci_result($stmt, 'NEXTVAL');
$stmt2 = oci_parse($conn, "insert into CUS_CUSTOMERS (cus_id, salutation, first_name, last_name) values('$val', :p_title, :p_fname, :p_lname)");
oci_execute($stmt2, OCI_DEFAULT);
if (! $conn ) {
echo "Unable to connect: " . var_dump( oci_error() );
die();
}
oci_commit($conn);
if (!$stmt2) {
$e = oci_error($conn);
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
?>
</body>
</html>
There is no error message, but also no new record in the table.
Thanks for your help!
Answers
-
Hi,
$stmt2 = oci_parse($conn, "insert into CUS_CUSTOMERS (cus_id, salutation, first_name, last_name) values('$val', :p_title, :p_fname, :p_lname)");
you are trying to insert the string '$val' not the variable $val to cus_id. You should bind the variable like you did with :p_title, :p_fname, :p_lname.
Like this:
$stmt2 = oci_parse($conn, "insert into CUS_CUSTOMERS (cus_id, salutation, first_name, last_name) values(:p_cus_id, :p_title, :p_fname, :p_lname)"); oci_bind_by_name($stmt2, ":p_cus_id", $val); oci_bind_by_name($stmt2, ":p_title", $title); oci_bind_by_name($stmt2, ":p_fname", $fname); oci_bind_by_name($stmt2, ":p_lname", $lname);
Note, that binding should be done after oci_parse().
Also, always remember to close the connections and free the resources when you don't need them anymore by:
oci_free_statement($stmt); oci_free_statement($stmt2); oci_close($conn);