Help with PHP/MySQL

Discussion in 'Web Design and Development' started by dkotter, Jan 17, 2011.

  1. dkotter, Jan 17, 2011
    Last edited by a moderator: Jan 17, 2011

    dkotter macrumors member

    Joined:
    Jul 30, 2009
    #1
    I am a beginner when it comes to PHP and I'm having some issues getting a data entry form to work right. I'm building a site for a local high school wrestling team and they want to hold an online raffle. I have that up and running fine, but they want an online form they can add someone, with the # of tickets they purchased, so they can sell tickets offline as well.

    I have the form set up and it adds the new person correctly to the database, but I can't get it to add the tickets at all. It needs to be able to add anywhere from 1 ticket and up, and have each ticket be it's own record in the database and be associated with the correct person. I have it set up to go through it with a while loop, but it apparently isn't working and I can't figure it out.

    Any help would be greatly appreciated. Below is the code, everything seems to be working up until close to the end.

    PHP:
    <?php
      
    // create short variable names
      
    $fname=$_POST['fname'];
      
    $lname=$_POST['lname'];
      
    $phone=$_POST['phone'];
      
    $email=$_POST['email'];
      
    $amount=$_POST['amount'];

      if (!
    $fname || !$lname || !$phone || !$email || !$amount) {
         echo 
    "You have not entered all the required details.<br />"
              
    ."Please go back and try again.";
         exit;
      }

      if (!
    get_magic_quotes_gpc()) {
        
    $fname addslashes($fname);
        
    $lname addslashes($lname);
        
    $phone addslashes($phone);
        
    $email doubleval($email);
      }

      @ 
    $db = new mysqli('localhost''root''root''raffle');

      if (
    mysqli_connect_errno()) {
         echo 
    "Error: Could not connect to database.  Please try again later.";
         exit;
      }

      
    $query "insert into raf_members (email,password,first,last,phone,active,ipaddress,since,expires) values
                ('"
    .$email."', 'password', '".$fname."', '".$lname."', '".$phone."', '1', 'none', '".THEDATE." at ".THETIME."', 'none')";
      
    $result_member $db->query($query);
      
      
    $query "select * from raf_members where email='".$email."'";
      
    $result $db->query($query);
        
    $i $result->fetch_array($result);
      
        
    $ab 0
        while (
    $ab $amount) {
        
    $ticket_id '';
        
    $ticketid md5(rand(1000000,9999999)); 
        
    $abc 0; while ($abc 20) { $ticket_id .= $ticketid[$abc]; $abc++; }
        
    $query "select * from raf_tickets where ticket_id='".$ticket_id."'";
        
    $result $db->query($query);
        
    $dup $result->num_rows; if (!$dup) { $test_time time() - 172800GetTime();
          
    $query "insert into raf_tickets (ticket_id,owner_mnum,thedate,thetime,active,`time_stamp`) values ('".$ticket_id."','".$i[mnum]."','".THEDATE."','".THETIME."','1','".time()."')";
          
    $result $db-query($query);
          if (
    $result) { $ab++; }
          }
       }

      if (
    $result_member) {
          echo  
    $db->affected_rows." member inserted into database.";
      } else {
            echo 
    "An error has occurred.  The member was not added.";
      }

      
    $db->close();
    ?>
     
  2. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #2
    On first glance, on line 45:
    $i[mnum]
    should be:
    $i['mnum'].
     
  3. harpster macrumors regular

    Joined:
    Jan 26, 2010
    #3
    When you have those kind of problems just use echo statements to test your variables to make sure they contain the data you expect before inserting or updating the database.

    example: echo $i['mnum'];exit;

    If the data is correct than the database query must be wrong so add the or die mysql error statement to your query which might help point out the error.

    mysql_connect("localhost", "admin", "1admin") or die(mysql_error());

    Also check to see of on line raffles are legal in your state because they are NOT legal in many states and considered a form of gambling. I had a client that wanted to do this and after checking we found out that we could get in big trouble for running a raffle even for a non-profit so we dropped the idea.
     
  4. dkotter, Jan 18, 2011
    Last edited: Jan 18, 2011

    dkotter thread starter macrumors member

    Joined:
    Jul 30, 2009
    #4
    Okay I have fixed that and no change.

    I tried the echo statement and nothing shows up, so I'm not sure what I'm doing wrong. I'm assuming it is something wrong with my query but I added an echo statement and die statement, and nothing changes. No error messages or text.

    So I'm really not sure what I've done wrong, as I've put echo statements other places as well and when I run it, just an empty page shows up, besides the usual navigation and images and such.

    PHP:
    $query "select * from raf_members where email='".$email."'";
      
    $result $db->query($query) or die(mysql_error());
        
    $i $result->fetch_array($result);
        echo 
    $i['mnum'];
        exit;
     
  5. neopickaze macrumors newbie

    Joined:
    Jan 18, 2011
    #5
    You have an @ symbol suppressing errors from line 22

    Try remove that first see if you have any error output and work from there.
     
  6. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #6
    Also try echoing your sql statements to see if they're getting the values they need. You can also run the sql statements manually in something like phpmyadmin to see what errors are keeping things from working.

    Without knowing your table structures that's about the only thing I can tell you.
     
  7. dkotter thread starter macrumors member

    Joined:
    Jul 30, 2009
    #7
    Okay I'm still not getting any errors but I've echoed my queries and everything looks good besides one thing. On the query below, I've echoed it and it looks good. But when I pull the result from that query and then echo it, nothing shows up. Also, when I echo the next query (not shown below) that includes the result, it doesn't show up either. So I know something is going wrong with trying to retrieve the result and store it in $i but I can't see anything wrong with it.

    PHP:
    $query "select * from raf_members where email = '".$email."'";
      echo 
    $query;
      
    $result $db->query($query);
      if (!
    $result) {
          throw new 
    Exception('Could not execute query');
      }
        
    $i $result->fetch_array($result);
        echo 
    $i['mnum'];
    Also, as far as table structure, I have mulitple tables but for this particular code, it uses a members table, that stores the names, email, phone, etc. and then the ticket table, that stores ticket information and is linked to the members number by the members #, which is the result that I can't get to show up.
     
  8. dkotter thread starter macrumors member

    Joined:
    Jul 30, 2009
    #8
    I've checked some and it seems to be all right, since it is for a high school sports team and all the money is going to help them, and not in someones pocket, so to speak. And the client supposedly has talked to people higher up that told him it was all right.
     
  9. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #9
    Are you getting results at all? Does
    PHP:
    $result->num_rows
    return greater than 0?
     
  10. dkotter thread starter macrumors member

    Joined:
    Jul 30, 2009
    #10
    Thanks everyone for your help. I think I've finally solved it and got it working!

    PHP:
    $i $result->fetch_array($result);
    The code above was the error, which I knew it was something there but it took me awhile to see it. Using fetch_array, you're supposed to pass in the name of the row or # or both, and I was trying to pass in a variable.
     
  11. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #11
    Good catch. It's always a bit more difficult to catch errors in someone else's code. I didn't even notice that.

    Actually you passed it the $result object. Usually you'd not pass anything except perhaps a result type, MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH. If you don't pass anything it defaults to MYSQLI_BOTH.
    http://www.php.net/manual/en/mysqli-result.fetch-array.php
     
  12. dkotter thread starter macrumors member

    Joined:
    Jul 30, 2009
    #12
    Yeah that's the same link I found that helped me fix it. I ended up using MYSQLI_ASSOC and it seems to be working. Thanks for your help.
     

Share This Page