ARRAY PROBLEMS : Please Help ME!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

ARRAY PROBLEMS : Please Help ME!

Post 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"
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: ARRAY PROBLEMS : Please Help ME!

Post by VladSun »

$_REQUEST[iwant]
==>
$_REQUEST['iwant']
There are 10 types of people in this world, those who understand binary and those who don't
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

Re: ARRAY PROBLEMS : Please Help ME!

Post 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"
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: ARRAY PROBLEMS : Please Help ME!

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

Re: ARRAY PROBLEMS : Please Help ME!

Post 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"
mabwi
Forum Commoner
Posts: 27
Joined: Wed Aug 01, 2007 4:51 pm

Re: ARRAY PROBLEMS : Please Help ME!

Post 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>');
}
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: ARRAY PROBLEMS : Please Help ME!

Post 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.
Post Reply