jspc
asked on
HTML file with PHP File
Hello,
I have a HTML "Contact" page.
When you click Submit it uses a contact.php file to write to a database.
Currently I am getting this error. It relates to adding the next ID automatically but I can't seem to get it working.
Is anyone able to assist please?
Here is the error:
My PHP file is below.
My HTML file is below
I have a HTML "Contact" page.
When you click Submit it uses a contact.php file to write to a database.
Currently I am getting this error. It relates to adding the next ID automatically but I can't seem to get it working.
Is anyone able to assist please?
Here is the error:
My PHP file is below.
<!DOCTYPE HTML>
<html>
<head>
<title>Contact</title>
<meta charset="utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
/* Set oracle user login and password info */
$dbuser = ;
$dbpass = ;
$db = ;
$connect = oci_connect($dbuser, $dbpass, $db);
/* Display connection error if fails */
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
//Extract CGI variables
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comment3=$_POST['comment3'];
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query_count);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
if (oci_fetch_array($stmt)) {
$count = oci_result($stmt,1);//returns the data for column 1
echo "<strong><font color=red>* * The below SQL INSERT INTO Statement is displayed for the purpose of Assignment 2-2 to verify Form data collected has been written to the SQL Tables * *</font></strong><br><br>";
} else {
echo "An error occurred in retrieving order id.\n";
exit;
}
$count++;
/* echo $count."</br>"; */
// Create the SQL statement to add the data
$query = "INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, comment3) VALUES ($count, '$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3')";
echo $query."</br></br>";
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query);
//echo "SQL: $query<br>";
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query_count);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
if (oci_fetch_array($stmt)) {
$product = oci_result($stmt,1);//returns the data for column 1
// echo $product."</br>";
} else {
echo "An error occurred in retrieving order id.\n";
exit;
}
//$product++;
//display a receipt with all the order details
echo ("<p>====================== <strong>EMAIL RECEIPT</strong> =============================</p>");
echo $date = date('d-m-y H:i:s');
echo ("<p><h2><font color=blue>Thank you for contacting us.</font></h2></p>");
echo ("<p><strong>First Name: </strong>".$firstname. "</p>");
echo ("<p><strong>Last Name: </strong>".$lastname. "</p>");
echo ("<p><strong>Address: </strong>".$address. "</p>");
echo ("<p><strong>Phone: </strong>".$phone. "</p>");
echo ("<p><strong>Email: </strong>".$email. "</p>");
echo ("<p><strong>Subject: </strong>".$subject. "</p>");
echo ("<p><strong>Comments: </strong>".$comment3. "</p>");
echo ("<p>====================== END OF RECEIPT =============================</p>");
// Close the connection
oci_close($connect);
?>
</body>
</html>
My HTML file is below
<!DOCTYPE html>
<html lang="en">
<head>
<style>
h4 {
background-color: #e1e1d0;
padding: 5px;
}
#Footer
{border: 1px solid black;
padding: 5px;
text-align: center;
}
input, textarea {margin-top: 10px;
margin-bottom: 10px;
padding: 5px;
width: 40%;
}
#submit {margin-left: 5px;
padding-right: 12px;
}
#Menu {background-color: #b8bac6;
padding: 20px;}
</style>
<title>BooksRUS</title>
<link rel="stylesheet" href="styles2.css" type="text/css">
<meta charset=utf-8>
</head>
<body>
<div id="Container">
<div id="Header"><h1>BooksRUS</h1>
<h3>Best Secondhand Book Shop in Geelong</h3></div>
<div id="Menu"><b><a href="index.html">Home</a> | <a href="books.html">Books</a> | <a href="orderbooks.html">Order Books</a> | <a href="faq.html">FAQ</a> | <a href="contact.html">Contact</a></b>
</div>
<div id="MainBody"></div>
<p><h4 style="Color:#3498DB">Contact Information</h4>
123 Moorabool Street
<br>
Geelong VIC 3220
<br>
Tel: 03 5244 1234
<br>
Email: <a href="mailto:contact@booksrus.com.au">contact@booksrus.com.au</a>
<p><h4 style="Color:#3498DB">Trading Hours</h4>
<p>Our business trading hours are: 9:00am to 6:00pm Monday to Sunday</p>
<form action="contact.php" method="post" name="contact" id="contactus">
<fieldset>
<legend>Contact Us:</legend>
First Name: <br><input type="text" name="firstname"><br>
Last Name: <br><input type="text" name="lastname"><br>
Address: <br><input type="text" name="address"><br>
Phone: <br><input type="text" name="phone"><br>
Email:: <br><input type="email" name="email"><br>
Subject: <br><input type="subject" name="subject"><br>
Comments: <br><textarea name="comment3"></textarea>
</fieldset>
<div id="submit">
<input type="submit" value="Submit">
<input type="reset" value="Clear the Form">
</div><br>
</form>
</body>
</html>
ASKER
I'm wanting the next Primary ID to be automatically populated - that's what the query_count is but not sure I have the correct idea in place.
OK. If your DB has an auto-increment primary key, then you don't need to request the ID from the database. You simply insert a record without an ID and it will automatically be given it by the DB
If that's the case, then you can remove lines 44-65 and 84-105. You would also need to remove the ID column and $count value from the query on line 66:
$query = "INSERT INTO Contact (firstname, lastname, address, phone, email, subject, comment3) VALUES ('$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3')";
If you don't have an auto-incrementing ID, then you'll need to send a query to your database to get one. Depends on your scheme, but something like this before line 44:
$query_count = "SELECT Max(id) from yourTable";
If that's the case, then you can remove lines 44-65 and 84-105. You would also need to remove the ID column and $count value from the query on line 66:
$query = "INSERT INTO Contact (firstname, lastname, address, phone, email, subject, comment3) VALUES ('$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3')";
If you don't have an auto-incrementing ID, then you'll need to send a query to your database to get one. Depends on your scheme, but something like this before line 44:
$query_count = "SELECT Max(id) from yourTable";
I would let the database handle that. Just use an IDENTITY (SQL Server) or AUTO_INCREMENT (MySQL) and let the db assign next ID. If you need the ID back, you can get that easily.
Apologies, Chris, starting typing before I saw your comment.
No worries Kevin :)
FYI - He's using an Oracle DB, so not sure what the identity columns are in that ??
FYI - He's using an Oracle DB, so not sure what the identity columns are in that ??
Ah, yes, I believe ORACLE uses SEQUENCE. So you create a sequence then just say next value for. Will see if I can find a good example.
ORACLE 12c added IDENTITY column but before then it was like this:
The insert statement then would be:
CREATE SEQUENCE contact_seq
START WITH 1
INCREMENT BY 1
You can cache some as well as other things - https://docs.oracle.com/cd/B12037_01/server.101/b10759/statements_6014.htmThe insert statement then would be:
INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, comment3)
VALUES (contact_seq.nextval, '$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3')
ASKER
Thanks for your help so far - as you can tell this is all very new to me ..
DB has an auto-increment primary key.
My code is now:
It is saying error on line 51 (below - I'm not sure why
DB has an auto-increment primary key.
My code is now:
//Extract CGI variables
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comment3=$_POST['comment3'];
// Create the SQL statement to add the data
$query="INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, comment3)
VALUES (contact_seq.nextval, '$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3')"
//echo $query"</br></br>";
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query);
//echo "SQL: $query<br>";
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect,);
oci_execute($stmt);
if (oci_fetch_array($stmt)) {
$product = oci_result($stmt,1);//returns the data for column 1
// echo $product."</br>";
} else {
echo "An error occurred in retrieving customer id.\n";
exit;
}
It is saying error on line 51 (below - I'm not sure why
Looks like you are missing semi colon after the query (EDIT: line 15).
And what is on line 51. You've only posted 45 lines of code.
Glad the sequence helped by the way... Note if you have existing data, you can change the "start with" in the create sequence to be after those rows, so your new rows fall in line.
ASKER
Something so small - thanks, I'll add that in
ASKER
That was the receipt section which I need and that works so I didn't post that.
Here is the entire code.
It is still not working and coming up with errors. Getting confused and a bit overwhelmed.
Here is the entire code.
It is still not working and coming up with errors. Getting confused and a bit overwhelmed.
<!DOCTYPE HTML>
<html>
<head>
<title>Contact</title>
<meta charset="utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
/* Set oracle user login and password info */
$dbuser = "";
$dbpass = "";
$db = "";
$connect = oci_connect($dbuser, $dbpass, $db);
/* Display connection error if fails */
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
//Extract CGI variables
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comment3=$_POST['comment3'];
// Create the SQL statement to add the data
$query="INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, comment3)
VALUES (contact_seq.nextval, '$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3')";
//echo $query"</br></br>";
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query);
//echo "SQL: $query<br>";
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query);
oci_execute($stmt);
if (oci_fetch_array($stmt)) {
$product = oci_result($stmt,1);//returns the data for column 1
// echo $product."</br>";
} else {
echo "An error occurred in retrieving customer id.\n";
exit;
}
//display a receipt with all the order details
echo ("<p>====================== <strong>EMAIL RECEIPT</strong> =============================</p>");
echo $date = date('d-m-y H:i:s');
echo ("<p><h2><font color=blue>Thank you for contacting us.</font></h2></p>");
echo ("<p><strong>First Name: </strong>".$firstname. "</p>");
echo ("<p><strong>Last Name: </strong>".$lastname. "</p>");
echo ("<p><strong>Address: </strong>".$address. "</p>");
echo ("<p><strong>Phone: </strong>".$phone. "</p>");
echo ("<p><strong>Email: </strong>".$email. "</p>");
echo ("<p><strong>Subject: </strong>".$subject. "</p>");
echo ("<p><strong>Comments: </strong>".$comment3. "</p>");
echo ("<p>====================== END OF RECEIPT =============================</p>");
// Close the connection
oci_close($connect);
?>
</body>
</html>
Deep breathes. I just spent 22 hours straight working on a code project. After a while you get so into the frustration you miss simple things, so just step back and assess. What are the current error messages? If you are using a code editor (IDE), do you see any color coding that suggests something is not coming across correctly. For example, you have the SQL query on two lines like I had it typed in code box. I have seen this in PHP not get recognized as one fluid string. Look for other simple things like that. If all else fails get to a file with all the code you know is working then add back the other lines a bit at a time. i.e., use a multi-line comment block then more the start of it as you work down the block of potentially bad code.
ASKER
Current messages are in the post above. I am using NotePad ++.
Yes I understand what you mean - you become a bit cross-eyed.
Yes I understand what you mean - you become a bit cross-eyed.
Okay, to get you started, I pulled the file into my environment and saw a few oddities. Working on cleaning up to post in entirety so there is no disconnect.
P.S. For the COMMENT3 error, double check that is the column name in the system. Note also case in case your system is set to be case sensitive.
ASKER
Ok, thank you - that's nice of you!
Yes I have checked my HTML and database and it's 'Comment3'
Yes I have checked my HTML and database and it's 'Comment3'
ASKER
I hope I didn't miss anything obvious but you had a few calls duplicated but issue likely was the case of the column and to be safe I added a semi-colon at the end of the query on the ORACLE side. I believe when doing a prepared statement it is necessary.
I also added oci_free_statement to cleanup the prepared query just in case you are running into issues there.
Note: if you are worried about future ORACLE exception, you can wrap things in Try/Catch so you can control the display to the user.
<!DOCTYPE HTML>
<html>
<head>
<title>Contact</title>
<meta charset="utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
/* Set oracle user login and password info */
$dbuser = "";
$dbpass = "";
$db = "";
$connect = oci_connect($dbuser, $dbpass, $db);
/* Display connection error if fails */
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
//Extract CGI variables
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comment3=$_POST['comment3'];
// Create the SQL statement to add the data
$query="INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, Comment3) VALUES (contact_seq.nextval, '$firstname', '$lastname', '$address', '$phone', '$email', '$subject', '$comment3');";
/*
Consider using a prepared statement as well, examples below.
http://php.net/manual/en/function.oci-parse.php
*/
// $query="INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, Comment3) VALUES (contact_seq.nextval, :fname, :lname, :addr, :ph, :em, :subj, :note);";
$stmt = oci_parse($connect, $query);
// oci_bind_by_name($stmt, ':fname', $firstname);
// oci_bind_by_name($stmt, ':lname', $lastname);
// oci_bind_by_name($stmt, ':addr', $address);
// oci_bind_by_name($stmt, ':ph', $phone);
// oci_bind_by_name($stmt, ':em', $email);
// oci_bind_by_name($stmt, ':subj', $subject);
// oci_bind_by_name($stmt, ':note', $comment3);
//echo $query"</br></br>";
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);
if (oci_fetch($stmt)) {
$product = oci_result($stmt, 1); //returns the data for column 1
// echo $product."</br>";
} else {
echo "An error occurred in retrieving customer id.\n";
exit;
}
//display a receipt with all the order details
echo ("<p>====================== <strong>EMAIL RECEIPT</strong> =============================</p>");
echo $date = date('d-m-y H:i:s');
echo ("<p><h2><font color=blue>Thank you for contacting us.</font></h2></p>");
echo ("<p><strong>First Name: </strong>".$firstname. "</p>");
echo ("<p><strong>Last Name: </strong>".$lastname. "</p>");
echo ("<p><strong>Address: </strong>".$address. "</p>");
echo ("<p><strong>Phone: </strong>".$phone. "</p>");
echo ("<p><strong>Email: </strong>".$email. "</p>");
echo ("<p><strong>Subject: </strong>".$subject. "</p>");
echo ("<p><strong>Comments: </strong>".$comment3. "</p>");
echo ("<p>====================== END OF RECEIPT =============================</p>");
// Close the connection
oci_free_statement($stmt);
oci_close($connect);
?>
</body>
</html>
I also added oci_free_statement to cleanup the prepared query just in case you are running into issues there.
Note: if you are worried about future ORACLE exception, you can wrap things in Try/Catch so you can control the display to the user.
Just realized too that you are doing an INSERT but then looking for the results...
Think you just want $result = oci_execute(...) or you will have to run another query. I think you can do multiple statements in oci_prepare if you wrap them in a PL/SQL block like:
Just have to end each with a semi-colon.
Think you just want $result = oci_execute(...) or you will have to run another query. I think you can do multiple statements in oci_prepare if you wrap them in a PL/SQL block like:
BEGIN
INSERT ... ;
SELECT ... ;
END;
Just have to end each with a semi-colon.
ASKER
Ok - I got errors coming up on line 63 and 63 so I commented out from 62 > 69. I don't think I need this.
Now I don't get any errors and a receipt page pops up. However, when I query the database there is no data populated.
Now I don't get any errors and a receipt page pops up. However, when I query the database there is no data populated.
I also include code and reference in code for using a prepared statement with bind parameters. You should use those whenever possible, so you avoid any SQL injection or issues with quotes, et cetera.
ASKER
All I want to do is to insert data in the contact table
Do you have access to query directly on the Oracle system? If yes, try running the generated query there manually and make sure it is not having an issue. If not, try getting the result of the oci_execute.
ASKER
No, I don't.
Try it like this then:
<!DOCTYPE HTML>
<html>
<head>
<title>Contact</title>
<meta charset="utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
/* Set oracle user login and password info */
$dbuser = "";
$dbpass = "";
$db = "";
$connect = oci_connect($dbuser, $dbpass, $db);
/* Display connection error if fails */
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
//Extract CGI variables
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comment3=$_POST['comment3'];
$query="INSERT INTO Contact (ID, firstname, lastname, address, phone, email, subject, Comment3) VALUES (contact_seq.nextval, :fname, :lname, :addr, :ph, :em, :subj, :note);";
$stmt = oci_parse($connect, $query);
oci_bind_by_name($stmt, ':fname', $firstname);
oci_bind_by_name($stmt, ':lname', $lastname);
oci_bind_by_name($stmt, ':addr', $address);
oci_bind_by_name($stmt, ':ph', $phone);
oci_bind_by_name($stmt, ':em', $email);
oci_bind_by_name($stmt, ':subj', $subject);
oci_bind_by_name($stmt, ':note', $comment3);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
$result = oci_execute($stmt);
if ($result === false) {
echo "An error occurred storing the contact.\n";
exit;
}
//display a receipt with all the order details
echo ("<p>====================== <strong>EMAIL RECEIPT</strong> =============================</p>");
echo $date = date('d-m-y H:i:s');
echo ("<p><h2><font color=blue>Thank you for contacting us.</font></h2></p>");
echo ("<p><strong>First Name: </strong>".$firstname. "</p>");
echo ("<p><strong>Last Name: </strong>".$lastname. "</p>");
echo ("<p><strong>Address: </strong>".$address. "</p>");
echo ("<p><strong>Phone: </strong>".$phone. "</p>");
echo ("<p><strong>Email: </strong>".$email. "</p>");
echo ("<p><strong>Subject: </strong>".$subject. "</p>");
echo ("<p><strong>Comments: </strong>".$comment3. "</p>");
echo ("<p>====================== END OF RECEIPT =============================</p>");
// Close the connection
oci_free_statement($stmt);
oci_close($connect);
?>
</body>
</html>
Okay, assuming you have checked all the names for proper case to make sure everything is correct in the resulting query string, you can try like this but does not make sense if the SQL looks good in the echo but is not running on the Oracle end. Note: the semi-colons I put in the query string were purposeful. That is what should be ending the SQL statement on the Oracle end.
<!DOCTYPE HTML>
<html>
<head>
<title>Contact</title>
<meta charset="utf-8">
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
/* Set oracle user login and password info */
$dbuser = "";
$dbpass = "";
$db = "";
$connect = oci_connect($dbuser, $dbpass, $db);
/* Display connection error if fails */
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
//Extract CGI variables
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comment3=$_POST['comment3'];
$query='begin insert into Contact(ID, firstname, lastname, address, phone, email, subject, Comment3) values(contact_seq.nextval, :fname, :lname, :addr, :ph, :em, :subj, :note); end;';
$stmt = oci_parse($connect, $query);
oci_bind_by_name($stmt, ':fname', $firstname);
oci_bind_by_name($stmt, ':lname', $lastname);
oci_bind_by_name($stmt, ':addr', $address);
oci_bind_by_name($stmt, ':ph', $phone);
oci_bind_by_name($stmt, ':em', $email);
oci_bind_by_name($stmt, ':subj', $subject);
oci_bind_by_name($stmt, ':note', $comment3);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
$result = oci_execute($stmt);
if ($result === false) {
echo "An error occurred storing the contact.\n";
exit;
}
//display a receipt with all the order details
echo ("<p>====================== <strong>EMAIL RECEIPT</strong> =============================</p>");
echo $date = date('d-m-y H:i:s');
echo ("<p><h2><font color=blue>Thank you for contacting us.</font></h2></p>");
echo ("<p><strong>First Name: </strong>".$firstname. "</p>");
echo ("<p><strong>Last Name: </strong>".$lastname. "</p>");
echo ("<p><strong>Address: </strong>".$address. "</p>");
echo ("<p><strong>Phone: </strong>".$phone. "</p>");
echo ("<p><strong>Email: </strong>".$email. "</p>");
echo ("<p><strong>Subject: </strong>".$subject. "</p>");
echo ("<p><strong>Comments: </strong>".$comment3. "</p>");
echo ("<p>====================== END OF RECEIPT =============================</p>");
// Close the connection
oci_free_statement($stmt);
oci_close($connect);
?>
</body>
</html>
P.S. silly question. If you do not have access to the Oracle box, how did you create the sequence??
The ORA-00933 error could be coming from the contact_seq.nextval being in the query if it doesn't really exist as a sequence on the server.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$stmt = oci_parse($connect, $query_count);
You have no variable in your file called $query_count.
The oci_parse method is ued to prepare a query, so at some point before line 44, you'd need your query:
$query_count = "SELECT something FROM somwehere";