Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

<?php IF () { } ?>

Guest
Aug 18, 2008 Aug 18, 2008
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?
TOPICS
Server side applications
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 18, 2008 Aug 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 19, 2008 Aug 19, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 20, 2008 Aug 20, 2008
AdonaiEchad wrote:
> 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';

If I'm not mistaken, you have a copy of my "Essential Guide to DW CS3".
This situation is identical to the one on page 502.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 20, 2008 Aug 20, 2008
Yes, I have followed your steps on page 502.
$LoginRS__query = sprintf("SELECT username FROM `user` WHERE username=%s AND userID !=" .$_POST['userID'], GetSQLValueString($loginUsername, "text"));

It works perfectly when it comes to the password field being empty but when it comes to the student number I cannot get the code to accept the student number because it is still the same user that has the student number assigned to him.

This is what I have currently as the if statement.
// if student number is empty, use existing password
if(empty($_POST['stNum'])){
$_POST['stNum'] = "";
} else {
$_POST['stNum'] = trim($_POST['stNum']);
if (strlen($_POST['stNum']) < 9) {
$error['studentNumber'] = 'The student number is less than 9 numbers';
}
if (strlen($_POST['stNum']) > 9) {
$error['studentNumber'] = 'The student number is greater than 9 numbers';
}
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';
}
}

