using update statement in php - php

I am trying to update my database using php however my update clause is not working.
This is the code:
<?php
if (isset($_POST['submit']))
{
date_default_timezone_set('Australia/Sydney');
$date = date("Y-m-d h:i:s");
$sqlUpdate = " UPDATE booking
SET checkedin = '1', checkin_datetime = '$date'
WHERE id = '$flightID'";
$result3 = mysqli_query($dbConn, $sqlUpdate) or trigger_error("Query Failed! SQL: $sqlUpdate - Error: ".mysqli_error($dbConn), E_USER_ERROR);
}
?>
In my database the flightID is an auto increment integer and the checkin is a tiny int.
I believe that it could be treating my variable as a string and not a integer which is why it is not updating in my database.
I have tried creating a variable to to hold the value of 1 and replace it with the hard coded value in the SQL which did not change anything.
Any ideas what the problem is and how to fix it?
here is a photo for my booking table
please note in my sql the 'flightID' variable is actually referring to the id column in the booking table

Related

using php to insert records in my database

I am trying to insert data into my database from a user form using php
I normally do not have any issues when doing this except this time and I believe it is because of the date type as explained below
Here is the code:
<?php
session_start();
$user = $_SESSION['who'];
if (!$_SESSION['who']){
header("location: login.php");
}
if (isset($_POST['submit'])){
require_once('dbconn.php');
$flightID = $_POST["flights"];
$sql= "INSERT INTO booking (flight_id,customer_id,checkedin,checkin_datetime, booking_datetime,baggage)
VALUES ('1', '1','0','10-10-10', '10-10-10','30')";
if($recordSet = $dbConn->query($sql)) {
echo "record set";
}
else
echo "not set";
}
?>
I will be using variables instead of hard coded values for my insert statement however I am just trying to get it to work.
Here are the attributes in my database table for booking
id, flight_id, customer_id, checkedin, checkin_datetime, booking_datetime, baggage
The ID is automatic which is why it is not in my sql statement
currently my page is displaying not set I think it may be due to the date field as I am not sure which format it may use.
UPDATE: I changed the date in my sql statement and it still is not working, no errors coming up as well.
You can Use
$sql= "INSERT into booking (flight_id,customer_id,checkedin,checkin_datetime, booking_datetime,baggage)
VALUES ('1', '1','0','2020-10-10', '2020-10-10','30')";
$sql= "INSERT INTO booking (flight_id,customer_id,checkedin,checkin_datetime, booking_datetime,baggage)
VALUES ('1', '1','0','2010-10-20', '2010-10-20','30')";
Note :
date format: YYYY-MM-DD
All id must be exists in the database (if you use Foreign Key for flight_id and customer_id).
Date format looks ok in the sql statement.
make sure ID field is auto increment in the database as you have not used in the sql statement.

How not to update all MySQL records with the same value?

When I'm updating MySQL records using the following SQL code, the result is always the same - it always update ALL records with the same value.
I used print_r($_POST); and found that the correct values were passed in PHP form.
// create and execute UPDATE query
foreach($_POST['update_category'] as $number => $update_category) {
$query = "UPDATE goals SET category='$update_category',goal='".$_POST['update_goal'][$number]."' WHERE username='$username';";
mysql_query($query, $connection);
}

MySqli error using time() in query to update a table

MySqli problem:
i used this query to update a field in table.
mysqli_query($con, " UPDATE users SET profile_pic = '$img_name' WHERE id = '$id' ");
where
$img_name = time().$id;
and
$id = 3;
when I echo $img_name, it give correct result.
but value stored in database it gives 2147483647 ie maximum integer value for my 32bit computer.
get rid of profile_pic field in the table
when uploading image, rename it to 'profile'.$id.'.jpg'

PHP/MySQL Concat to a single column and Update other columns in table

Am trying to only concat new updates to column updates and UPDATE the values in the rest of the columns but I've hit bit of a snag that I can't seem to workout.
My SQL looks like this:
$query="Update tickets SET product='$product',
p='$p',
i='$i',
summary='$summary',
workaround='$workaround',
concat(updates,'$additional_update'),
status='$status',
raised_by='$raised_by',
updated_by_user='$updated_by' WHERE id='$id'";
the updates column is like a comments column, where new updates are meant to be appended to the existing text.
The error I'm getting on the web server:
Update tickets SET product='T-Box', p='00000817766', i='-', summary='Testing update field
\r\nAdding an update\r\ntesting if null works for update', workaround='n/a', concat(updates,' ','test2#18:53:17:second update/n'), status='Open', raised_by='No', updated_by_user='test2' WHERE id='223'
Running the query directly in MySQL:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(updates,'test2#18:53:17:second update/n'), status='Open', raised_by='No', updat' at line 1
Help is much appreciated!
You need to specify where the value of this statement concat(updates,'$additional_update') to be set.
Update tickets
SET product = '$product',
p = '$p',
i = '$i',
summary = '$summary',
workaround = '$workaround',
updates = CONCAT(updates,'$additional_update'), // <== see this
status = '$status',
raised_by = '$raised_by',
updated_by_user = '$updated_by'
WHERE id = '$id'
try this:
$query="Update tickets SET product='$product',
p='$p',
i='$i',
summary='$summary',
workaround='$workaround',
updates=concat(updates,'$additional_update'),
status='$status',
raised_by='$raised_by',
updated_by_user='$updated_by' WHERE id='$id'";

Update int in MySQL Field

How can I increment an int in a cell of a MySQL database? I know that auto-increment is no use because I never want to add a new row, just update an existing one. I'm currently using this (POST var used for clarify, is verified in the real code):
$columnToUpdate = 'type'.$_POST['voteid'];
$query = "UPDATE myTable $columnToUpdate = $columnToUpdate+1 WHERE id=1;";
if(!mysql_query($query)) {
echo json_encode(array('success' => false, 'message' => 'Update failed: '.mysql_error()));
exit;
}
In the database I have 6 fields, id, type1, type2, type3, type4, type5, and a single row with id set to 1. The intention is to recieve a number (1-5), and build a reference to the correct column before updating the field. That results in Update failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=type4+1 WHERE id=1' at line 1, so I guess it's not getting the field value out properly before it increments.
Once this is working I'm also going to need to decrement a field in the same way, unless its value is 0. So for bonus points, can I do all this in one query or would it be better to split it up?
I think you've missed the keyword 'SET' from your query - try
$query = "UPDATE myTable SET $columnToUpdate = $columnToUpdate+1 WHERE id=1;";
Edit:
To do the "decrement unless it's zero" you could use something like:
UPDATE myTable SET $columnToUpdate =
CASE $columnToUpdate
WHEN 0 THEN 0
ELSE $columnToUpdate - 1
END CASE
WHERE id=1;`
For bonus points, to decrement:
$query = "UPDATE myTable SET '$columnToUpdate' = '$columnToUpdate'-1 WHERE id=1 AND '$columnToUpdate' > 0";
Besides the injection issues, it seems as if your workflow may need some work. Are you sure you want to choose the column that will be updated based on POST variable? It seems like you would specify the column and use the variable to find the record that needs to be updated:
IE:
"UPDATE myTable SET votes=votes+1 WHERE id=$post_variable;"
Again you should send the variable as a parameterized query to protect yourself from SQL injection.

Categories