Hi guys, I have a php code that displays a list of dishes where a user can check on those dishes and then save the selection to the database. My code works find when selecting and saving the data. But the problem is that when I try to uncheck one of the dishes that is previously been saved, it won't remove the unchecked dish in the database.
Here is the class file I am using:
class.php
<?php
class Food {
private $_con;
function __construct(){
$this->_con = mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('food', $this->_con) or die(mysql_error());
}
function getDishes(){
$query = 'SELECT * FROM dish';
$result = mysql_query($query);
$data = array();
if($result){
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
return $data;
}
}
function getPersons(){
$query = 'SELECT * FROM person';
$result = mysql_query($query);
$data = array();
if($result){
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
}
return $data;
}
function getPersonData($personID){
$query = 'SELECT * FROM person WHERE personid='.$personID;
$result = mysql_query($query);
$data = array();
if($result){
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
}
return $data;
}
function insertPersonDish($personID, $dishID){
$query = 'SELECT * FROM persondish WHERE personID='.$personID.' AND dishid='.$dishID;
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
return true;
} else {
$insert = 'INSERT INTO persondish(personid, dishid) VALUES('.$personID.','.$dishID.')';
$result = mysql_query($insert);
return mysql_insert_id();
}
}
function deletePersonDish($personID, $dishID){
$query = 'DELETE FROM persondish WHERE personid='.$personID.' AND dishid='.$dishID;
return (mysql_query($query));
}
}
Related Article:Help with php and mysql
is a solved PHP discussion thread by stevenbeatsmith that has 6 replies, was last updated 1 year ago and has been tagged with the keywords: dreamweaver, mysql, php.
Have you tried debugging it by putting echos or var_dumps etc inside your deletePersonDish function? Personally I would echo out the query to see if it what you are expecting, i.e. your are recieving the expected values through the parameters:
function deletePersonDish($personID, $dishID){
$query = 'DELETE FROM persondish WHERE personid='.$personID.' AND dishid='.$dishID;
echo $query;
return (mysql_query($query));
}
This helps you better pinpoint where you are going wrong.
@bradly I don't need to update the record. an Example if the selected dish is already in the database, it will not do anything thus if the user clicks on the button but did not click 2 dishes (where the 2 dishes is in the database) then it will be deleted because it is not checked or chosen by the user.
@bops I already did echo the query and it did return the right sql statement for delete. I don't need to var_dump it because it is just a delete query. The think is that when I check only the 3rd dish, it will insert all the dish where not what I want.
Example if I check only 1 dish, that 1 dish should be inserted and the other dish that is not checked should be deleted.