inserting mysql date field into MySQL Database - php

I'm having an issue submitting a form into a database once I have added a Submitted_Date field.
The errors are as follows:
Fatal error: Uncaught mysqli_sql_exception: Incorrect date value:
'2022' for column 'Submitted_Date'
mysqli_sql_exception: Incorrect date value: '2022' for column
'Submitted_Date'
Prior to adding in $Submitted_Date = date('Y-m-d', strtotime($_POST['Submitted_Date'])); I was also getting an error for incorrect listed fields on the binding.
The Submitted_Date field is a date input field so needs to be handled as 'NULL' value if a date has not been entered. Instead I also get a
'Incorrect date value: '1970'
Any help would be great. Thanks.
<?php
if (isset($_POST['submit'])) {
$sql = $conn->prepare("UPDATE orders SET Publisher_Email=? , Client_Name=? , Submitted_Date=? WHERE PC_Job_Number=?
");
$Publisher_Email = $_POST['Publisher_Email'];
$Client_Name = $_POST['Client_Name'];
$Submitted_Date = date('Y-m-d', strtotime($_POST['Submitted_Date']));
$sql->bind_param("ssii", $Publisher_Email, $Client_Name, $Submitted_Date, $_GET["PC_Job_Number"]);
if (empty($Submitted_Date)) {
$Submitted_Date = "NULL";
}
if($sql->execute()) {
$success_message = "Edited Successfully";
} else {
$error_message = "Problem in Editing Record";
}
}
$sql = $conn->prepare("SELECT * FROM orders WHERE PC_Job_Number=?");
$sql->bind_param("i",$_GET["PC_Job_Number"]);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
}
$conn->close();
?>

Related

Insert foreign key value in my sql database?

