last day (14 days later) » 

3:02 PM
1
A: Check Checkboxes according to the data from the database

maraca<input ... <?php if (in_array($name["name"], split($name["diseases"], ","))) echo "checked"; ?> /> For XHTML you may want to write: echo 'checked="checked"';

 
(in_array from which table did you get that. I mean where are you selecting the table?
 
in_array is a standard php function, type php + what you want and you will find stack overflow or other useful articles. php.net/manual/en/function.in-array.php
 
also i cant understand what is in the ... can you write please the whole code???
 
... is the same as before. You can basically insert this php command anywhere you like in your input, but you have to be careful to add spaces or to be save return " checked " instead.
 
you mean something like that <input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>" <?php if (in_array($name["name"], $name["diseases"].split(","))) echo "checked"; ?> />
 
3:02 PM
yes, this seems to be correct, but the comments about the data design are true, comma seperated lists in a database are almost always bad. It can slow down requests incredibly.
 
i am getting an error . check it if you want(prntscr.com/6v8r68)
 
Yes, split was wrong JS style instead of PHP... split + PHP in search engine and you would have seen. PHP luckily gives helpful errors unlike others that give no error messages at all.
 
I saw that you edited your code but still i am getting errors (prntscr.com/6v8tpr)
 
I think this wasn't the newest version of it.
 
I am getting this error with this code <input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name["name"], split($name["diseases"], ","))) echo "checked"; ?> />
 
3:03 PM
you've got it now?
 
yes i am getting this errors prntscr.com/6v8voy
 
well, i don't know how your deseases column is actually called ;-)
 
the name of the table is patient and the column is disease
this is the query from i am getting the data
if($query = mysql_query("SELECT username,spid,gender,email,fname,lname,age,disease,password FROM patient WHERE username='$username'") or die(mysql_error()))
{

if(mysql_num_rows($query)>=1){
    while($row = mysql_fetch_array($query)) {
        $fname = $row['fname'];
        $lname = $row['lname'];
        $email = $row['email'];
        $password = $row['password'];
        $age = $row['age'];
        $disease = $row['disease'];
    }
 
then just change "diseases" to 'disease' (and "name" to 'name' to be consistent)
 
still the same
 
3:07 PM
ah even more esier
 
the whole code is this...
<?php

/**
 * @author Nick Bourlai
 * @copyright 2015
 */

include_once 'header.php';

$username = $_SESSION["username"];


if(isset($_POST['username'])){

            $fname = mysql_real_escape_string($_POST["fname"]);
            $lname = mysql_real_escape_string($_POST["lname"]);
            $email = mysql_real_escape_string($_POST["email"]);
            $password = mysql_real_escape_string($_POST["password"]);
            $age = ($_POST["age"]);
            $disease = implode(",",$_POST["disease"]);
 
you have those variables? $diseases and $name (or $lname and $diseases in your case), then just apply the new edit
I mean $disease and $lname in your case
 
yes i am having those variables
I sent you above the whole code
 
if (in_array($name, split($diseases, ","))) echo "checked";
if (in_array($lname, split($disease, ","))) echo "checked";
2nd one should work for you
 
and where I should place that?
 
3:14 PM
instead of the old if
in the input
 
No errors but still it doesnt check the diseases that i have in my database for this user
 
need to see the output
 
is it like <input ... checked />
 
The code is this
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($lname, split($disease, ","))) echo "checked"; ?> />
 
3:17 PM
no the result of that
what the browser gets
 
and how I do that?
 
firebug, right-click analyze, f12 on most browsers or display source
 
checked is never added
means it can never find $lname in $disease
you fetched the values before
 
why we want the $lname and not the $username
?
 
3:23 PM
no we want the name of the sickness
to be $name
 
so why do we have it $lname because $lname is the last name of the user
 
change it then, you know your columns best, i just didn't knew how they are called
 
the table is this (prntscr.com/6v959b)
so how should i do it
 
post input line again
code
 
if(isset($_POST['username'])){

            $fname = mysql_real_escape_string($_POST["fname"]);
            $lname = mysql_real_escape_string($_POST["lname"]);
            $email = mysql_real_escape_string($_POST["email"]);
            $password = mysql_real_escape_string($_POST["password"]);
            $age = ($_POST["age"]);
            $disease = implode(",",$_POST["disease"]);

            $query="UPDATE patient
                    SET fname = '$fname', lname = '$lname', email = '$email', password = '$password', age='$age', disease='$disease'
 
3:33 PM
the <input ... > line is not there
 
oups sry
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($lname, split($disease, ","))) echo "checked"; ?> /><br />
 
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($lname, split($disease, ','))) echo ' checked'; ?> />
 
same errors
 
<input type="checkbox" name="disease[]" value="<?php echo $DISEASENAME; ?>"<?php if (in_array($lname, split($disease, ','))) echo ' checked'; ?> />
put the right one in there
DISEASENAME i don't know what it is
or post the whole code
with the input line
 
the whole code
<?php

/**
 * @author Nick Bourlai
 * @copyright 2015
 */

include_once 'header.php';

$username = $_SESSION["username"];


if(isset($_POST['username'])){

            $fname = mysql_real_escape_string($_POST["fname"]);
            $lname = mysql_real_escape_string($_POST["lname"]);
            $email = mysql_real_escape_string($_POST["email"]);
            $password = mysql_real_escape_string($_POST["password"]);
            $age = ($_POST["age"]);
            $disease = implode(",",$_POST["disease"]);
 
3:40 PM
it was correct what i posted in the beginning, why you fetch into $name ? that causes the whole confusion
 
i cant understand what do you mean , in the input code echo $name['name'] is the name of the disease that I am getting from the database
 
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], split($disease, ','))) echo ' checked'; ?>
right?
 
same errors , maybe a problem is that this $sql = "SELECT name FROM disease"; is to get all the diseases from the database and this if($query = mysql_query("SELECT username,spid,gender,email,fname,lname,age,disease,password FROM patient WHERE username='$username'") or die(mysql_error())) is to get all the data and the stored diseases
 
yes this what i proposed only works if $disease is a global variable, fetches all diseases at program start into it and then forget about it
otherwise you need to also fetch all diseases
 
and how I do that :-/ ????
 
3:49 PM
i see you are currently only fetching all diseases
not all the diseases that the person has
 
yes because i should display all the diseases that's why
so I need a second sql?
 
you need to also fetch all the diseases of the person
 
$sql2 = "SELECT disease FROM patient WHERE username='$username'"; something like that
?
 
and store it as comma separated list in $disease
then it is going to work
yes
or you can also store it as array in $disease and remove the split, this is better
 
<?php
$sql = "SELECT name FROM disease";
$sql2 = "SELECT disease FROM patient WHERE username='$username'";
$query_resource = mysql_query($sql);
$query_resource2 = mysql_query($sql2);
while( $name = mysql_fetch_assoc($query_resource) && $name2 = mysql_fetch_assoc($query_resource2)):
?>
<span><?php echo $name['name']; ?></span>
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name2['name'], split($disease, ','))) echo ' checked'; ?> />"<br />

<?php endwhile; ?>
and i am getting this prntscr.com/6v9h57
 
3:53 PM
the while doesn't change
compared to before
 
can you show me how ?
 
while( $name = mysql_fetch_assoc($query_resource)):
?>
<span><?php echo $name['name']; ?></span>
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], split($disease, ','))) echo ' checked'; ?> />"<br />
ok lets start again
at the top
it's really annoying typing in here
 
i can share you the file if you want
 
$query_resource2 = mysql_query($sql2);
so far so good
 
ok
 
3:57 PM
then you need to do a while to fetch all entries of resource2 into an array, lets say $disease
(independent of the other while and before it)
you get that?
 
ok
yes
 
ok, then
while( $name = mysql_fetch_assoc($query_resource)):
...
 
ok
 
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], $disease)) echo ' checked'; ?> /><br />
the rest is the same
 
wait a sec
 
4:00 PM
and as in the answer, if you use XHTML it is echo 'checked="checked"'
no... echo ' checked="checked"' (space at the beginning)
 
you mean something like that ...
    <?php
  $sql = "SELECT name FROM disease";
  $sql2 = "SELECT disease FROM patient WHERE username='$username'";
  $query_resource = mysql_query($sql);
  $query_resource2 = mysql_query($sql2);
  while( $name = mysql_fetch_assoc($query_resource2)):
  while( $name = mysql_fetch_assoc($query_resource) ):
?>
    <span><?php echo $name['name']; ?></span>
    <input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], $disease)) echo ' checked'; ?> /><br /><br />
 
no, the first endwhile has to be before the first while
 
ok
    <?php
  $sql = "SELECT name FROM disease";
  $sql2 = "SELECT disease FROM patient WHERE username='$username'";
  $query_resource = mysql_query($sql);
  $query_resource2 = mysql_query($sql2);
  while( $name = mysql_fetch_assoc($query_resource2)):
  <?php endwhile; ?>
  while( $name = mysql_fetch_assoc($query_resource) ):
?>
    <span><?php echo $name['name']; ?></span>
    <input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], $disease)) echo ' checked'; ?> /><br /><br />
I am confused , can you write exactly please the changes ? Thank you :)
 
while( $name = mysql_fetch_assoc($query_resource2)):
$disease.push($name); // or however you add values to an array
php endwhile; ?>
before the while $disease = array() or something to initialize probably also needed
 
    <?php
  $sql = "SELECT name FROM disease";
  $sql2 = "SELECT disease FROM patient WHERE username='$username'";
  $query_resource = mysql_query($sql);
  $query_resource2 = mysql_query($sql2);
  $disease = array()
  while( $name = mysql_fetch_assoc($query_resource2)):
  $disease.push($name);
  <?php endwhile; ?>
  while( $name = mysql_fetch_assoc($query_resource) ):
?>
    <span><?php echo $name['name']; ?></span>
    <input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], $disease)) echo ' checked'; ?> /><br /><br />
Parse error: syntax error, unexpected '<' in E:\xampp\htdocs\ptixiaki\edit_patient_profile.php on line 64
 
4:08 PM
yes... but this isn't the correct syntax, array() could also be Array()... i don't know, and it needs a ; at the end, also .push is probably not a function in php it has to be written different
 
it doesnt show me an error on .push
 
i only write the skeleton in php, the rest is dynamically retrieved in ajax... so i don't bother learning this stuff by heart
the first end while shouldnt be in <? ?>
 
and what should it be?
 
well nothing, just endwhile;
the .push error will probably show up now;-)
 
Parse error: syntax error, unexpected 'endwhile' (T_ENDWHILE) in E:\xampp\htdocs\ptixiaki\edit_patient_profile.php on line 66
<!--emfanizei tis epiloges gia ta diseases me basi auta p exoume sti basi mas -->
<?php
$sql = "SELECT name FROM disease";
$sql2 = "SELECT disease FROM patient WHERE username='$username'";
$query_resource = mysql_query($sql);
$query_resource2 = mysql_query($sql2);
$disease = Array()
while( $name = mysql_fetch_assoc($query_resource2)):
$disease.push($name);
endwhile;
while( $name = mysql_fetch_assoc($query_resource) ):
?>
<span><?php echo $name['name']; ?></span>
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>"<?php if (in_array($name['name'], $disease)) echo ' ch
 
4:14 PM
ok .push doesn't show up, but it has to be the problem, google the correct syntax
how to add element to array php
 
semicolon still missing after Array();
 
Fatal error: Call to undefined function push() in E:\xampp\htdocs\ptixiaki\edit_patient_profile.php on line 65
PUSH :P
 
yes
 
$disease.array_push($name);
 
4:19 PM
array_push($disease, $name);
but now that I know how your database probably looks like, you could have done it much easier
 
still doesnt do what I want it to do
 
do it all in SQL, so that you get the name of each disease and another column that says you if checked or not... then 1 loop like before
 
you mean something like the 3rd answer of my question stackoverflow.com/questions/29718568/…
 
no, that is what we are doing now
basically the same as i suggested
it is just general pseudo code (which is a good thing to show concepts), mine is php specific
 
I am really confused can you write the code please because i cant understand what exactly i should do
 
4:28 PM
by now you should know all the steps, I can't be your personal debugger, look at the output in the browser, see if the checked is there in the input, if not see why the if is never true... etc.
 
but if i do it like you said and if in the future change some diseases from my database then i should change the code and this is not good :/
 
no, why?
 
because if a have if statements for all the diseases and then i change for example alergy to alergy22 then i should go to the code and correcct this line
 
best solution: create an sql query returning 2 columns: disease, has_disease
fetch the result iterate over it... pretty easy
 
$sql = "SELECT name FROM disease UNION ALL SELECT disease FROM patient WHERE username='$username'";
something like that?
 
4:33 PM
no, it will be an ugly query, because you have a comma seperated list
you should have a table HAS_DISEASE(patient_id, disease_id)... then it would be really easy
the query would become:
 
ok wait a min , i really want to save the diseases in another table but i couldnt find a way to store each disease into different row .
and that was the reason why i store all the diseases in one row
 
SELECT d.name, h.patient_id FROM disease d LEFT OUTER JOIN has_disease h ON h.disease_id = d.disease_id;
 
what is that??
 
if h.patient_id is NULL then the patient hasn't the disease, otherwhise he has it
 
wait a min
if a change my database and store in a different table the diseases of a patient it is going to be easier?
 
4:43 PM
a lot easier, everything, adding a disease to a patient, removing etc.
 
ok w8 a min to do it
 
just add and remove entried to has_disease
 
i will have a table like this one...
CREATE TABLE patientDiseases
(
id int AUTO_INCREMENT,
username varchar(50),
disease varchar(50),
PRIMARY KEY (username,id)
)ENGINE=MyISAM;
and there I will store all the disease for each user
 
yes, but the table doesn't really need an id, the disease and the person do
 
ok i will delete the id
 
4:45 PM
then the table patientDiseases would have 2 foreign keys: patient_id and person_id
i mean patient_id and disease_id
and the primary key (patient_id, disease_id)
 
CREATE TABLE patientDiseases
(
username varchar(50),
disease varchar(50),
FOREIGN KEY (username,disease)
)ENGINE=MyISAM;
something like that?
 
no something like
Create Table ...
(
username FOREIGN KEY REFERENCES patient.id
disease foreign key references disease.id
or whatever the syntax is
PRIMARY KEY(username, disease)
)
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY REFERENCES patient.id,
 
in mysql that would look different
username int not null,
disease int not null,
primary key (username, disease)
,
foreign key username references patient (id) on delete cascade,
foreign key disease references disease (id) on delete cascade
you don't really need the foreign keys, but what's the point in using a relational database system then, with on delete cascade if a user or a disease is deleted all entries that belong to them are automatically deleted in the patientDiseases table too
well ok, i think that's it for today... gotta go now, cu
 

last day (14 days later) »