The reason I have this code at the beginning as such...is because not all uses are students and need a student number.
if(empty($_POST['stNum'])){
$_POST['stNum'] = "";
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 20, 2008 Aug 20, 2008
AdonaiEchad wrote:
> Yes, I have followed your steps on page 502.
> $LoginRS__query = sprintf("SELECT username FROM `user` WHERE username=%s AND
> userID !=" .$_POST['userID'], GetSQLValueString($loginUsername, "text"));
>
> It works perfectly when it comes to the password field being empty but when it
> comes to the student number I cannot get the code to accept the student number
> because it is still the same user that has the student number assigned to him.

Page 502 has nothing to do with the password. You need to create a SQL
query that selects the username where the username is the student's name
and the student number is NOT the number submitted.

If John Doe already has the student number 123456789 and no one else
have the same number, this query should produce zero results:

SELECT studentName
WHERE studentName ='John Doe'
AND studentNumber != 123456789

If it produces a result, it means that John Doe has changed his student
number and chosen one that already belongs to someone else.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 21, 2008 Aug 21, 2008
I believe I understand what you are saying but I am not 100% sure if I am executing this correctly.

Right now my if statement stands as follows...
if(empty($_POST['stNum'])){
$_POST['stNum'] = "";
} else {
$_POST['stNum'] = trim($_POST['stNum']);
if (strlen($_POST['stNum']) < 9) {
$error['studentNumber'] = 'The student number is less than 9 numbers';
}
if (strlen($_POST['stNum']) > 9) {
$error['studentNumber'] = 'The student number is greater than 9 numbers';
}
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';
}
}

I have two record sets one getUser and the other studentNo.

The getUser is as follows...
SELECT *
FROM `user`
WHERE userID = colname

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_GET['userID']
-----------------------

The studentNo record set that you want me to change from this...
SELECT `user`.stNum
FROM `user`
WHERE stNum = colname

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_GET['stNum']
-----------

To...
SELECT first_name
FROM user
WHERE first_name = firstName
AND stNum != colname

Name: firstName
Type: Text
Default: -1
Run-time Value: $_GET['first_name']

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_GET['stNum']
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 21, 2008 Aug 21, 2008
AdonaiEchad wrote:
> The studentNo record set that you want me to change from this...
> SELECT `user`.stNum
> FROM `user`
> WHERE stNum = colname
>
> Name: colname
> Type: Numeric
> Default: -1
> Run-time Value: $_GET['stNum']
> -----------
>
> To...
> SELECT first_name
> FROM user
> WHERE first_name = firstName
> AND stNum != colname
>
> Name: firstName
> Type: Text
> Default: -1
> Run-time Value: $_GET['first_name']
>
> Name: colname
> Type: Numeric
> Default: -1
> Run-time Value: $_GET['stNum']

Yes, although I'm not sure whether you're sending the details via GET or
POST. If you're using POST, the Runtime value should use $_POST, not $_GET.

The other thing that you haven't yet grasped is that you need to check
how many results are returned. Comparing the numbers is no use.

At the end of the recordset code you should see this:

$totalRows_studentNo = mysql_num_rows($studentNo);

You need to use the value in $totalRows_studentNo to check whether there
recordset produces any results.

if ($totalRows_studentNo) {
// the number is taken by someone else
} else {
// it's OK
}

Your conditional statement needs to come after the recordset code.
Otherwise, $totalRows_studentNo won't exist.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 22, 2008 Aug 22, 2008
You are right, I did not grasp it.

I have changed my studentNo recordset of $_GET to $_POST...from...
SELECT first_name
FROM user
WHERE first_name = firstName
AND stNum != colname

Name: firstName
Type: Text
Default: -1
Run-time Value: $_GET['first_name']

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_GET['stNum']
--------------
To...

SELECT first_name
FROM user
WHERE first_name = firstName
AND stNum != colname

Name: firstName
Type: Text
Default: -1
Run-time Value: $_POST['first_name']

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_POST['stNum']
--------
Now what I am getting confused is for the code you gave me...
if ($totalRows_studentNo) {
// the number is taken by someone else
} else {
// it's OK
}

I know I need to place the conditional statement after $totalRows_studentNo. The code that I had done previously, do I insert it into your conditional statement or do I need to redo the logic? I guess it would be a nested if statement within a if statement.

if(empty($_POST['stNum'])){
$_POST['stNum'] = "";
} else {
$_POST['stNum'] = trim($_POST['stNum']);
if (strlen($_POST['stNum']) < 9) {
$error['studentNumber'] = 'The student number is less than 9 numbers';
}
if (strlen($_POST['stNum']) > 9) {
$error['studentNumber'] = 'The student number is greater than 9 numbers';
}
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';
}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 22, 2008 Aug 22, 2008
AdonaiEchad wrote:
> You are right, I did not grasp it.
>
> I have changed my studentNo recordset of $_GET to $_POST...from...
> SELECT first_name
> FROM user
> WHERE first_name = firstName
> AND stNum != colname
>
> Name: firstName
> Type: Text
> Default: -1
> Run-time Value: $_GET['first_name']
>
> Name: colname
> Type: Numeric
> Default: -1
> Run-time Value: $_GET['stNum']
> --------------
> To...
>
> SELECT first_name
> FROM user
> WHERE first_name = firstName
> AND stNum != colname
>
> Name: firstName
> Type: Text
> Default: -1
> Run-time Value: $_POST['first_name']
>
> Name: colname
> Type: Numeric
> Default: -1
> Run-time Value: $_POST['stNum']
> --------
> Now what I am getting confused is for the code you gave me...
> if ($totalRows_studentNo) {
> // the number is taken by someone else
> } else {
> // it's OK
> }
> I know I need to place the conditional statement after $totalRows_studentNo
> but do I insert it into this code...
> if(empty($_POST['stNum'])){
> $_POST['stNum'] = "";
> } else {
> $_POST['stNum'] = trim($_POST['stNum']);
> if (strlen($_POST['stNum']) < 9) {
> $error['studentNumber'] = 'The student number is less than 9 numbers';
> }
> if (strlen($_POST['stNum']) > 9) {
> $error['studentNumber'] = 'The student number is greater than 9 numbers';
> }
> 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';
> }
> }
>
> into the conditional statement or do I need to get rid of it completely and
> start over?
>


--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 22, 2008 Aug 22, 2008
AdonaiEchad wrote:
> Now what I am getting confused is for the code you gave me...

The reason you're getting confused is because you're looking at code.
Forget code for a moment. Sit down and write out a list of what you want
the code to do.

Is the number set?
Is it nine characters?
If it meets those two criteria, run the query to check if it belongs to
anyone else.

> if (strlen($_POST['stNum']) < 9) {
> $error['studentNumber'] = 'The student number is less than 9 numbers';
> }
> if (strlen($_POST['stNum']) > 9) {
> $error['studentNumber'] = 'The student number is greater than 9 numbers';
> }