I want to insert foreign key value in table.I have two tales employee(employee_id) and attendance. Here employee_id is foreign key in attendance table.
I try a lot but value is not inserted.
Here is my code
if(isset($_POST['submit']))
{
$date = date('Y-m-d',strtotime($_POST['daily_date']));
$in = $_POST['daily_in'];
$l_out = $_POST['lunch_out'];
$l_in = $_POST['lunch_in'];
$out = $_POST['daily_out'];
$emp_remarks = $_POST['remarks'];
$sql = "INSERT INTO attendance (atten_id,daily_date,daily_in,lunch_out,lunch_in,daily_out,remarks,employee_id)
VALUES('NULL','$date','$in','$l_out','$l_in','$out','$emp_remarks','".$_REQUEST['employee_id']."')";
$res = mysql_query($sql);
if ($res > 0) {
echo "inserted";
}
If I run below code then
if(isset($_POST['submit']))
{
$date = $_POST['daily_date'];
$in = $_POST['daily_in'];
$l_out = $_POST['lunch_out'];
$l_in = $_POST['lunch_in'];
$out = $_POST['daily_out'];
$emp_remarks = $_POST['remarks'];
if(isset($_REQUEST['employee_id']))
{
echo "Employee Id" .$_REQUEST['employee_id'];
}
else {
echo "Smoething went wrong";
}
$sql = "INSERT INTO attendance (atten_id,daily_date,daily_in,lunch_out,lunch_in,daily_out,remarks,employee_id)
VALUES
('NULL','$date','$in','$l_out','$l_in','$out','$emp_remarks','".$_REQUEST['employee_id']."')";
its gives
Smoething went wrong not inserted error
Before inserting try this , try this to check whether value was passed or not.
isset($_REQUEST['employee_id'])
{ echo "Employee Id" .$_REQUEST['employee_id'];
} else {
echo "Smoething went wrong";
}

Receiving an error "ORA-00904: "ORDERNUMBER": invalid identifier" for php?

It says the error code is indentified at line 69 and line 72, which the codes are:
Line 69: OCI_Execute($stmt);
Line 72: if(OCI_Fetch($stmt)) {
Here is my following code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
/* Set oracle user login and password info */
$dbuser = "..."; /* your [login */
$dbpass = "..."; /* your oracle access password */
$db = "SSID";
$connect = OCILogon($dbuser, $dbpass, $db);
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
// Extract form data
$book1 = $_REQUEST['book1'] ;
$price1 = $_REQUEST['price1'] ;
$quantity1 = $_REQUEST['quantity1'] ;
$subtotal1 = $_REQUEST['subtotal1'] ;
$book2 = $_REQUEST['book2'] ;
$price2 = $_REQUEST['price2'] ;
$quantity2 = $_REQUEST['quantity2'] ;
$subtotal2 = $_REQUEST['subtotal2'] ;
$book3 = $_REQUEST['book3'] ;
$price3 = $_REQUEST['price3'] ;
$quantity3 = $_REQUEST['quantity3'] ;
$subtotal3 = $_REQUEST['subtotal3'] ;
$book4 = $_REQUEST['book4'] ;
$price4 = $_REQUEST['price4'] ;
$quantity4 = $_REQUEST['quantity4'] ;
$subtotal4 = $_REQUEST['subtotal4'] ;
$grandtotal = $_REQUEST['grandtotal'] ;
// get the max ID in plants table and allocate $ID+1 for the new record
$max_id_stmt = "SELECT max(ID) FROM bookorder ";
// check the sql statement for errors and if errors report them
$stmt = OCIParse($connect, $max_id_stmt);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);
$ID =0;
if(OCIFetch($stmt)) {
$ID= OCIResult($stmt,1); //return the data from column 1
}else {
echo "An error occurred in retrieving book id.\n";
exit;
}
$ID++;
// get the max ID in plants table and allocate $ID+1 for the new record
$max_OrderNumber_stmt = "SELECT max(OrderNumber) FROM bookorder ";
// check the sql statement for errors and if errors report them
$stmt = OCIParse($connect, $max_OrderNumber_stmt);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCI_Execute($stmt);
$OrderNumber =1111;
if(OCI_Fetch($stmt)) {
$OrderNumber= OCIResult($stmt,1); //return the data from column 1
}else {
echo "An error occurred in retrieving book order number.\n";
exit;
}
$OrderNumber++;
// Create the SQL statement to add the data. Note: field value should be single quoted '' if VARCHAR2 type.
$sql = "INSERT INTO plantorder (ID, OrderNumber, plant1, price1, quantity1, subtotal1, plant2, price2, quantity2, subtotal2, plant3, price3, quantity3, subtotal3, plant4, price4, quantity4, subtotal4, grandtotal) VALUES ($ID, '$OrderNumber', '$plant1', '$price1', '$quantity1', '$subtotal1', '$plant2', '$price2', '$quantity2', '$subtotal2', '$plant3', '$price3', '$quantity3', '$subtotal3', '$plant4', '$price4', '$quantity4', '$subtotal4', '$grandtotal')";
// Add the data to the database as a new record
$stmt = OCIParse($connect, $sql);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);
$order_count_sql = "SELECT max(OrderNumber) FROM plantorder";
$stmt = OCIParse($connect, $order_count_sql);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);
$order_count =0;
// retrieve the max order number
if(OCIFetch($stmt)) {
$order_count = OCIResult($stmt,1); //return the data from column 1
}else {
echo "An error occurred in retrieving order id.\n";
exit;
}
echo("Your order has been received; please quote Order Number #".$order_count." in any correspondence. Thank you.");
?>
</body>
</html>
i have left out the login and password info just to let you know, that isnt causign the error :) Kind Regards

Resource id #6 error, Not sure how to fix it

