Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.

PHP Code Help

Let me preface this by saying this is not php-fusion related. I am trying to put together a webform and the data collected is inserted into a mysql database. Everything seems to be working except that I need to validate one of the entries to ensure that the data entered is not already in the table column. I have tried many pieces of code, none of which seem to work. I have read about making the column UNIQUE but I have also read that it is better to use a SELECT statement to do this which is how I have tried to accomplish this. The column in question is the WSBNo. As I mentioned above, the form is working but will/does not validate that column. This is the code I am using in the form processor form:

CodeDownload  
$query = "SELECT COUNT(*) FROM fusion_players17 WHERE WSBNo = (' . mysql_real_escape_string(WSBNo) .')";
      //$query = "SELECT FROM fusion_players17 WHERE WSBNo = '$WSBNo'";
      $result = mysql_query($query);
      if (mysql_result($result, 0, 0) > 0){
      //if (mysql_result($result) >0){
    // some data matched
   echo "<center>";
   echo "That Square Has Been Taken, Go Back And Pick Another Square";
   echo "</center>";
}
else {
       
        $sql = "INSERT INTO fusion_players17 (user_name, name, address, city, state, zip, email, password, Reg, SurvI, SurvII, BTL, Play, WSB, CPP, Marg, total, WSBNo)
                VALUES ('$user_name', '$name', '$address', '$city', '$state', '$zip', '$email', '$password', '$Reg', '$SurvI', '$SurvII', '$BTL', '$Play', '$WSB', '$CPP', '$Marg', '$total', '$WSBNo')";
        if (!mysql_query($sql,$connection)){
            die('Error: ' . mysql_error());



If more information is needed, let me know and thanks in advance for any assistance provided.
From what I understand , you do the check here,
CodeDownload  
$query = "SELECT COUNT(*) FROM fusion_players17 WHERE WSBNo = (' . mysql_real_escape_string(WSBNo) .')";
      //$query = "SELECT FROM fusion_players17 WHERE WSBNo = '$WSBNo'";
      $result = mysql_query($query);



What we do not know at this point is the value of $WSBNo.

You can do a print_r($_POST); above the line and see the value to compare against.
and then define $WSBNo before the SQL query with something like ( depending on value you want to compare ),

CodeDownload  
$WSBNo = $_POST['WSBNo'];

Thanks for the quick response. The value of WSBNo is numeric and can be any number from 00 to 99. It is the number of a square and only 25 players can select square numbers. What I am trying to prevent is someone entering number 21 (or any other number) if number 21 (or any other number) has already been selected. The form is located here. http://www.fredswebportal.com/regform...2_rev1.php. It is a test site in case you want to enter some data. I already have code that will shut down that pool once the 1st 25 players have signed up for that pool.
Alright, aslong as the $WSBNo is defined above, I think this should do the trick.

CodeDownload  

$query = "SELECT COUNT(*) FROM fusion_players17 WHERE WSBNo = (' . mysql_real_escape_string($WSBNo) .')";
$result = mysql_result($query, 0, 0);
   if ((!$result) && ($WSBNo <= 99)) {

      $sql = "INSERT INTO fusion_players17 (user_name, name, address, city, state, zip, email, password, Reg, SurvI, SurvII, BTL, Play, WSB, CPP, Marg, total, WSBNo)
      VALUES ('$user_name', '$name', '$address', '$city', '$state', '$zip', '$email', '$password', '$Reg', '$SurvI', '$SurvII', '$BTL', '$Play', '$WSB', '$CPP', '$Marg', '$total', '$WSBNo')";
      
      if (!mysql_query($sql,$connection)){
         die('Error: ' . mysql_error());
      } // This is missing from example

   } else {
   
      echo "<center>";
      echo "That Square Has Been Taken, Go Back And Pick Another Square";
      echo "</center>";
   
}

If I read this line correctly ( if (($result > 0) && ($WSBNo <= 21)) { ) it is saying that if WSBNo is greater than 0 and less than or equal to 21 then don't accept it. I must have misstated my comment. The value of WSBNo can be any number from 00 to 99. I used the number 21 as an example. Assuming that I am correct in my interpretation, if I change that line to if (($result > 0) && ($WSBNo <= 99)) { it should work assuming I define WSBNo above as suggested?
Check again, I updated the post.
Well, that seems to work, except that the message that the square has been taken appears in the confirmation page as noted in this screen shot.

http://www.fredswebportal.com/regform...mation.jpg

How do I display this message before it goes to the confirmation page because I am afraid that the player will not see it and will assume that his square has been accepted. I really, really appreciate your help on this.
np mate, working late today anyway :)
See the code now, ( updated post )
The only change I noted in the code was the following line:

original line:
CodeDownload  
if (($result) && ($WSBNo <= 99)) {


updated code:
CodeDownload  
if ((!$result) && ($WSBNo <= 99)) {



What that did is remove the display at the top of the confirmation page as shown in the screen shot. However, it allowed the square number 36 to be inserted into the database even though square number 36 already exists. If I missed the change in the code, let me know.

What I am trying to do is display a message that says that square is already taken and give the player an opportunity to go back and change it before inserting all of the registration information into the database.

Since I am in California, I can see that you are definitely working late.
oh ! just shuffle it around, I inserted a bracket where one is missing here, comment added so you see it.
Sorry I had a looong rough day, get slow on the later hours :)
I must be missing something. I copied your code as is and the square I selected along with other data was inserted into the database even though the square number was already in the database. I then changed this line from
CodeDownload  
if ((!$result) && ($WSBNo <= 99)) {



to
CodeDownload  
if (($result) && ($WSBNo <= 99)) {



and then the square number was not inserted into the database nor was any other data inserted. However, the message that the square is already taken is displayed at the top of the confirmation page which as I stated earlier will cause the player to think that his square was selected and accepted. Can that message be displayed prior to the confirmation page being displayed and if so how do I do it?
Perhaps some result issue there reading raw mysql output, it is a standard if else statement. So if the $result dont find a value !$result & the value is less or equal to 99 do the injection, else display message.

You can run lab with this.
!$result means that it should be negative.
if ($result) means true

CodeDownload  
$result = 1;
$WSBNo = 4;

   if ((!$result) && ($WSBNo <= 99)) {

      echo "I accept, input is correct and not taken. Data saved";

   } else {
   
      echo "<center>";
      echo "That Square Has Been Taken, Go Back And Pick Another Square";
      echo "</center>";
   
}

The code with the line
CodeDownload  
if (($result) && ($WSBNo <= 99)) {



does work and displays the message that says the square is already taken. The problem is that the message is displayed at the top of a confirmation page (as shown in a previous screenshot). I would like that message to be displayed before the confirmation page is displayed.

While I freely admin that I am a novice in writing code, I believe that the code as written should display the message prior to the confirmation page being displayed. I am attaching the entire form processing script in the hope that you can see where that may happen.
afoster attached the following file:
form_process.zip [1.87kB / 118 Downloads]
Ok, read notes and see.
Falk attached the following file:
form_process_1.zip [2.09kB / 126 Downloads]
Well, I am totally confused at this point. If I use the code
CodeDownload  
if ((!$result) && ($WSBNo <= 99)) {


it will let me enter a square that has already been taken or a square that hasn't been taken with no error message.

If I use the code
CodeDownload  
if (($result) && ($WSBNo <= 99)) {



it won't let me enter a square that has been taken nor will it let me enter a square that hasn't been taken, but will display the error message. I have no idea what to do now.

I am attaching all of the files that I am using for this form in the hope that you can see where the problem is and again, thanks for your help as I know how busy you are.
I still have the sql and what not, but anyway , let´s resolve it :)

here is one idea,

CodeDownload  
$results = mysql_query($query, $connection);

   if ((!$results) && ($WSBNo <= 99)) {

First of all, thanks for moving the files to security...I woke extra early this morning because I suddenly realized that I had exposed the database by sending the files. As for your idea, I have already tried that and the result is that I can add a square number that has been already picked as well as it adds a number that has not been picked and there is no error message displayed. See the post just above your latest post.
Np mate, You should change password anyway :)
A yes, but the key change is here,
CodeDownload  
$results = mysql_query($query, $connection);



Instead of a mysql_query($query,0,0); I attach it to connection directly.

I am like 100% sure that it is here it fails, let me try print the logic in text rather than code.
if
!$result = we did not find a match in the SQL for the submitted number ( it can vary how it is checked asdispayed above ) depends on PHP version, etc.
$WSBNo <= 99 = must not be over 99 but can be equal to 99.
Then
Run SQL
else
display error
I had changed my password as soon as I got up this morning...

I made the change you suggested and using
CodeDownload  
if ((!$result) && ($WSBNo <= 99)) {



allowed me to enter a number that was already picked and it did not generate the error message.

When I changed it to
CodeDownload  
if (($result) && ($WSBNo <= 99)) {



It would not let me enter a number that has been picked, it generated the error message. Unfortunately it would not let me enter a number that has not been picked and it would generate the same error message.

BTW, my webhost is running PHP Version 5.5.22.
Maybe you can send me FTP details a temp account and I will look there for you?
Thread Information
Author
Replies
28 posts
Views
4,243 times
Last Post
Last updated on 3 years ago
You can view all discussion threads in this forum.
You cannot set up a bounty in this discussion thread.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
You cannot up or down-vote on the post in this discussion thread.
Users who participated in discussion: Falk, afoster