I got 12 images and i want to update/edit them with a form. I choose to update one or more but not all of them, but keep the old image name in database.
I tried the following code but when i update one or two images, it's not keeping the old ones and the table field is null.
foreach($_FILES['more_images']['name'] as $key => $post_image) {
$getOld_ImageName = mysql_query("SELECT `image` FROM `more_images` WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
$getOld_Image = mysql_fetch_array($getOld_ImageName);
$more_imageOld = $getOld_Image['image'];
if (isset($post_image)) {
$file_name = $_FILES['more_images']['name'][$key];
$file_tmp = $_FILES['more_images']['tmp_name'][$key];
if($file_name == $more_imageOld) {
$updateMore_Images = mysql_query("UPDATE `more_images` SET `image` = '$more_imageOld' WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
} else {
move_uploaded_file($file_tmp, "$uploaddir/$file_name");
$updateMore_Images = mysql_query("UPDATE `more_images` SET `image` = '$file_name' WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
}
} else {
if ($key == $getOld_Image['key']) {
$post_image = $more_imageOld;
$updateMore_Images = mysql_query("UPDATE `more_images` SET `image` = '$post_image' WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
}
}
}
The $_FILE returns the new image names and blank on images that im keeping.
0 =
1 = 4061857_1_b.jpg
2 = 4061857_2_b.jpg
3 = 4061857_3_b.jpg
4 = 4061857_4_b.jpg
5 = 4061857_5_b.jpg
6 =
7 =
8 =
9 =
10 =
11 =
In other words im trying to update the table and where every $key is blank to keep the previous image name in the table as it was.
Any suggestions ?
I changed the if (isset($post_image)) to if (!empty($post_image)) and it worked.
Related
I'm trying to catch all data from MySQL to check data already exists or not
if data already exists then it should update else insert new data.
I have 7 to 8 conditions in where clause, some of them are null
now the issue is
when i run query its not catching null column thats why its insert new data instead of updating
below is my code
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
if (mysqli_num_rows($fetch_stock) > 0) {
echo "UPDATE book_stock SET stock_count = stock_count + 1 WHERE book_name = '$book_name' AND author_id = $book_author AND author_id1 = $book_author1 AND author_id2 = $book_author2 AND author_id3 = $book_author3 AND author_id4 = $book_author4 AND author_id5 = $book_author5 AND author_id6 = $book_author6";
mysqli_query($connection,
"UPDATE book_stock
SET stock_count = stock_count + 1
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
} else {
echo "INSERT INTO book_stock (book_name, author_id, author_id1, author_id2, author_id3, author_id4, author_id5, author_id6, stock_count) value ('$book_name', $book_author,$book_author1, $book_author2, $book_author3, $book_author4, $book_author5, $book_author6, 1)";
mysqli_query($connection,
"INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2,
author_id3, author_id4, author_id5, author_id6,
stock_count)
value ('$book_name', $book_author,$book_author1, $book_author2,
$book_author3, $book_author4, $book_author5, $book_author6, 1)");
}
When i print query Its look like this
INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2, author_id3,
author_id4, author_id5, author_id6, stock_count)
value ('cat demo1', 1,2, 3, 4, NULL, NULL, NULL, 1)
I got solution for this from 1 website, which is
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND IFNULL(author_id1, 0)= IFNULL($book_author1, 0)
AND IFNULL(author_id2, 0)= IFNULL($book_author2, 0)
AND IFNULL(author_id3, 0)= IFNULL($book_author3, 0)
AND IFNULL(author_id4, 0)= IFNULL($book_author4, 0)
AND IFNULL(author_id5, 0) = IFNULL($book_author5, 0)
AND IFNULL(author_id6, 0) = IFNULL($book_author6, 0)"
);
Its working when i want to display stock.
But i want to do a crud operation by using this logic.
I tried to do from above solution, its work fine but, in db table its update null with 0.
which is not got as per me.
please guide me with best solution.
Thanks
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.
I have the following code to write the name and score of a player into a highscore table. How can I write the 'name' in uppercase into the database?
if(isset($_GET['name']) && isset($_GET['score'])) {
$name = strip_tags(mysql_real_escape_string($_GET['name']));
$score = strip_tags(mysql_real_escape_string($_GET['score']));
$checkExist = mysql_query("SELECT `name`, `score` FROM `$tbl_name` WHERE `name` = '$name'");
$row = mysql_fetch_assoc($checkExist);
if (mysql_num_rows($checkExist) > 0){
if ($score > $row['score']){
$sql = mysql_query("UPDATE `$tbl_name` SET `score` = '$score' WHERE `name` = '$name'");
} else {
// ERROR MSG: Your new score is lower.(not updating the database)
}
} else {
$sql = mysql_query("INSERT INTO `$tbl_name` (`id`,`name`,`score`) VALUES ('','$name','$score');");
}
$name = strtoupper(strip_tags(mysql_real_escape_string($_GET['name'])));
http://php.net/manual/en/function.strtoupper.php
Either use strtoupper() in PHP, or UPPER() in MySQL: both do exactly the same, it's up to you.
I have the following table:
CREATE TABLE list(
country TINYINT UNSIGNED NOT NULL,
name VARCHAR(10) CHARACTER SET latin1 NOT NULL,
name_index INT UNSIGNED NOT NULL,
UNIQUE KEY(country, name), PRIMARY KEY(country, name_index)) ENGINE = INNODB
I want to:
Given: ($country, $name, $new_index)
Check if a row with country = $country && name = $name exists.
If the row exists, get the index $index = name_index.
If the row doesn't exist, add it and then get the index.
I can do the following using many queries, but I am looking for an efficient way to do it, using only one query. Is this possible?
It's not possible with only one query.
You CAN do this:
$sql = "SELECT name_index FROM (your table) WHERE country = '$country' AND
name = '$name' LIMIT 1";
$query = mysql_query($sql);
$numrows = mysql_num_rows($query);
if($numrows == 1) {
$row = mysql_fetch_row($query);
$index = $row[0];
} else {
$sql = "INSERT INTO (your table) (country, name)
VALUES('$country','$name')";
$query = mysql_query($sql);
$check = mysql_num_rows($query);
if($check > 0) {
$sql = "SELECT name_index FROM (your table) WHERE country = '$country' AND
name = '$name' LIMIT 1";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
$index = $row[0];
} else {
echo "Error occured while trying to insert new row";
}
}
Hope this helps :).
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