You want the number to be exactly nine, so there's no need to do both of
these:

if (strlen($_POST['stNum']) != 9) {
$error['studentNumber'] = 'The student number must be 9 numbers';
}

Presumably, you don't want letters mixed in with the numbers, so you
should improve it like this:

if (strlen($_POST['stNum']) != 9 && !is_numeric($_POST['stNum'])) {
$error['studentNumber'] = 'The student number must be 9 numbers';
}

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

This last conditional statement has no meaning. This is what you need to
do.

if (!$error) {
// run the query to find out if anyone else has the number
if ($totalRows_studentNo) {
$error['studentNumber'] = 'This number has already been assigned';
}
}

Quite frankly, you seem to be making unnecessary problems for yourself.
Why not generate the student numbers automatically? With a nine-digit
number, the likelihood of someone picking the same number as another
person is small, but you're turning it into a guessing game.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 26, 2008 Aug 26, 2008
I have sat down and wrote what I want to do on the update page. The record set are pretty much the same as I have stated earlier but I'll place it again. I think you might understand more where I am coming from of my logic and if it can be done.

The getUser is as follows...
SELECT *
FROM `user`
WHERE userID = colname

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_GET['userID']
-----------------------

SELECT *
FROM user
WHERE first_name = firstName
AND stNum != colname

Name: firstName
Type: Text
Default: -1
Run-time Value: $_POST['first_name']

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_POST['stNum']
--------------------

Admin clicks on user list to update website users...
=====================================
1. If the student Number form field is blank leave it blank and proceed updating the form.
2. If the student Number form field is not blank you may enter a student number.
2a. If number has been entered trim any white spaces.
2b. If number has been entered and been trimmed check to see if there are nine numbers entered in the form field, if not give error to enter appropriate numbers in the form field.
2c. Check to see if the student Number in the form field have been assigned by other users and to ignore the student Number that is currently being updated if assigned already.
2d. If everything is clear then update student Number form field with rest of form fields

This is what I have written and I have been running into problems making the php code work this logic. Even using the code you gave me above this post.

This is the functionality I have, is this doable?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 26, 2008 Aug 26, 2008
AdonaiEchad wrote:
> This is the functionality I have, is this doable?

Yes, it sounds doable. What I don't understand is why you're using $_GET
for userID, but $_POST for the others. $_GET should be used to load the
details in the update form. Then save userID as a hidden field, and
submit it again, this time in the $_POST array.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 27, 2008 Aug 27, 2008
Yes David you are right I was supposed to change it to $_GET. I have made the changes.

David, I was going back to your book and forgive me for digressing because the update user page is still not working for me.
I went over your chapter 16 on page 548 where you spoke of preventing duplicate entries for insert new author. The way you structured the code for $totalRows_checkAuthor (which I now understand what you meant about that), can the same method be applied to my student number even though I am using the same structure as your user update in your book. The reason I am saying this is because on page 549 you set at $alreadyRegistered = false; and then you presented the if($totalRows_checkAuthor > 0), can this be applied the same way for the studentNo?

I noticed on page 550 when you go into the else { //go ahead with server behaviour, that the if statement is differently structured as the one being used for the update user list on page 484 point 7 of if(!error) {}.

I guess what I am saying is can the if statement on page 548 be merged into the php code on page 484. I hope this make sense. Or am I confusing myself here and doing to much thinking?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2008 Aug 27, 2008
AdonaiEchad wrote:
> I guess what I am saying is can the if statement on page 548 be merged into
> the php code on page 484. I hope this make sense.

Page 484 checks for several possible errors. Page 548 checks only for a
duplicate entry. That's why they're different.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 27, 2008 Aug 27, 2008
Understood.
So if I still use everything the same as far as recordset as follows...
SELECT *
FROM user
WHERE first_name = firstName
AND stNum != colname

Name: firstName
Type: Text
Default: -1
Run-time Value: $_GET['first_name']

Name: colname
Type: Numeric
Default: -1
Run-time Value: $_GET['stNum']
-------------------
I am using the same method that was used in your book page 497-505 by placing the update record server behaviour code in the
if(!error) {
}

and underneath my $MM_flag = "MM_update";
I have the...
if (isset($_POST[$MM_flag])) {
}

Between this I place my logic code of...
if (isset($_POST[$MM_flag])) {
if (strlen($_POST['stNum']) != 9 && !is_numeric($_POST['stNum'])) {
$error['studentNumber'] = 'The student number must be 9 numbers';
}
// run the query to find out if anyone else has the number
if ($totalRows_studentNo) {
$error['studentNum'] = 'This number has already been assigned';
}
}
}

