Updating database entry using PHP not working for text fields - php

Im trying to run the following code but it is giving me a strange error..
$result = $db->query("UPDATE `items`
SET `item_label`= ".$title.",
`item_quantity`=".$quantity.", `item_price`=".$price."
WHERE `item_id` = ".$_POST['id']);
If i remove the item_label = ".$title.", from the above code it works perfectly and successfully updates the quantity and price of the given row. e.g.
$result = $db->query("UPDATE `items`
SET `item_quantity`=".$quantity.",
`item_price`=".$price."
WHERE `item_id` = ".$_POST['id']);
when I run the code containing the item_label section it fails to set the item_label. and it gives the following error message..
Unknown column 'Updated Text' in 'field list'
Now the "Updated Text" is the value of $title.
Im baffled as to how / why it is viewing this content as a column header!?
any ideas as to why this would happen?

Since its a String you should give quotes around the $title
I would have done something like below
$result = $db->query("UPDATE items SET item_label = '".$title."', item_quantity=$quantity,item_price=$price WHERE item_id =$_POST['id']");

Related

using update statement in 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

Mysqli .Field value not getting updated

I am trying to update a field when particular function is called but its value is not getting updated in db . And also no errors showing up . Field type is text .
I tried by adding new field to table and then updated that field in my query and it worked fine, but after some time same issue with new field .
$con=mysqli_connect("localhost","username","password","dbname");
mysqli_query($con,"UPDATE sales_flat_quote SET extra_params = 'aaa',new_params = 'bbb' WHERE entity_id = 362214") or die(mysqli_error($con));
Above query was working earlier but later it doesn't update the value in database and if you run query directly on phpmyadmin it works fine .
Any Ideas ?
There's an error in your syntax, you forgot to close the double quote:
mysqli_query($con,"UPDATE sales_flat_quote SET extra_params = 'aaa',new_params = 'bbb' WHERE entity_id = 362214") or die(mysqli_error($con));
This should work. Syntax coloring is your friend ;)

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'";

Successfully Setting a MSSQL Database value to NULL

I have a query (insecure for the minute) that attempts to set a value of a column to NULL. Basically just revert it to being empty. 'Allow NULL' is checked in the Database design. Its an MSSQL Database. First of all I tried:
$query = "UPDATE Table_Name SET Image = '', Thumb = '' WHERE PageID = 5";
Then:
$query = "UPDATE Table_Name SET Image = NULL, Thumb = NULL WHERE PageID = 5";
The second one produces no errors, but does not set the database value to NULL. From what I can see though (website research) 'NULL' is correct? Sorry, new to PHP.
EDIT
The query is called by an if statement. That checks to see if a GET value is equal to. I am aware that this isn't very secure. I am just trying to test/play a little with techniques. The full call and query are as follows.
<?php
if (isset($_GET['change']) && $_GET['change'] == "image1") {
$query = "UPDATE Table_Name SET Image = NULL, Thumb = NULL WHERE NewsID = ".$_GET['id']." ";
}
?>
This if statement is triggered a delete 'button'. The code looks like:
delete</td>
I now believe, the query isn't getting called? However the button does change and set the values it is meant to do, so all I can assume, is that the if statement doesn't pick up on this. Update.php is the page the queries are currently on so it almost just acts as a refresh.
I haven't used PHP in a very long time, but does NULL need to be enclosed in quotes?
Try this:
$query = "UPDATE Table_Name SET Image = 'NULL', Thumb = 'NULL' WHERE PageID = 5";
Hope this helps.

After SQL update, field is set to 0 [PHP/MySQL]

Okay, I'm currently sick so maybe the answer is right in front of me but I've been sitting here for the past 4 hours trying to figure out why this is happening.
Basically I have a script that is supposed to update a row in the database.
The field I'm trying to update is 'name' and it looks like this:
// Updating the database with the new name
$name = $this->db->slashes( $this->friends[$row->key] );
$sql = "UPDATE `friends` SET `name` = '" . $name . "' AND `status` = 'updated' WHERE `key` = " . $row->key . " LIMIT 1";
echo '-- Query: ' . $sql . '<br />';
$result2 = $this->db->query( $sql );
if( !$result2 )
echo mysql_error();
Output becomes this(an example):
-- Query: UPDATE `friends` SET `name` = 'Andrew Johnsson' AND `status` = 'updated' WHERE `key` = 7823583 LIMIT 1
It did not generate any mysql_error so the query seem to have gone through properly and by the looks of the query, it should have just updated a row in the database where the key was a number and set the new name to Andrew Johnsson aswell as setting status to updated.
However! After looking in the database after this update, the name for this row is set to '0'. Why on earth did it do that?
Any ideas as to why this is happening?
Also, tell me if you need some more information to be able to help me and I'll kindly provide it!
Your UPDATE syntax is incorrect. It should be SET name = 'string', status = 'updated' WHERE key...
Currently you are literally asking it to evaluate the AND's in order to determine what the name column should be updated with, which is why you're getting the 0.
Well, this might be too silly, but name is a string field, right? If it's set to any kind of integer field, this is exactly what would happen: the string is parsed as 0.
I bet it's something a wee bit more complicated than that, though.

Categories