Basicaly what im trying to is... I'm trying to store data if Room_id and User_id doesn't Exists. and update if exsist... Whenever im runing this below mention query im getting 25 row entry with same data in each row... Also when im trying to update its not working... Im using Array on my form page.
function room_insert_data($room_insert_data) {
global $session_user_id;
$updates = array();
array_walk($room_insert_data, 'array_sanitize');
foreach($room_insert_data as $bfield=>$bdata) {
$field = '`' . implode('`, `', array_keys($room_insert_data)) . '`';
$data='\'' . implode('\', \'', $room_insert_data) . '\'';
$updates[] = '`' . $bfield . '` = \'' . $bdata . '\'';
if($room_insert_data['room_id'] == false) {
mysql_query("INSERT INTO `room_type` ($field) VALUES ($data)") or die(mysql_error());
} else {
mysql_query("UPDATE `room_type` SET " . implode(', ', $updates) . " WHERE `room_id` = '$room_id' AND `user_id` = '$user_id'") or die(mysql_error());
}
}
}
Would appricate if anyone can help me here... Or else just let me know why am i getting 25 row entry instent of 1...
You should only be looping over the array when you're building up the UPDATE
query. You're getting 25 inserts because there are 25 columns in your table, and you're doing an INSERT
while you loop through all of them. And the UPDATE
isn't working right because you're doing it over and over for each column, and not initializing the array properly.
if ($room_insert_data['room_id'] == false) {
$field = '`' . implode('`, `', array_keys($room_insert_data)) . '`';
$data='\'' . implode('\', \'', $room_insert_data) . '\'';
mysql_query("INSERT INTO `room_type` ($field) VALUES ($data)") or die(mysql_error());
} else {
$updates = array();
foreach($room_insert_data as $bfield=>$bdata) {
$updates[] = '`' . $bfield . '` = \'' . $bdata . '\'';
}
mysql_query("UPDATE `room_type` SET " . implode(', ', $updates) . " WHERE `room_id` = '$room_id' AND `user_id` = '$user_id'") or die(mysql_error());
}
You're imploding the values of $room_insert_data inside your loop... i.e. you're using every value in 1 query, but you're repeating the INSERT query for every field in $room_insert_data. It seems like you need the loop to build the $updates array, but you shouldn't be including running the query inside that loop.
It seems like what you really want to be doing is this:
function room_insert_data($room_insert_data) {
array_walk($room_insert_data, 'array_sanitize');
if(empty($room_insert_data['room_id'])) {
$field = '`' . implode('`, `', array_keys($room_insert_data)) . '`';
$data='\'' . implode('\', \'', $room_insert_data) . '\'';
mysql_query("INSERT INTO `room_type` ($field) VALUES ($data)") or die(mysql_error());
} else {
$updates = array();
foreach($room_insert_data as $bfield=>$bdata) {
$updates[] = '`' . $bfield . '` = \'' . $bdata . '\'';
}
$room_id = $room_insert_data['room_id'];
$user_id = $room_insert_data['user_id'];
mysql_query("UPDATE `room_type` SET " . implode(', ', $updates) . " WHERE `room_id` = '$room_id' AND `user_id` = '$user_id'") or die(mysql_error());
}
}