Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Can't use else if help!

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Can't use else if help!

Postby lincon » Wed Apr 03, 2013 8:04 am

Hi,

I hope someone can help, I simply want to have lots of else if but it's not letting me put just 1 in there. :help:
I've tried using "switch" but because I don't know the email address before someone enters it switch won't work.

I've seen other people have problems with else if and query so I don't know if that's my problem.
Any help would be soooo well received. Thanks for taking the time.

Code: Select all
<?php
$username = "xxxx";
   $password = "xxxx!";
   $hostname = "xxxxx";
   
   
   $dbhandle = mysql_connect($hostname, $username, $password, $db_table ) or die("Could not connect to database");
   
   $selected = mysql_select_db("users", $dbhandle);
   

      if(isset($_POST['payer_email'])){
         $payer_email = $_POST['payer_email'];
         

         $query = mysql_query("SELECT * FROM tests WHERE payer_email='$payer_email'");
         if(mysql_num_rows($query) > 0 ) { //check if there is already an entry for that username
               echo "User has been blacklisted!";
               }
               
{ else if ($query = mysql_query("SELECT * FROM over_month_user WHERE payer_email='$payer_email'");
         if(mysql_num_rows($query) > 0 ) { //check if there is already an entry for that username
               echo "over_month_user!";
               
         } else {
            mysql_query("INSERT INTO tests (first_name, payer_email) VALUES ('$first_name', '$payer_email')");
            echo "details stored in tests database!";
         }
   }
   mysql_close();
?>

<html>
   <body>
      <h1>insert user</h1>
         <form method="post" action="">
            <p>first name:</p><input type="text" name="first_name" />
            <p>payer_email:</p><input type="payer_email" name="payer_email" />
            <br />
            <input type="Submit" value="Submit" />
         </form>
   </body>
</html>
lincon
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 03, 2013 7:54 am

Re: Can't use else if help!

Postby seandisanti » Wed Apr 03, 2013 8:46 am

1) mysql_ functions are deprecated and will probably be removed in an upcoming update, because they're slow and less secure than mysqli_ or PDO which has become the standard for database interactions.

2) Rather than having a bunch of calls to your mysql_query function based on your conditions, have an $sql variable that is assigned a different statement based on your conditions, and then you can have one execute point for the query which will make debugging a lot easier.

will add more in a bit, i have a meeting now
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Can't use else if help!

Postby seandisanti » Wed Apr 03, 2013 9:45 am

Ok, here is a link to a quick PDO lesson that will make your life easier and your code better: http://jream.com/learning/videos/php-oo ... o-examples
It's very short but a huge help. Also make sure you search the forum as I'm sure your question has been answered several times. Just search for logins, database queries etc.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Can't use else if help!

Postby lincon » Wed Apr 03, 2013 10:07 am

Thanks so much for the quick replies, I've only just started learning php :(

I'm watching the video now, but is it possible to mix PDO and php, meaning can I keep everything I've done with IPN already and add PDO in to the script.

Mainly I'd like to keep the script above (spent 2 weeks getting that, I know :( ) but where it the submit button is pressed it then goes in to PDO so I have my 6 or 7 different query's (checks on something before moving to the next)

Hope this makes sense?
Thanks
lincon
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 03, 2013 7:54 am

Re: Can't use else if help!

Postby seandisanti » Wed Apr 03, 2013 10:40 am

PDO will only take over the database interaction part for you. It can be used procedurally the way you're using the mysql_ functions right now, or objectively as demonstrated in the video. Check http://php.net/PDO for explanation of procedural implementation. Right now I'm responding from my iPad so I can't conveniently work on your code but I expect to be able to do some revisions for you in about 2 hours
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Can't use else if help!

Postby lincon » Wed Apr 03, 2013 11:37 am

