在多个文件中用MySQL进行PHP的变量传输

时间:2019-03-12 06:33:41

标签: php mysql

我正在编码PHP,我复制了一个文件并想添加一些东西

我想指定一个变量,我可以将其带到下一个文件,然后对数据库创建操作(我想将该变量与fragen_id进行比较)。将使用“ SELECT * FROM fragen WHERE fragen_id == $ chosen_one”进行比较。变量$ chosen_one由另一个文件中的oncklick制成:

<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
include("templates/header.inc.php");// simple the header, nothing important ;)
$zahl=4;
?>

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen");
$result = $statement->execute();
while($row = $statement->fetch()) {

        echo '<a class="kein" href="forum_forum.php"><div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subt  itel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "br><br>",'<div>'.$row['fragen_id'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div></a>';



}
$statement = $pdo2->prepare("SELECT fragen_id FROM fragen");
$result = $statement->execute();
echo $result;
?>
<?php 
include("templates/footer.inc.php");
?>

只是为了开释:“ fragen”在我的母语中意为“问题” 现在,数据库中的另一个PHP文件应该只读取一行。该行由上一个文件确定

<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
include("templates/header.inc.php");
$chosen_one=1;
?>

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen WHERE fragen_id==$chosen_one");
$result = $statement->execute();
while($row = $statement->fetch()) {
        echo '<div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subitel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "<br><br>",'<div class="forum_Bild"><img class="pic" alt="Ein Bild" title="Eine Pflanzen" src="'.$row['fragen_picture'].'">',
        "<br><br>","Bildlink:  ".$row['fragen_picture'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div>';

}
?>
<?php 
include("templates/footer.inc.php");
?>

和functions.inc.php

<?php
/**
 * A complete login script with registration and members area.
 *
 * @author: Nils Reimers / http://www.php-einfach.de/experte/php-codebeispiele/loginscript/
 * @license: GNU GPLv3
 */
include_once("password.inc.php");

/**
 * Checks that the user is logged in. 
 * @return Returns the row of the logged in user
 */
function check_user() {
    global $pdo;

    if(!isset($_SESSION['userid']) && isset($_COOKIE['identifier']) && isset($_COOKIE['securitytoken'])) {
        $identifier = $_COOKIE['identifier'];
        $securitytoken = $_COOKIE['securitytoken'];

        $statement = $pdo->prepare("SELECT * FROM securitytokens WHERE identifier = ?");
        $result = $statement->execute(array($identifier));
        $securitytoken_row = $statement->fetch();

        if(sha1($securitytoken) !== $securitytoken_row['securitytoken']) {
            //Vermutlich wurde der Security Token gestohlen
            //Hier ggf. eine Warnung o.ä. anzeigen

        } else { //Token war korrekt
            //Setze neuen Token
            $neuer_securitytoken = random_string();
            $insert = $pdo->prepare("UPDATE securitytokens SET securitytoken = :securitytoken WHERE identifier = :identifier");
            $insert->execute(array('securitytoken' => sha1($neuer_securitytoken), 'identifier' => $identifier));
            setcookie("identifier",$identifier,time()+(3600*24*365)); //1 Jahr Gültigkeit
            setcookie("securitytoken",$neuer_securitytoken,time()+(3600*24*365)); //1 Jahr Gültigkeit

            //Logge den Benutzer ein
            $_SESSION['userid'] = $securitytoken_row['user_id'];
        }
    }


    if(!isset($_SESSION['userid'])) {
        die('Bitte zuerst <a href="login.php">einloggen</a>');
    }


    $statement = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $result = $statement->execute(array('id' => $_SESSION['userid']));
    $user = $statement->fetch();
    return $user;


}

/*
Returns true when the user is checked in, else false
*/
function is_checked_in() {
    return isset($_SESSION['userid']);
}

/**
 * Returns a random string
 */
function random_string() {
    if(function_exists('openssl_random_pseudo_bytes')) {
        $bytes = openssl_random_pseudo_bytes(16);
        $str = bin2hex($bytes); 
    } else if(function_exists('mcrypt_create_iv')) {
        $bytes = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
        $str = bin2hex($bytes); 
    } else {
        //Replace your_secret_string with a string of your choice (>12 characters)
        $str = md5(uniqid('your_secret_string', true));
    }   
    return $str;
}

/**
 * Returns the URL to the site without the script name
 */
function getSiteURL() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    return $protocol.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/';
}

/**
 * Outputs an error message and stops the further exectution of the script.
 */
function error($error_msg) {
    include("templates/header.inc.php");
    include("templates/error.inc.php");
    include("templates/footer.inc.php");
    exit();
}

0 个答案:

没有答案