transform string in mysql field - php

I have a database field image URL in which there are image names like image-0073 but i need to create a URL like www.xxxxx.com what i already did with the following code
if (isset($result2['img_name'])) {
$result2['img_name'] = 'http://www.xxxxxx.com/' . $result2['img_name'];
But now i have to replace the name of image image-0073 with new name www.xxxxx.comimage-0073 in the field image URL but don't know how to update that specific field.

Use CONCAT function
like below :
update tablename set imageURL=CONCAT("www.xxxxx.com',imageURL)
Please add conditions and do changes according to your requirments.

update table set img_name = concat('http://www.xxxxxx.com/',img_name) where some_field=condition
note that if you are going to update every field in the table with no condition, it will make no sense.
I am also have no idea why do you say you want to empty the field.

You can try:
UPDATE table_name SET field_name = $newImageUrl WHERE img_name = $result2['img_name'];

Related

php image update query, how to update the query

Exampe list view:
Example of edit view:
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
$sql = "INSERT INTO imgexam
(image, name)
VALUES
('{$imgData}', '{$_FILES['userfile']['name']}');";
How to change the query, so it will update the current image instead of inserting a new one?
So what you have to do to update it to the new image is the following steps:
Remove old image Upload
new image Update the row in the database to
the name of the new image
How to remove old image using PHP:
unlink('directory/images/'.$image);
You might need to do a selection from the database to get the right image name.
How to upload new image using PHP:
Read this page for more informations on uploading files
Then after uploading the new image, you need to change the row in the database.
How to change the row in the database
$sql = "UPDATE FROM `table` SET `imagename` = '$newimagename' WHERE `imagename` = $oldimagename";
$query = mysql_query($sql) or die(mysql_error());
Thats is the steps you need. Ask if you have problems.
The SQL query you're looking for to update a record would be similar to this:
UPDATE imgexam SET image = ? WHERE name = ?;
If you have duplicate names, you'll want to retrieve the ID for the record and use that in the WHERE clause. You can get the ID with a method like PDO::lastInsertId() or by querying for it.
What you may also be looking for is sometimes called an UPSERT operation. This means I want to UPDATE or INSERT. MySQL provides an ON DUPLICATE KEY clause. Let's assume ID is your primary key for this table.
Change your query to read
INSERT INTO
imgexam (id, image, name)
VALUES (1, '{$imageData}', '{$_FILES['userfile']['name']}')
ON DUPLICATE KEY UPDATE
image = '{$imageData}';
Update query for BLOB things:
mysql_escape_string() just treats the string as raw bytes, and adds escaping where it believes it's appropriate.
$query = "UPDATE mytable SET blobthing = '" .mysql_escape_string($varblobthing) .
"' WHERE id=2";

Update MySql values with it's current value in php

I have a column called name in a table info
So I want to update that with it's current value, like this name + user_input. I tried with this code but not working
mysql_query("UPDATE info SET name = name + '$user_input' WHERE id='$user_id'");
But It returns 0 and update column to 0....
Any idea how to accomplish this task??
You should use CONCAT to concatenate string in MySQL (+ is for addition arithmetic operations which I guess can't properly work with names) :
UPDATE info SET name = CONCAT(name,'$user_input') WHERE id='$user_id'

Using a SELECT Query to look up a UPDATE Query on MySQL

I'm using a SELECT query to obtain a variable using mysql_fetch_assoc. This then puts the variable into an UPDATE variable to put the returned value back into the database.
If I hard code the value, or use a traditional variable and it goes in just fine, but it doesn't work when using a value previously retrieved from the database. I've tried resetting the array variable to my own text and that works.
$arrgateRetrivalQuery = mysql_query(**Select Query**);
$arrGate = mysql_fetch_assoc($arrgateRetrivalQuery);
$arrivalGateTest = $arrGate['gatetype'];
$setGateAirportSQL = "UPDATE pilots SET currentgate = '".$arrivalGateTest."' WHERE pilotid = '".$pilotid."'";
$setGateAirportQuery = mysql_query($setGateAirportSQL);
// Close MySQL Connection
mysql_close($link);
This will just make the field to update have nothing in it, however whenever I remove the variable from the SELECT to one I define, array or not, it will work.
Hope this is clear enough. Thanks in advance.
Is arrivalGateTest a number or a string? How did you try to put another value in the query? If you are sure the previous query returns a value, try to write: $setGateAirportSQL = "UPDATE pilots SET currentgate = '$arrivalGateTest' WHERE pilotid = '$pilotid'";.
Just change your sql to inlcude a subquery.
You could use the following general syntax:
UPDATE pilots SET currentgate = (SELECT gate FROM airport WHERE flight='NZ1') WHERE pilotid='2';
which is demonstrated on this fiddle
This saves the extra query and more accurately describes what you are trying to achieve.
WARNING - test it carefully first!

how to add prefix in auto generated field value in database table?

we have a auto-generated field in database table and we want to add prefix in auto-generated value Like AVL0001.
So you could do it a couple ways... Is this an auto-index field in the database? If it is an integer type, then you won't be able to include data like you are mentioning above, however, if it is something generated from a script, just concatenate the number and your prefix before insert. You could probably also do this with a trigger on the database. Any additional details would help improve this answer.
$currentdbvalue = 'example';
$prefix = 'AVL0001';
$newvalue = $prefix.$currentdbvalue;
outputs "AVL0001example"
or if you'd like an underscore u can use:
$newvalue = $prefix."_".$currentdbvalue;
which would output "AVL0001_example"
Let id be your table column. You can add AVL ahead of your id by concatenating both in your sql query.
ie In Mysql,
$yourid="1";
INSERT INTO table( id )
VALUES (
CONCAT( "AVL", $yourid, id )
)
Or you can concatenate the AVL with yourid before inserting it into database like,
$yourid="AVL"."1";
In either way you cannot add it into an auto incrementing field. Because its type is INT.

add to a mysql column instead of replacing?

I want to add an extra value to a mysql table column, e.g:
the field 'photos' already has an image name in, but i want to add another image name into the field using php.
I thought INSERT would just add it in on top but it seems to replace the existing image name. I want to avoid selecting the current field values, adding the new value and re-inserting if possible.
thanks!
code:
php:
$query = "INSERT INTO `listings-rent` (photos) VALUES ('$fileName') WHERE id = '$insertID'";
mysqli_query($mysqli, $query);
the problem is this replaces the existing value in the 'photos' column, rather than adding to it
It is highly discouraged, to keep multiple values in the same column. But it is, of course possible.
UPDATE `photos` SET imagename = CONCAT(imagename, ",", $second_name) WHERE id=$insertID'
You will then use explode in PHP to get an array of strings like:
array("image1.jpg", "image2.jpg")
You insert with a WHERE statement. This will not work.
try:
$query = "INSERT INTO `listings-rent` (photos,id) VALUES ('$fileName', $insertID)";
mysqli_query($mysqli, $query);

Categories