Reputation: 620
What would the correct syntax to update a table be while using PHP to make the connection? If I manually change a row on Wamp server it shows me this:
UPDATE `proyecto`.`labs` SET `estado` = '2' WHERE `labs`.`idlab` =1;
But a quick Ctrl+C and Ctrl+V on my code doesn't do anything, nor does it trigger any error. For reference here is my PHP code:
$db_hostname = 'localhost';
$db_database = 'XXX';
$db_username = 'XXX';
$db_password = 'XXX';
$db_server = db_init();
function db_init(){
global $db_hostname,
$db_database,
$db_username,
$db_password;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server)
die ("Unable to connect with MySQL: " . mysql_error());
mysql_select_db($db_database)
or die ("Unable to connect with database " . mysql_error());
return $db_server;
}
function do_query($query){
global $db_server;
$result = mysql_query($query , $db_server);
if( !$result)
die("Failed sentence: " . mysql_error());
return $result;
}
This is the function I am trying to invoke, it is being call via jQuery, just in case I am adding the code below:
function labStatusEliminar(){
if(isset($_POST['pId'])){
$idEliminar = $_POST['pId'];
$query = "UPDATE bk.estado FROM labs as bk WHERE bk.idlab = $idEliminar";
}
}
jQuery:
$(".btnEliminar").click(function() {
var idAttrE = $(this).attr('id');
var idEliminar = parseInt(idAttrE.substring(12));
var request = $.ajax({
url: "includes/functionsLabs.php",
type: "post",
data: {
'call': 'labStatusEliminar',
'pId':idEliminar},
dataType: 'html',
success: function(response){
$('#info').html(response);
}
});
});
Upvotes: 0
Views: 83
Reputation: 11665
The correct syntax for UPDATE is:
UPDATE `table_name`
SET `column_name` = 'value', `column_name`='value'...
WHERE `column_name` = 'value'
Upvotes: 1
Reputation: 12798
Looks like your UPDATE
statement is missing the SET
part.
$query = "UPDATE bk.estado FROM labs as bk WHERE bk.idlab = $idEliminar";
That probably should be
$query = "UPDATE labs SET estado=2 WHERE labs.idlab = $idEliminar";
I'd also suggest you look into mysqli or PDO and use prepared statements instead. You code is currently vulnerable to SQL injection.
Upvotes: 1