2 votes

Exécution du code PHP sans cliquer sur le bouton

Je travaille sur un site web qui utilise PHP, je veux mettre à jour une base de données lorsque le bouton est cliqué. Mais pour une raison quelconque, chaque fois que la page est chargée, le code s'exécute de toute façon, je ne veux pas que cela se produise car cela pourrait vraiment gâcher l'ensemble du code. Comment puis-je arrêter l'exécution automatique du script ?

<?php
    ob_start();
    session_start();
    include_once 'dbconnect.php';

    // if session is not set this will redirect to login page
    if( !isset($_SESSION['user']) ) {
        header("Location: index.php");
        exit;
    }

    $res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
    $userRow=mysql_fetch_array($res);
    //Here is where the script is
    if ( isset($_POST['send']) ) {
        if ( ! empty($_POST['sender'])){
            $name = $_POST['sender'];
        }
        if ( ! empty($_POST['reciever'])){
            $name = $_POST['reciever'];
        }

        $query = "UPDATE users SET userCoins = userCoins + 1  WHERE userName='Morgan'";
        $res = mysql_query($query);
        if ($res) {
            $error = "Success!";
        } else {
            $error = "Something Went Wrong!";
        }
    }
?>
<!DOCTYPE html>
<html>
    <?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
    <head>
        <title>ServiceCoin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
        <link rel="stylesheet" href="scripts/home/index.css" />
    </head>
    <body>
        <ul>
            <li><a href="#" class="a">ServiceCoin.com(image)</a></li>
            <li><a href="logout.php?logout" class="a">Sign Out</a></li>
            <li><a href="#" class="a">Contact</a></li>
            <li><a href="#" class="a">Get Service Coins</a></li>
            <li><a href="#" class="a">News</a></li>
            <li><a href="settings.php" class="a">Settings</a></li>
            <li><a href="#" class="a">Referrals</a></li>
            <li><a href="service.php" class="a">Services</a></li>
            <li><a href="home.php" class="a">Home</a></li>
        </ul>
        <br /><br />
        <center>
        <h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <span id="services"><?php echo $userRow['userCoins']; ?></span> Service Coins</h3>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
            <div class="form-group">
                <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
                    <input type="text" name="sender" class="form-control" placeholder="Enter Your Wallet Key" value="<?php echo $row['userCoins']; ?>" maxlength="15" />
                    <span class="text-danger"><?php echo $error; ?></span>
                </div>
                <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
                    <input type="text" name="reciever" class="form-control" placeholder="Enter The Recievers Wallet Key" value="<?php echo $row['userCoins']; ?>" maxlength="15" />
                    <span class="text-danger"><?php echo $error; ?></span>
                </div>

            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-block btn-primary" name="send">Sign Up</button>
            </div>
        </form>
        </center>
    </body>
</html>
<?php ob_end_flush(); ?>

UPDATE

Ma page est complètement blanche maintenant.

<?php
    ob_start();
    session_start();
    include_once 'dbconnect.php';

    if(!isset($_SESSION['user'])) {
        header("Location: index.php");
        exit;
    }

    $condition = empty($_POST['sender']) || empty($_POST['reciever']);
    if ($condition) {
        die; // if your post data is empty PHP will no longer be executed
    }

    $res= "SELECT * FROM users WHERE userId=".$_SESSION['user'];
    $mysqli->query($con, $res); // you are doing nothing with it in your code, why?
    $name = $_POST['sender'];       
    $name = $_POST['reciever'];

    $query = "UPDATE users SET userCoins = userCoins + 1  WHERE userName='Morgan'";
    $res = $mysqli->query($con, $query);
    if ($res) {
       $error = "Success!";
    } else {
       $error = "Something Went Wrong!";
    }
?>
<!DOCTYPE html>
<html>
    <?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
    <head>
        <title>ServiceCoin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
        <link rel="stylesheet" href="scripts/home/index.css" />
    </head>
    <body>
        <ul>
            <li><a href="#" class="a">ServiceCoin.com(image)</a></li>
            <li><a href="logout.php?logout" class="a">Sign Out</a></li>
            <li><a href="#" class="a">Contact</a></li>
            <li><a href="#" class="a">Get Service Coins</a></li>
            <li><a href="#" class="a">News</a></li>
            <li><a href="settings.php" class="a">Settings</a></li>
            <li><a href="#" class="a">Referrals</a></li>
            <li><a href="service.php" class="a">Services</a></li>
            <li><a href="home.php" class="a">Home</a></li>
        </ul>
        <br /><br />
        <center>
        <h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <span id="services"><?php echo $userRow['userCoins']; ?></span> Service Coins</h3>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
            <div class="form-group">
                <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
                    <input type="text" name="sender" class="form-control" placeholder="Enter Your Wallet Key" value="<?php echo $row['userCoins']; ?>" maxlength="15" />
                    <span class="text-danger"><?php echo $error; ?></span>
                </div>
                <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
                    <input type="text" name="reciever" class="form-control" placeholder="Enter The Recievers Wallet Key" value="<?php echo $row['userCoins']; ?>" maxlength="15" />
                    <span class="text-danger"><?php echo $error; ?></span>
                </div>

            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-block btn-primary" name="send">Sign Up</button>
            </div>
        </form>
        </center>
    </body>
</html>
<?php ob_end_flush(); ?>

1voto

Karol Gasienica Points 1472

Vous devez utiliser MySQLi . Pourquoi pas mysql_* vous pouvez lire aquí

Solution

Votre code pourrait ressembler à ce qui suit :

$mysqli = new mysqli("localhost", "my_user", "my_password", "table_name"); // here you will need your connection data, you can store it in dbconnect.php.

// if session is not set this will redirect to login page    
if(!isset($_SESSION['user'])) {
    header("Location: index.php");
    exit;
}

$condition = empty($_POST['sender']) || empty($_POST['reciever']);
if (!$condition) {
    $res= "SELECT * FROM users WHERE userId=".$_SESSION['user'];
    $mysqli->query($res); // you are doing nothing with it in your code, why?
    $name = $_POST['sender'];       
    $reciever = $_POST['reciever'];

    $query = "UPDATE users SET userCoins = userCoins + 1  WHERE userName='Morgan'";
    $res = $mysqli->query($query);
    if ($res) {
        $error = "Success!";
    } else {
        $error = "Something Went Wrong!";
        echo "Error: ".$mysqli->error; // here you can check your errors
    }
}

Manuel

Plus d'informations sur MySQLi peuvent être trouvés dans Manuel PHP

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X