Skip to main content
August 18, 2008
Question

<?php IF () { } ?>

  • August 18, 2008
  • 3 replies
  • 1249 views
I am having a problem with my code and I seem not to be getting the result I want. I tried different tests to see if it is my code or my database. I am trying to make a if statement where if administration creates a student that if someone else has the same student number it will give the administrator an error.

I have made a recordset called "studentNo" which contains a student name, address and their student number which is a "int" of 9 characters. I am using mySQL.

As an example of what the database looks like as such
Student No. User
1 200777895 Darryl
2 333333333 Michael
3 987654321 Fred
4 123456789 Carolina

Now this is my code of an if statement.
if ($_POST['stNum'] == $row_studentNo['stNum']){
$error['studentNumber'] = 'This student number has already been assigned, please enter a new student number';
}

When my if code looks on the recordset called "studentNo" it finds no matching student numbers so it thinks there are no duplicates when I've entered a series of 9 numbers in a form field. If I type 200777895 student number the error message comes up and tells me that someone has that student number. If I type in the form field a different number 333333333 it shows there is no error and that student number is available. I noticed that the 200777895 student number is my first user on my list in the database and somehow the code does not search the whole database for other student numbers.

I have tested to see if it is my code and when I change the code to search the actual student number instead of the recordset studentNo, I get my error message saying that student number has been taken.

Here is the test if code I used.
if ($_POST['stNum'] == 123456789){
$error['studentNumber'] = 'This student number has already been assigned, please enter a new student number';
}

I do not know what I am doing wrong in this code below where it is not searching my whole recordset of studentNo.
if ($_POST['stNum'] == $row_studentNo['stNum']){
$error['studentNumber'] = 'This student number has already been assigned, please enter a new student number';
}


I can't figure out what am I doing wrong, anyone have any ideas?
This topic has been closed for replies.

3 replies

August 28, 2008
Now it work. Thank you David, much appreciated.
August 18, 2008
That was it, it works perfect now. I would not have thought it was a SQL statement.
Inspiring
August 18, 2008
AdonaiEchad wrote:
> I can't figure out what am I doing wrong, anyone have any ideas?

The problem almost certainly lies with your SQL query. The query should
be using $_POST['stNum'] as a variable to query the database.

SELECT name FROM students
WHERE stNum = var1

Set $_POST['stNum'] as var1 and set its type to Number.

If the number of rows returned by the recordset is greater than 0, the
number has already been assigned.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
August 20, 2008
David thank you for your help. I now ran into another situation because the code works perfectly if I am inserting a new record. When I want to update the user it now tells me that the existing number is giving me the error message of
$error['studentNumber'] = 'This student number has already been assigned, please enter a new student number';

I was trying to work the code as such...
if ($_POST['stNum'] == $row_getUser['stNum']){
$_POST['stNum'] = $row_getUser['stNum'];
}


if ($_POST['stNum'] == $row_studentNo['stNum']){
$error['studentNumber'] = 'This student number has already been assigned, please enter a new student number';
}

My logic for using the $row_getUser['stNum'] instead of $row_studentNo['stNum'] is because the $row_getUser['stNum'] holds my userID for my update user link, which I thought the if statement code can see that $_POST['stNum'] is the same as $row_getUser['stNum'] so that the if statement code will allow me to update without thinking the student number has already been assigned to someone else.

I am out of options on what the if statement should be.