I keep getting a 'Resource id # 6' failure when submitting a script on my website. The code I'm using is the same type of code I use for registering for the website and that works but this script doesn't work at all. What my code does is send a booking request with the fields as shown to the database. I keep getting a Resource id#6 error , and I've googled what that is but I can't seem to figure out whats wrong. I am a beginner at php , so any tips on whats to look for to avoid a resource id # 6 error would be a lot of help
<?php
//$pattern="/^.+#.+/.com/";
//error_reporting(0);
if(isset($_POST["submit"])){
$Name_of_Person = $_POST['Name_of_Person'];
$Name_of_Group = $_POST['Name_of_Group'];
$room = $_POST['room'];
$How_Many_People = $_POST['How_Many_People'];
$Date_of_Booking = $_POST['Date_of_Booking'];
$End_time = $_POST['End_time'];
$Purpose = $_POST['Purpose'];
$Contact_Number = $_POST['Contact_Number'];
$Contact_Email = $_POST['Contact_Email'];
$Alcohol = $_POST['Alcohol'];
$Security = $_POST['Security'];
$Projector = $_POST['Projector'];
$Extra_Chairs = $_POST['Extra_Chairs'];
$Extra_Info = $_POST['Extra_Info'];
$Activated = '0';
$con = mysql_connect('localhost','root','test123') or die("couldn't connect");
mysql_select_db('bookerdb') or die("couldn't connect to DB");
//if(filter_var($email, FILTER_VALIDATE_EMAIL)){//(preg_match($pattern, $_POST['Contact_Email'])){
$query = mysql_query("SELECT * FROM `booking_table` WHERE Date_of_Booking='".$Date_of_Booking."' AND room='".$room."'");
$numrows = mysql_num_rows($query);
echo $query;
if($numrows==0){
$sql="INSERT INTO `booking_table` (Name_of_Person,Name_of_Group,room,How_Many_People,Date_of_Booking,End_time,Purpose,Contact_Number,Contact_Email,Alcohol,Security,Projector,Extra_Chairs,Extra_Info, Activated) VALUES ('$Name_of_Person','$Name_of_Group','$room','$How_Many_People','$Date_of_Booking','$End_time','$Purpose','$Contact_Number','$Alcohol','$Security','$Projector','$Extra_Chairs','$Extra_Info',$Activated)";
$result = mysql_query($sql);
if($result){
echo "Sent to be approved";
$redirect_page = '../ASC.php';
$redirect = true;
if($redirect==true){
header('Location: ' .$redirect_page);
}
}else{
echo "Failed";
}
}else{
echo"There is already a requested booking on that date & time";
$redirect_page = '../EAR.php';
$redirect = true;
if($redirect==true){
header('Location: ' .$redirect_page);
}
}
/*}else{
echo "error";
$redirect_page = '../EWF.php';
$redirect = true;
if($redirect==true){
header('Location: ' .$redirect_page);
}
}*/
}
?>
You have error in your second SQL query. You try to insert 14 values into 15 columns (in values you forgot $Contact_Email).
$sql="INSERT INTO `booking_table` (Name_of_Person,Name_of_Group,room,How_Many_People,Date_of_Booking,End_time,Purpose,Contact_Number,Contact_Email,Alcohol,Security,Projector,Extra_Chairs,Extra_Info, Activated) VALUES ('$Name_of_Person','$Name_of_Group','$room','$How_Many_People','$Date_of_Booking','$End_time','$Purpose','$Contact_Number','$Contact_Email','$Alcohol','$Security','$Projector','$Extra_Chairs','$Extra_Info',$Activated)";
Than remove echo $query from your code, line 30.
In $query isn't query, but mysql result object. You can't work with that by this way, you can't echo it.

Update successfully but couldn't update into DB

