Using HTML form to update Oracle Database - Database Not Updating - php

I am attempting to create a form where an admin can update a webpage's data. I am taking the form's $_POST variables and sending them to the handler (in this case, UpdateInfo_SA.php). From there I am trying to send it to the database (see snippet 1). There is no error, in fact the page is blank (which is expected, given the current code). However, the table does not update.
Info:
Table Name: LOGISTICS_SLIDESHOW
Updating OH_RECORDABLE_ENERGY to any value (just to test to see if this works) where LOGISTICS_UPDATEID=0 (this represents the first column of the table).
Snippet 1 (Form Handler - UpdateInfo.php):
<?php
require_once("mcl_Oci.php");
$objConnect = oci_connect("user", "pass", "(description=(address=(protocol=tcp)(host=HOST)(port=1533))(connect_data=(service_name=SID)))")
$strSQL = "UPDATE INTOXDM.LOGISTICS_SLIDESHOW ";
$strSQL .="SET OH_RECORDABLE_ENERGY = '6'";
$strSQL .="WHERE LOGISTICS_UPDATEID= 0 ";
$objParse = oci_parse($objConnect, $strSQL);
$objExecute = oci_execute($objParse, OCI_DEFAULT);?>
Snippet 2 (Form - SA_Update.php):
<?php
$objConnect = oci_connect("user", "pass", "(description=(address=(protocol=tcp)(host=HOST)(port=1533))(connect_data=(service_name=SID)))");
?>
<div align="center">
<span style="font-size:60px";>
<u>Update Page<br></u>
Schedule Adherence: OH Contractors<br>
</span>
<form method="post" action="UpdateInfo_SA.php">
<span style="font-size:30px;">
<u>Total YTD:<input type="number" name="SA_ytd_total" value="%"><br>
Total MTD:<input type="number" name="SA_mtd_total" value="%"><br>
Energy YTD: <input type="number" name="SA_ytd_energy" value="total"><br>
Energy MTD: <input type="number" name="SA_mtd_energy" value="total"><br>
Hydraker YTD: <input type="number" name="SA_ytd_hydraker" value="total"><br>
Hydraker MTD: <input type="number" name="SA_mtd_hydraker" value="total"><br>
NG Gilbert YTD: <input type="number" name="SA_ytd_gilbert" value="total"><br>
NG Gilbert MTD: <input type="number" name="SA_mtd_gilbert" value="total"><br></u>
<input type="submit" value="Submit">
</form>
As you can tell, the server is using php's to communicate with itself and the database. Thanks for any help you can give, it is very appreciated!!

Do you commit the transaction in your php, where open up the connection and update ?
Unless autocommit is turned ON or you commit ,the updated data is not available to the rest of world
EDIT: http://www.php.net//manual/en/function.oci-execute.php
The above link refers to the auto commit options available. Default is NO auto commit.. So all updates are rollbacked when the session is disconnected.

Related

Not able to insert data using html form and php using wamp [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 1 year ago.
I am trying to insert data into a database from HTML form using php. I made two files html form and other is PHP script. When I click on submit in html form, it shows me the php code. I am using wamp server for database. I put my html files in C:/wamp64/www directory and html files at my local directory. The database table is :
id int(11)
fname varchar(30)
salary int(11) . Id is not auto-incremented and it is a primary key.
Html code:
<html>
<body>
<h2>Employee's Information</h2>
<form action="employee.php" method="POST">
<label for="id">Enter employee id:</label><br>
<input type="text" id="id" name="id" value=""><br>
<label for="fname">Enter First name:</label><br>
<input type="text" id="fname" name="fname" value=""><br><br>
<label for="salary">Enter Employee Salary:</label><br>
<input type="text" id="salary" name="salary" value=""><br><br>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
Php code:
<?php
$mysql_hostname="localhost";
$mysql_username="root";
$mysql_password="";
$mysql_database="employee";
$con=mysql_connect($mysql_hostname,$mysql_username,$mysql_password);
if(!$con){
die('Connection Error: '.mysql_error());
}
mysql_select_db($mysql_database, $con);
if(isset($_POST['submit']))
{
$s_id = $_POST['id'];
$s_name = $_POST['fname'];
$salary = $_POST['salary'];
$employeeinsert = "INSERT INTO employee1
(id, fname, salary)
VALUES('".$s_id."','".$s_name."','".$salary."')";
if(!mysql_query($employeeinsert,$con)) {
echo "Error: " .mysql_error($con);
} else {
echo "1 record added";
}
}
?>
The code is neither giving any error on submitting data nor it is inserting the data into the database.
I am not getting what the error is.
If this is false then the code successfully produces no output:
if(isset($_POST['submit']))
Which is what's happening, since the condition is false. The form has a submit button, but that button has no name attribute to its value isn't sent to the server:
<input type="submit" value="Submit">
Give it a name:
<input type="submit" name="submit" value="Submit">
It's always a good idea to have some kind of indication of any given code branch, even if just logging something somewhere so you can see what's happening. Code will happily produce no output/result if that's what it's instructed to do, but as you've discovered it can leave you with no information about what's happened.
As an aside, and this is important, your code is wide open to SQL injection. You'll want to start addressing that.

Update data WHERE

I have a table called client and I am trying to update the contact number, but for only the id that is typed in. I have a form that creates two textfields for the data to be changed. My problem is im unsure on how i can only update data for only the id that is entered.
Code:
<form method="post" name="update" >
Client ID:
<br>
<input type="text" name="clientid"><br>
Contact Number:
<br>
<input type="text" name="contactno"><br>
<input type="submit" name="submit" value="Update"><br><br>
</form>
<?php
if(isset($_POST['submit'])){
$client = $_POST['clientid'];
$contact = $_POST['contactno'];
$result= $pdo->prepare ("UPDATE client SET client_contact_number='$contact' WHERE client_id='$client'");
$result->execute;
}
?>
your syntax should be:
mysqli_query($connection, $sql_query)
you're missing the $connection object in your method.
PHP docs: http://php.net/manual/en/mysqli.query.php
*Original version of this question used procedural syntax - hence my answer.

If input exists from one table update it on another one

Im trying to make like a refill code that will refill my table.
I have one table that have my refill code in it and other table that stores the balance of the account.
table1: card_credit (table that stores the balance of the account)
table2:card_refill (table that have me refill code)
I have created this code with session and PHP. Now I'm stuck and dont know how to move forward.
I want to make when i write in the refill code from table card_refill that its take the amount of credit into value in table card_refill
refill.php
<strong>Refill</strong>
<form action="refill.php" method"post">
<input type="text" name="refillcode"/>
<br>
<input type="submit" name="Submit" value="Refill" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['refillcode'] = $_POST['refillcode'];
}
?>
Here is a possible solution, I am just don't know, where the card_id comes from.
This is inserting a new record into your card_credit table.
// starting the session before any output
session_start();
//include here the database connection file!
//for example:
//include('db_connection.php');
if (isset($_POST['Submit'])) {
//First do a validation here, is the refillcode number, exists, etc...
//Insert it into the table
$sql = "INSERT INTO card_credit (card_id, value) VALUES ('[YOUR_CARD_ID_HERE]', " . intval($_POST['refillcode']) . ")";
//Link is the resource variable when you created the mysqli_connect
mysqli_query($link, $sql);
//Redirect here if you want
}
?>
<!-- HTML CODE STARTS HERE -->
<strong>Refill</strong>
<form action="refill.php" method="post">
<input type="text" name="refillcode"/>
<br>
<input type="submit" name="Submit" value="Refill" />
</form>