Aww thank you, if you do manage to work out why I can't add else if or work out the best way for me to have many different else type query's I would love to donate to you (if that's allowed and not breaking any forum rules)

I've spent so long on this, first I spent a week on IPN only to find paypal had the problem not me, then another week learning as much as I can on php only to find it getting removed next year. arrg :(

Any help will save my bleeding eyes. (p.s. I'm serious about a donation)
lincon
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 03, 2013 7:54 am

Re: Can't use else if help!

Postby seandisanti » Wed Apr 03, 2013 12:40 pm

donation not required, but i'm not stupid enough to turn away money whether the rules say i'm supposed to or not.

here is how i'd re-write your form processing page.
Code: Select all


include 
'Database.php';
if ($_POST) {
    $db = Database::getInstance();
    if (isset($_POST['payer_email'])) {
        $payer_email = $db->quote($_POST['payer_email']);
        $sql = "SELECT * FROM tests WHERE payer_email=" . $payer_email;
        $inTests = $db->query($sql);
        if (!empty($inTests))
            die('User has been blacklisted!');
    }
//if we get to this point, then wasn't in tests
    $sql = "SELECT * FROM over_month_user WHERE payer_email=" . $payer_email;
    $overMonth = $db->query($sql);
    if (!empty($overMonth)) {
        die('over_month_user!');
    }
    $sql = "INSERT INTO tests (first_name, payer_email) VALUES (" . $db->quote($first_name) . "," . $payer_email . ")";
    $return = $db->query($sql);
    if (!$return) {
        die('There was an error<br />' . $db->errorInfo());
    } else {
        echo 'Success, your id is :' . $db->lastInsertId();
    }
}
 else {
    echo '<html>
    <body>
        <h1>insert user</h1>
        <form method="post" action="'
 . $_SERVER['PHP_SELF'] . '">
            <p>first name:</p><input type="text" name="first_name" />
            <p>payer_email:</p><input type="payer_email" name="payer_email" />
            <br />
            <input type="Submit" value="Submit" />
        </form>
    </body>
</html>'
;
}

at the top you'll see i included my Database.php that file is :
Code: Select all

<?php
/*
 * Database class only one connection is allowed.
 */

class Database extends PDO{

    private $connection;
    private static $instance;

    public static function getInstance() {
        if (!self::$instance) {
            
            self
::$instance = new self("mysql:host=". DBHOST . ";dbname=".DB,DBUSER,DBPASSWORD);
        }
        return self::$instance;
    }



    /*
     * empty clone magic method to prevent duplication
     *
     */

    private function __clone() {

    }

    public function getConnection() {
        return $this->connection;
    }

}

all you have to do is supply it your database location and credentials in place of the constants that I use, and make sure to save it in the same folder as the other file (or update the 'include' accordingly).
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Can't use else if help!

Postby lincon » Thu Apr 04, 2013 2:16 am

Hi
Thanks, haven't had time to look at it, will in about an hour or so, will pm you about the other thing :)
Thanks
lincon
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 03, 2013 7:54 am

Re: Can't use else if help!

Postby lincon » Thu Apr 04, 2013 4:34 am

Hi

I've had a look and tried to get it to work I think my problem is the database details are different to the way I used on php.

I couldn't see where I need to put db_name, host, etc, so I looked on the net and found this code.
Code: Select all
private static $dbhost = '21xxxxxx';
private static $dbuser = 'xxxxxxxx';
private static $dbpass = 'xxxx!';
private static $dbname = 'xxxxx';


Which I think needs to be added, so I put it here.

Code: Select all
<?php
/*
 * Database class only one connection is allowed.
 */
private static $dbhost = '213xxxxx';
private static $dbuser = 'xxxxx';
private static $dbpass = 'sxxxxx!';
private static $dbname = 'phxxxl';
class Database extends PDO{

    private $connection;
    private static $instance;


I also put the <?php & ?> top and bottom of the code, does PDO need that?

Also (sorry) If I wanted to add more if's would I just repeat the same over and over like this:

Code: Select all
 $sql = "SELECT * FROM over_month_user WHERE payer_email=" . $payer_email;
    $overMonth = $db->query($sql);
    if (!empty($overMonth)) {
        die('over_month_user!');
   
 }
 $sql = "SELECT * FROM tests WHERE quantity=" . $quantity;
    $overMonth = $db->query >1($sql);
    if (!empty($overMonth)) {
        die('ordered 1 too many');

 }
    $sql = "INSERT INTO tests (first_name, payer_email) VALUES (" . $db->quote($first_name) . "," . $payer_email . ")";
    $return = $db->query($sql);
    if (!$return) {
        die('There was an error<br />' . $db->errorInfo());


Thank you so much.
lincon
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 03, 2013 7:54 am

Re: Can't use else if help!

Postby johnj » Thu Apr 04, 2013 6:41 am

@lincon - Please read about the following before you proceed:
a). What is PHP and what tags are needed for a php file, difference between short tag and normal php tag
b). What is a class and class variable
c). What is PDO?

Unless you understand what the above are, it will be difficult for you to proceed.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron