I have an UPDATE query and using Ajax, I wanted to know if any value is empty can I only update the values that not empty in the database. I don't know if this is possible to have a if statement or something to check to skip the empty values. I know I can just add another form element but just wanted to know if there was another solution.
Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',
$id = $_POST['id'];
$title = "";
$description = $_POST['Description'];
$date = $_POST['Date'];
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id;
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
Update: This is what worked for me. Thanks Karim Daraf
$query = " UPDATE user SET
Title = Coalesce($title,Title ) etc...
Try it with Coalesce .
$query = " UPDATE user
SET
`Title` = CASE WHEN `Title`='' or `Title` IS NULL THEN '$title' END,
`Description` = CASE WHEN `Description`='' Or `Description` IS NULL THEN '$description' END,
`Date` = CASE WHEN `Date`='' Or Date` IS NULL THEN '$date' END
WHERE `id` = '".$id."' ";
or :
$query = " UPDATE user
SET
`id` = Coalesce('$id''".$id."' , NULLIF(`id`,'')),
`Title` = Coalesce('$title''".$title."',NULLIF(`Title`,'') ) ,
`Description` = Coalesce('$description''".$description."' , NULLIF(`Description`,'') ) ,
`Date` = Coalesce('$date''".$date."',NULLIF(`Date`,''))
WHERE `id` = '$id''".$id."' ";
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = COALESCE(NULLIF("'.$title.'", ""),`Title`),
`Description` = "'.$description.'",
`Date` = "'.$date.'"
WHERE `id` = "'.$id.'"';
Not sure to understand: you have data and want to update, but only if some fied in the DB are empty?
In the case perfom only a where:
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id.' AND Title = '';
for example
Related
I want to check if a row value from my database is NULL or empty so I did the following:
$existcheck = "
SELECT *
FROM snm_content";
$existcheckcon = $conn->query($existcheck);
$existcheck = $existcheckcon->fetch_assoc();
if((!empty($existcheck['plaats'])) AND (!empty($existcheck['straat']))){
echo 'vol';
}else{
$vultable = "
ALTER TABLE snm_content
ADD (straat varchar(255) NULL,
plaats varchar(255) NULL)";
$vultablecon = $conn->query($vultable);
$vultable = $vultablecon->fetch_assoc();
$updatetable = "
UPDATE snm_content
SET straat = '".$straatnaam."', plaats = '".$bedrijven['plaats']."'";
$updatetablecon = $conn->query($updatetable);
$updatetable = $updatetablecon->fetch_assoc();
}
But I get the following message:
There was an error running the query [Duplicate column name 'straat']
Both straat and plaats in snm_content are all filled with NULL.
So why does it get to the else statement if the values are NULL?
I should mention, this code is inside a while loop from another query.
You should check if the column exist not if column have a data so you have to do like this :
$existcheck = "DESC snm_content";
$existcheckcon = $conn->query($existcheck);
$existcheck = $existcheckcon->fetch_assoc();
if((!empty($existcheck['plaats'])) AND (!empty($existcheck['straat']))){
echo 'vol';
}else{
$vultable = "
ALTER TABLE snm_content
ADD (straat varchar(255) NULL,
plaats varchar(255) NULL)";
$vultablecon = $conn->query($vultable);
$vultable = $vultablecon->fetch_assoc();
$updatetable = "
UPDATE snm_content
SET straat = '".$straatnaam."', plaats = '".$bedrijven['plaats']."'";
$updatetablecon = $conn->query($updatetable);
$updatetable = $updatetablecon->fetch_assoc();
}
I'm trying to figure how to call a query once.
I have 6 different variables for images, title and desc.
In this code, I need to know how to loop for id from 0 to 6.
$date = new DateTime("NOW");
$image1 = 'SSSS';
$title1 = 'AAAA';
$desc1 = 'BBBB';
$image2 = 'RRRR';
$title2 = 'GGGG';
$desc2 = 'VVVV';
/// 4 vars later....
$id = 6;
$get = $this->db->queryRow("UPDATE `featured` SET `image` = '{$image.$id}', `title` = '{$title.$id}', `desc` = '{$desc.$id}', `date` = '{$date->format('Y-m-d H:i:s')}' WHERE id = '{$id}'");
return(object) $get;
To build a collection of Querys use the multi_query function.
Loop to build your Query string to pass to the db and concatenated by a semicolon.
<?php
for($i=0;$i <= $maxquerys;$i++){
$query = "UPDATE `featured` SET `image` = '".$image.$id."', `title` = ".$title.$id."', `desc` = '".$desc.$id."', `date` = '".$date->format('Y-m-d H:i:s')."' WHERE id = '".$id."';"
}
/* execute multi query */
if ($mysqli->multi_query($query)) {
while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
you may check also the result by
echo $mysqli->affected_rows;
?>
I have tried to use a simple $query model and it works fine.
Create a valid Query string to pass to the db
<?php
$query = "UPDATE `featured` SET `image` = '".$image.$id."', `title` = ".$title.$id."', `desc` = '".$desc.$id}."', `date` = '".$date->format('Y-m-d H:i:s')."' WHERE id = '".$id."';"
$result=$mysqli->query($query);
// Verify results
if(!$result) {
$ErrMessage = "ErrSqlQuery:" . $mysqli->error . "\n";
$mysqli->close();
die($ErrMessage);
}
you can check also the result by
echo $mysqli->affected_rows;
?>
$query_build = "";
foreach($arr as $$image){
$query_build .= "UPDATE `featured` SET `image` = '{$image.$id}', `title` = '{$title.$id}', `desc` = '{$desc.$id}', `date` = '{$date->format('Y-m-d H:i:s')}' WHERE id = '{$id}';";
}
$get = $this->db->queryRow($query_build);
Accumulate all the queries and execute all at once.
for(some loop condition):
mysql_query("UPDATE `details` SET
`url_battlelog` = '".$stats[$out]['url_battlelog']."',
`url_bf3stats` = '".$stats[$out]['url_bf3stats']."',
`rank_img_medium` = '".$stats[$out]['rank_img_medium']."',
`country_name` = '".$stats[$out]['country_name']."',
`country` = '".$stats[$out]['country']."',
`country_flag` = '".$stats[$out]['country_flag']."',
`rank_number` = '".$stats[$out]['rank_number']."',
`score_total` = '".$stats[$out]['score_total']."',
`time_total` = '".$stats[$out]['time_total']."',
`dogtag_basic_img` = '".$stats[$out]['dogtag_basic_img']."',
`dogtag_basic` = '".$stats[$out]['dogtag_basic']."',
`dogtag_advance_img` = '".$stats[$out]['dogtag_advance_img']."',
`dogtag_advance` = '".$stats[$out]['dogtag_advance']."'
WHERE `name_player` = '".$stats[$out]['name_player']."'
")
or die(mysql_error());
for(2nd loop condition):
mysql_query("UPDATE `weapons` SET
`img` = '".$gun_img."',
`name` = '".$gun_name."',
`kit` = '".$gun_kit."',
`time` = '".$gun_time."',
`kills` = '".$gun_kills."',
`headshots` = '".$gun_hs."',
`shots` = '".$gun_shots."',
`hits` = '".$gun_hits."',
`star_total` = '".$gun_star_c."',
`star_img` = '".$gun_star_i."',
`star_need` = '".$gun_star_n."',
`rank_curr` = '".$gun_rank_c."',
`rank_all` = '".$gun_rank_w."',
`desc` = '".$gun_desc."',
`category` = '".$gun_cat."',
`range` = '".$gun_range."',
`fire_rate` = '".$gun_fire_rate."',
`ammo` = '".$gun_ammo."',
`auto_fire` = '".$gun_fire_auto."',
`burst_fire` = '".$gun_fire_burst."',
`single_fire` = '".$gun_fire_single."',
`unlock_total` = '".$unlock_total."',
`unlock_done` = '".$unlock_done."',
`unlock_p` = '".round($unlock_p)."'
WHERE `name_player` = '".$stats[$out]['name_player']."'
")
or die(mysql_error());
the problem is that only 2nd table (weapons) is updating, 1st table (details) is not showing any changes. Doesn't show any error.
I have same type of script for inserting data into both tables and its working fine.
I'm new to MySQL and PHP. sorry for bad English....
did you test by writing your SQL ? I think your [WHERE] condition is not fully filled
$sql = "UPDATE `shows` SET `title` = '$title', `tagline` = '$tagline', `desc` = '$desc' , `img_src = '$imgsrc' WHERE id = $showid";
The query above does not want to work, I simply get a mysql_error saying error at '' on line 1;
Any idea where I am going wrong?
You're missing a tick:
`img_src = '$imgsrc' WHERE id = $showid";
should be:
`img_src` = '$imgsrc' WHERE id = $showid";
If i'm doing multiple mysql queries on the same table, occasionally some get skipped.
Why is that?
For example:
<?php
mysql_query("UPDATE `tb` SET `field` = '' WHERE `Id` = '$something'");
mysql_query("UPDATE `tb` SET `field2` = '' WHERE `Id` = '$something'");
mysql_query("UPDATE `tb` SET `field3` = '0' WHERE `Id` = '$something'");
?>
Sometimes one of the queries will not be executed?
Why is that?
-Or is it something wrong with my server not general mysql?
(Obviously I know now to update the same table in the same query, but before that I was very confused as to why it happens, can anyone please explain?)
Thanks!
You do not need to make 3 queries, if you are updating the same rows:
$q = "
UPDATE table
SET field = '',
field2 = '',
field3 = 0
WHERE Id = :id
";
$statement = $pdo->prepare( $q );
$statement->bindParam(':id', $something, PDO::PARAM_INT);
$statement->execute();
Also, you should stop using the ancient mysql_* functions. They are not maintained anymore and process for deprecation has already begun.
Maybe you should avoid the 10+ year old API and learn something for this decade: PDO Tutorial for MySQL Developers.
debug your code to see if a query fails:
$result = mysql_query("UPDATE `tb` SET `field` = '' WHERE `Id` = '$something'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$result = mysql_query("UPDATE `tb` SET `field2` = '' WHERE `Id` = '$something'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$result = mysql_query("UPDATE `tb` SET `field3` = '0' WHERE `Id` = '$something'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Use the following to debug the code:
mysql_query("UPDATE `tb` SET `field` = '' WHERE `Id` = '$something'") or die(mysql_error());
mysql_query("UPDATE `tb` SET `field2` = '' WHERE `Id` = '$something'") or die(mysql_error());
mysql_query("UPDATE `tb` SET `field3` = '0' WHERE `Id` = '$something'") or die(mysql_error());
UPDATE
Make sure you are escaping $something using:
$something = mysql_real_escape_string($something);