i am sending the arraylist from android to php. i want replace old menunames as new menunames. in my table contains two columns 1.menuimage 2.menuname.. i want to update only new menunames
my table structure
test(table name)
menucode menuimage menuname
1 image p
2 " q
3 " r
4 " s
5 " t
i want update only menuname according to the menucode ... i am passing menucode[1,2,3,4,5,6,7]...
<?php
$old_menu_code = explode(",", str_replace(array("[","]"), "", $_POST['menucode']));
$new_menu_names = explode(",", str_replace(array("[","]"), "", $_POST['editmainmenu']));
mysql_connect("localhost", "root", "root");
mysql_select_db("test");
foreach ($old_menu_names as $key => $old_name) {
$new_name = mysql_real_escape_string($new_menu_names[$key]);
$old_code = mysql_real_escape_string($old_menu_code[$key]);
mysql_query("UPDATE `test` SET `menuname` = '$new_name' WHERE menucode= '$old_code'") or die('Error' . mysql_error());
echo "Updated";
}
?>
the above code test is database contains test table.in test table contains two columns one menuimage and menuname . in menuimage contains 7 images and menuname contains 7 menunmaes. i want only update menuinames. but above code update only first row menunames column. remaining rows are not updated..please what mistake i have been done
you can use str_replace
& explode
to achieve what you want ..
Spiting Data
$old_menu_names = "[p,q,r,s,t,u]" ; // or $old_menu_names = $_POST['menuname'] ;
$new_menu_names = "[a,b,c,d,e,f]" ; // or $new_menu_names = $_POST['editmainmenu'] ;
$old_menu_names = explode(",", str_replace(array("[","]"), "", $old_menu_names));
$new_menu_names = explode(",", str_replace(array("[","]"), "", $new_menu_names));
SQL ERROR
mysql_query("UPDATE `test` SET `menuname` = '$new_name' WHERE menuname = '$old_name'")
What statement would not work because you are trying to update a filed menuname
that you are using in where condition WHERE menuname =
I would recommend you change it to something like memuid
if it exist in your table or get the ID
first and use it to update it .
EDIT 1
Updated Code
$mysql = new mysqli("localhost","root","root","test");
$sql = "SELECT menucode FROM test where menuname = '%s' ";
foreach ($old_menu_names as $key => $old_name) {
$new_name = mysql_real_escape_string($new_menu_names[$key]);
$old_code = mysql_real_escape_string($old_menu_names[$key]);
$result = $mysql->query(sprintf($sql,$old_code))->fetch_assoc();
if(!empty($result['menucode']))
{
$mysql->query(sprintf("UPDATE `test` SET `menuname` = '%s' WHERE menucode= '%d'",$new_name,$result['menucode']));
echo "Updated " , $mysql->affected_rows . PHP_EOL;
}
else
{
echo "Missing menucode for $old_code " , PHP_EOL;
}
}
I hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.