php - What's wrong with this update query?

353

I'm trying to make a form in a website where you can write the name, price or image of the product you want to update and in the textbox next to every field you can write the new value you want it to take but idk why everytime I write something the only field that updates is the name and instead of change the former name for the new one it change to 0, the php code I'm using is this:

<?php

mysql_connect("localhost","paradise_root","gantzminus1");
mysql_select_db("paradise_dbsalon");
$nombre = $_POST['txtnombre']; 
$precio = $_POST['txtprecio'];  
$imagen = $_POST['txtimagen'];
$nombre2 = $_POST['txtnombre2']; 
$precio2 = $_POST['txtprecio2'];  
$imagen2 = $_POST['txtimagen2'];

$_GUARDAR_SQL = "update productosyservicios set nombre = ('".$nombre2."') or precio = ('".$precio2."') or imagen = ('".$imagen2."') where nombre = ('".$nombre."') or precio= ('".$precio."') or imagen = ('".$imagen."') "; 

$rst = mysql_query($_GUARDAR_SQL) or die("error".$_GUARDAR_SQL); 
if($rst==true){
$rest=false;

}
?>
628

Answer

Solution:

this gona be with multiple CASE :

Try that:

UPDATE productosyservicios SET 
nombre = CASE WHEN nombre = '".$nombre."' then '".$nombre2."' else nombre   END ,
precio = CASE WHEN precio = '".$precio."' then '".$precio2."' else precio   END ,
imagen = CASE WHEN imagen = '".$imagen."' then '".$imagen2."' else imagen   END
654

Answer

Solution:

$_GUARDAR_SQL = "update productosyservicios set nombre = ('".$nombre2."'), precio = ('".$precio2."'), imagen = ('".$imagen2."') where nombre = ('".$nombre."') or precio= ('".$precio."') or imagen = ('".$imagen."') "; 

You hador in your UPDATE

(UPDATE productosyservicios set nombre = something **OR** <- this shouldnt be or it should be a comma

updates should be seperated by commas

118

Answer

Solution:

Your query is off.

$_GUARDAR_SQL = "UPDATE productosyservicios SET nombre = '".$nombre2."', precio = '".$precio2."', imagen = '".$imagen2."' WHERE nombre = '".$nombre."' OR precio = '".$precio."' OR imagen = '".$imagen."'"; 
364

Answer

Solution:

Your SQL query is very bad (not correct).

You must change to:

$_GUARDAR_SQL = "update productosyservicios set nombre = '".$nombre2."' , precio = '".$precio2."' , imagen = '".$imagen2."' where nombre = '".$nombre."' or precio= '".$precio."' or imagen = '".$imagen."' "; 

I'm not sure for your where clauses.

Also check your database. Somewhere you can have integer column for text - change to varchar for all texts.

People are also looking for solutions to the problem: php - Dynamically created multi select box values not inserted correctly

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.