Page 1 of 1

ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 3:11 am
by azhan
hey guys,

I try to create a code which detect un-matching input,

Code: Select all

<?php
// Connects to your Database 
mysql_connect("localhost", "root", "password") or die(mysql_error()); 
mysql_select_db("hospital") or die(mysql_error()); 
 if ($_REQUEST[iwant]){ 
$id = $_COOKIE['ID_my_site']; 
$result = mysql_query("SELECT title FROM pix WHERE pid ='$id'");
while($list = mysql_fetch_array($result)) {
if([color=#FF0040]$_REQUEST[iwant] != $list['title'] [/color]) {die ('<br><center><font color=red>NO SUCH IMAGES FOR THIS PARTICULAR PATIENT</font><br>Please Enter the correct title that had been assigned to this patient only</center>');}
}}
$_REQUEST[iwant] ==>> is the input that had been key in by user and i wanted this to match with my title stored in pix table...if not match it will die with message.

THE PROBLEM IS :
when it comes to this part if($_REQUEST[iwant] != $list['title'] ) , my all codes are working well if there is only ONE title inside my pix table. BUT when i stored more than one title inside my pix table...the IF code suddenly DIE with message. It seems it couldn't work with more than one title stored.....i thought when i used mysql_fetch_array i could fetch all my data.... :(

Did I Do Anything Wrong??

Help me Please

Thanks!


Azhan
_________________________
http://www.productcoverdesign.com-"Cheapest E-Cover Design"

Re: ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 3:19 am
by VladSun
$_REQUEST[iwant]
==>
$_REQUEST['iwant']

Re: ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 3:25 am
by azhan
VladSun wrote:$_REQUEST[iwant]
==>
$_REQUEST['iwant']
thanks for the reply vladsun, but unfortunately that didnt works also...there is nothing wrong with the code for one title fetching as i mentioned earlier. The problem is when it try to fetch more than one title.

any idea?

thanks

Azhan
_________________________
http://www.productcoverdesign.com-"Cheapest E-Cover Design"

Re: ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 3:36 am
by VladSun
It was the first one to notice.

You better rewrite your SQL query like this
[sql]"SELECT count(pid) FROM pix WHERE pid ='".$id."' and title=".$_REQUEST['iwant'][/sql]
No further PHP loops are needed.

And do sanitize user input.

Re: ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 5:31 am
by azhan
Im sorrie...but it still doesnt works....

Further explanation
In the page, it will display several title of image assigned to particular user once the user log in

like this : Welcome User 1
Below are the title of Images assign for u
- Xray
-MRI
Please enter the title into the search box for preview


Second of all, if the user enter the correct title..the page will show his/her image and if the user enter the wrong title...the page will return "No such images"

MY problem here is if I only stored ONE title under user...my code is function WELL, but when i stored more than one title, the code always return "No such Images"


How do i write Decent IF statement WHILE it fetch the title according to user id log in previously??

thanks for all of your guys effort,appreciate it!

Azhan
_________________________
http://www.productcoverdesign.com-"Cheapest E-Cover Design"

Re: ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 12:39 pm
by mabwi
How is the title stored if there's more than one?

If it's in separate rows, your code exits (with the die()) function after the first failure - it doesn't check further rows. Try something like:

Code: Select all

 
$found = FALSE;
while($list = mysql_fetch_array($result)) {
  if ($_REQUEST['iwant'] == $list['title']) { 
    $found = TRUE;
  }
}
 
if (!$found) {
  die ('<br><center><font color=red>NO SUCH IMAGES FOR THIS PARTICULAR PATIENT</font><br>Please Enter the correct title that had been assigned to this patient only</center>');
}
 

Re: ARRAY PROBLEMS : Please Help ME!

Posted: Fri Jul 18, 2008 1:22 pm
by califdon
mabwi asked before I could. :) If you are storing more than one title in the same field, that violates the First Normal Form rule of relational database design, that a column can contain only "atomic" data, that is, data that cannot be separated into several pieces. If you are not doing that, then you have to explain how you are storing multiple titles.