Forum Stats

  • 3,826,348 Users
  • 2,260,632 Discussions


Oracle, PHP, insert problem

user545194 Member Posts: 605 Bronze Badge
edited Nov 25, 2013 9:00AM in PHP


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">


    $("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');



            type: "POST",

            url: "ajax.php",

            data: "title="+title + "&fname="+ fname + "&lname=" + lname,

            success: function(){


                //$('form#submit :input').val("");




    return false;






        <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="">


                <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>



        <div class="success" style="display:none;">

            <p>Client has been added.</p>






<!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.




        <meta charset="UTF-8">








$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() );




if (!$stmt2) {   

    $e = oci_error($conn);

    trigger_error(htmlentities($e['message']), E_USER_ERROR);





There is no error message, but also no new record in the table.

Thanks for your help!


  • JuliusZ
    JuliusZ Member Posts: 29
    edited Nov 25, 2013 9:01AM


    $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:

This discussion has been closed.