Prevent executing mathematical expression in input field when adding value to MySQL (PHP)

I have set up the following form:
<form name="pric" method="post" action="up.php">
<div id="prices_col">Season A<br>
<input type='text' name="date0" maxlength="13" size="15" style="font-size: 9px;" value="<?php echo $_date[0]?>" />
</div>
<div align="middle"><input type="submit" value="EDIT"></div>
</form>
Information in database right now was like this ($_date[0] contains):
04/06 - 25/06
After posting the information, it decided to run the expression and I got something like:
-1.333333333
I use the following code:
$_date[0] = trim($_POST["date0"]);
mysql_query("UPDATE price SET _date=".$_date[0]." WHERE id='0'") or die(mysql_error());
How can I stop it from executing? I need to store the value as a plain text to the database.
mysql_query("UPDATE `price` SET `_date`='".mysql_real_escape_string(trim($_POST["date0"]))."' WHERE `id`=0") or die(mysql_error());
as _date is a text field and mysql_real_escape_string for security

How to set-up php in order to insert data into a database?

I'm trying to insert some data into a database using an html form. The form is coded in html, the database is managed using MySQL and I'm using php to insert data. I think the problem is not on the code but in the php set-up (I include the code just in case).
I've installed MAMP (I'm working on a Snow Leopard Mac). PHP seems to be working properly (If I enter commands on the terminal everything works fine)
The action of the form is a php file called insertar.php
The problem is that when I click on the submit button after filling the form, the browser shows the code for insertar.php but it doesn't insert.
I would greatly appreciate your help.
PHP CODE:
<?php
$conn = mysql_connect("localhost","root","root");
if (!$conn)
{
die('Imposible efectuar la conexion' . mysql_error());
}
mysql_select_db("zealot", $conn);
$id = $_POST["id"];
$date = $_POST["date"];
$name = $_POST["name"];
mysql_query("insert into logistica_rastreo values('$id' , '$date' , '$name');");
?>
HTML CODE
<form name="the_form" method="post" action="insertar.php">
<table>
id: <input type="text" size="30" maxlength="40" name="id"/>
date: <input type="text" size="30" maxlength="40" name="date"/>
name: <input type="text" size="30" maxlength="40" name="name"/>
</table>
<br/>
<input type="submit" value="Registrar" name="registrar" />
<br/>
</form>
Hi deps_stats,
you HTML and PHP code is perfectly ok as far as i can verify this...
You need to check your server settings from cpanel, and check whether query modules (i.e execution, deletion,etc are enabled or not)..
This problem is at your server level not at code level... Hope this would help..
Not sure if this is the problem, but it's good practice to always make sure to specify which columns you want to insert into. For example, instead of this:
mysql_query("insert into logistica_rastreo values('$id' , '$date' , '$name');");
do something like this:
mysql_query("insert into logistica_rastreo(id,date,name) values('$id' , '$date' , '$name');");

Categories