I couldn't find any error. I tried the query on phpmyadmin and it works well but when I do in php page, it couldn't update into DB. The following code below:
$registerID = ($_POST['registerID']);
$firstName = ucwords(htmlspecialchars($_POST['firstName']));
$lastName = ucwords(htmlspecialchars($_POST['lastName']));
$emailAddress = htmlspecialchars($_POST['emailAddress']);
$mainAddress = ucwords(htmlspecialchars($_POST['fullAddress']));
$updateCity = ucwords($_POST['userCity']);
$updateCountry = $_POST['userCountry'];
$postalCode = strtoupper(htmlspecialchars($_POST['userZip']));
$profilePic = $_POST['pic'];
$updateProf = " UPDATE register_user
SET firstName='$firstName',
lastName='$lastName',
emailAddress='$emailAddress',
mainAddress='$mainAddress',
registerCity='$updateCity',
registerCountry='$updateCountry',
postalCode='$postalCode'
WHERE registerID = '$registerID'";
if (mysqli_query($mysqli, $updateProf)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($mysqli);
}
In the end, there are no errors after I updated on the webpage, it just show Record updated successfully. But it didn't update into DB. Any ideas?
UPDATED CODING
$checkProfile = "SELECT * FROM register_user where emailAddress = '$emailAddress'";
$editProfile = mysqli_query($mysqli,$checkProfile);
if ($editProfile) {
if (mysqli_num_rows($editProfile) > 0) {
header("Location: event?error=That name of email has already been taken");
} else {
$updateQuery = "UPDATE register_user
SET firstName = '$firstName',
lastName = '$lastName',
emailAddress = '$emailAddress',
mainAddress = '$mainAddress',
registerCity = '$updateCity',
registerCountry = '$updateCountry',
postalCode = '$postalCode'
WHERE registerID = '$registerID'";
$updateResult = mysqli_query($mysqli,$updateQuery);
header("Location: profileUser");
}
}
After I updated, it still doesn't work after I am using prepared statement. Any ideas?
Try executing the query first, saving it into a variable.
then, check if the query executed by doing:
if(!$query) echo "Query error : " . $mysqli->error;
This will give you more detailed error report.

Validate unique entry on edit/update

I'm trying to validate a unique entry when editing/updating a record.
I am validating by searching the database, if the entry is not found then proceed else print an error message. If an entry (I am using this same process when adding new records)
My validation code is:
if (!empty($_POST['ip_add'])) {
if (filter_var($_POST['ip_add'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
$errors[] = $_POST['ip_add'] . ' is not a valid IPv4';
} else {
$ip = $_POST['ip_add'];
//check if ip is unique
$qip = 'select INET_NTOA(ip_add) AS ip_add from equipment where ip_add = INET_ATON(:ip)';
$database->query($qip);
$database->bind(':ip', $ip);
$rs = $database->resultset();
//execute our query
$database->execute();
if ($rs != null) {
$errors[] = $_POST['ip_add'] . ' is not unique';
} else {
$ip_add = $_POST['ip_add'];
}
}
} else {
$errors[] = "Please enter a valid IP address";
}
if there are no errors update database:
$query = "
UPDATE equipment
SET site_code = :site_code,
site_id = :site_id,
system_name = :system_name,
ip_add = INET_ATON(:ip_add),
mcast = INET_ATON(:mcast),
sys_loc = :sys_loc,
systype = :systype,
itamname = :itamname,
dimetis = :dimetis,
DNS = :DNS
WHERE id = :id
";
//prepare query for excecution
$database->query($query);
//bind the parameters
$database->bind(':site_code', $site_code);
$database->bind(':site_id', $site_id);
$database->bind(':system_name', $system_name);
$database->bind(':ip_add', $ip_add);
$database->bind(':mcast', $multicast);
$database->bind(':sys_loc', $sys_loc);
$database->bind(':systype', $systype);
$database->bind(':itamname', $itamname);
$database->bind(':dimetis', $_POST['dimetis']);
$database->bind(':DNS', $_POST['DNS']);
$database->bind(':id', $_POST['id']);
// Execute the query
$database->execute();
echo "Record was updated.";
}
}
I was thinking I could search on all records execpt current record that I am currently editing.
How would I do this?
Is there a better way for checking if IP addrss is unique when editing a record?
I got this to work be excluding current record from search:
$qip = '
SELECT
INET_NTOA(ip_add) AS ip_add
FROM equipment
WHERE
ip_add = INET_ATON(:ip)
AND id != :id
';
$database->query($qip);
$database->bind(':ip', $ip);
$database->bind(':id', $id);

Categories