Am I getting this correctly?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2008 Aug 27, 2008
AdonaiEchad wrote:
> Am I getting this correctly?

Sorry, I don't have the faintest idea what you're actually doing.

The following code does what I think you're trying to achieve (for the
sake of brevity, I have left out the connection and GetSQLValueString()
definition):

$colname_getStudentNum = "-1";
if (isset($_POST['stNum'])) {
$colname_getStudentNum = $_POST['stNum'];
}
$var2_getStudentNum = "-1";
if (isset($_POST['firstname'])) {
$var2_getStudentNum = $_POST['firstname'];
}
mysql_select_db($database_testConn, $testConn);
$query_getStudentNum = sprintf("SELECT students.stNum FROM students
WHERE stNum = %s AND students.firstname != %s",
GetSQLValueString($colname_getStudentNum,
"int"),GetSQLValueString($var2_getStudentNum, "text"));
$getStudentNum = mysql_query($query_getStudentNum, $testConn) or
die(mysql_error());
$row_getStudentNum = mysql_fetch_assoc($getStudentNum);
$totalRows_getStudentNum = mysql_num_rows($getStudentNum);

$colname_getStudent = "-1";
if (isset($_GET['stud_id'])) {
$colname_getStudent = $_GET['stud_id'];
}
mysql_select_db($database_testConn, $testConn);
$query_getStudent = sprintf("SELECT * FROM students WHERE stud_id = %s",
GetSQLValueString($colname_getStudent, "int"));
$getStudent = mysql_query($query_getStudent, $testConn) or
die(mysql_error());
$row_getStudent = mysql_fetch_assoc($getStudent);
$totalRows_getStudent = mysql_num_rows($getStudent);

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
if (strlen($_POST['stNum']) != 9 && !is_numeric($_POST['stNum'])) {
$error['studentNumber'] = 'The student number must be 9 numbers';
}
if ($totalRows_getStudentNum > 0) {
$error['duplicate'] = 'That number is already taken';
}
if (!$error) {
$updateSQL = sprintf("UPDATE students SET firstname=%s, stNum=%s
WHERE stud_id=%s",
GetSQLValueString($_POST['firstname'], "text"),
GetSQLValueString($_POST['stNum'], "int"),
GetSQLValueString($_POST['stud_id'], "int"));

mysql_select_db($database_testConn, $testConn);
$Result1 = mysql_query($updateSQL, $testConn) or die(mysql_error());

$updateGoTo = "list_studs.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
}

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 28, 2008 Aug 28, 2008
I have changed accordingly for my update page. However, if I update a student and he has already been assigned a student number when I click my update button it tells me that it has already been taken, which happens to be his own student number. If there was no student number assigned in the student list and I update the student and type in a student number, it will update even if someone else has the student number but when I go back into the page and update it tells me that the student number has been assigned. Even if I had a distinct student number when I click update it tells me that the student number has been assigned.

This is what I have as follows...
$colname_getUser = "-1";
if (isset($_GET['userID'])) {
$colname_getUser = $_GET['userID'];
}
mysql_select_db($database_conYeshivah, $conYeshivah);
$query_getUser = sprintf("SELECT * FROM `user` WHERE userID = %s", GetSQLValueString($colname_getUser, "int"));
$getUser = mysql_query($query_getUser, $conYeshivah) or die(mysql_error());
$row_getUser = mysql_fetch_assoc($getUser);
$totalRows_getUser = mysql_num_rows($getUser);

