This discussion is archived
1 Reply Latest reply: Nov 25, 2013 6:01 AM by JuliusZ RSS

Oracle, PHP, insert problem

user545194 Newbie
Currently Being Moderated

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!

  • 1. Re: Oracle, PHP, insert problem
    JuliusZ Newbie
    Currently Being Moderated

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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points