$colname_getStudentNum = "-1";
if (isset($_POST['stNum'])) {
$colname_getStudentNum = $_POST['stNum'];
}
$var2_getStudentNum = "-1";
if (isset($_POST['first_name'])) {
$var2_getStudentNum = $_POST['first_name'];
}
mysql_select_db($database_conYeshivah, $conYeshivah);
$query_getStudentNum = sprintf("SELECT stNum, first_name FROM `user` WHERE stNum = %s AND first_name = %s", GetSQLValueString($colname_getStudentNum, "int"),GetSQLValueString($var2_getStudentNum, "text"));
$getStudentNum = mysql_query($query_getStudentNum, $conYeshivah) or die(mysql_error());
$row_getStudentNum = mysql_fetch_assoc($getStudentNum);
$totalRows_getStudentNum = mysql_num_rows($getStudentNum);

------------------------

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

if (strlen($_POST['stNum']) != 9 && !is_numeric($_POST['stNum'])) {
$error['studentNumber'] = 'The student number must be 9 numbers';
}
if ($totalRows_getStudentNum > 0) {
$error['duplicate'] = 'That number is already taken';
}
if(!$error){
$updateSQL = sprintf("UPDATE `user` SET dateOfUpdate=%s, stNum=%s, first_name=%s, family_name=%s, username=%s, pwd=%s, email=%s, address=%s, city=%s, prov=%s, postal=%s, phone=%s, cell=%s WHERE userID=%s",
GetSQLValueString($_POST['dateOfUpdate'], "date"),
GetSQLValueString($_POST['stNum'], "int"),
GetSQLValueString($_POST['first_name'], "text"),
GetSQLValueString($_POST['family_name'], "text"),
GetSQLValueString($_POST['username'], "text"),
GetSQLValueString($_POST['pwd'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['address'], "text"),
GetSQLValueString($_POST['city'], "text"),
GetSQLValueString($_POST['prov'], "text"),
GetSQLValueString($_POST['postal'], "text"),
GetSQLValueString($_POST['phone'], "int"),
GetSQLValueString($_POST['cell'], "int"),
GetSQLValueString($_POST['userID'], "int"));

mysql_select_db($database_conYeshivah, $conYeshivah);
$Result1 = mysql_query($updateSQL, $conYeshivah) or die(mysql_error());

$updateGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 28, 2008 Aug 28, 2008
AdonaiEchad wrote:
> I have changed accordingly for my update page. However, if I update a student
> and he has already been assigned a student number when I click my update button
> it tells me that it has already been taken, which happens to be his own student
> number.
The problem lies here:

> $query_getStudentNum = sprintf("SELECT stNum, first_name FROM `user` WHERE
> stNum = %s AND first_name = %s", GetSQLValueString($colname_getStudentNum,
> "int"),GetSQLValueString($var2_getStudentNum, "text"));

It should be this:

$query_getStudentNum = sprintf("SELECT stNum, first_name FROM `user`
WHERE
stNum = %s AND first_name != %s", GetSQLValueString($colname_getStudentNum,
"int"),GetSQLValueString($var2_getStudentNum, "text"));

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 28, 2008 Aug 28, 2008
Everything works perfectly thus far. I see it was the "!=" that threw me off. There is only one little thing that is a draw back and that is if the student number is already blank and I update a user it gives me the error where the student number must have nine numbers. It would be perfect if everyone had a student number but some users are not students themselves. As always David you have been most helpful.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 28, 2008 Aug 28, 2008
AdonaiEchad wrote:
> There is only one little thing that is a draw back and that is if the student
> number is already blank and I update a user it gives me the error where the
> student number must have nine numbers.

if (!empty($_POST['stNum']) && strlen($_POST['stNum']) != 9 &&
!is_numeric($_POST['stNum'])) {
$error['studentNumber'] = 'The student number must be 9 numbers';
}

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 18, 2008 Aug 18, 2008
That was it, it works perfect now. I would not have thought it was a SQL statement.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 28, 2008 Aug 28, 2008
LATEST
Now it work. Thank you David, much appreciated.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines