简体   繁体   中英

Select Where don't work - user ID

I work on a script, but this don't work, i don't know why.

So yea, this is the Code, who not work :

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']);
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}

I think this is because ("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']);

I hope you can help me!

And this is the full code :

<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
        $user_home->redirect('index.php');
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>

<?php include_once 'header.php'; ?>

        <div id="wrapper">

        <div class="container">

            <div class="page-header">
            <h3>Kevin-Bank.ch - Bank & Shop und bald vieles mehr!</h3>
            </div>

            <form method="post" action="">
                <label>Minecraft Name:</label>
                <input disabled class="form-control" type="text" name="jajaj" value="<?php echo $row['userName']; ?>" required/>
                <input class="hidden" type="text" name="name" value="<?php echo $row['userName']; ?>" />
                <label>Email Adresse:</label>
                <input disabled class="form-control" type="text" name="jajajja" value="<?php echo $row['userEmail']; ?>" required/>
                <input class="hidden" type="text" name="email" value="<?php echo $row['userEmail']; ?>" />
                <label>Frage:</label>
                <textarea class="form-control" rows="5" name="frage" required="required"></textarea>
                <br >
                <button type="submit" name="senden" class="btn btn-md btn-info">Senden</button>
                <p></p>

        </div>
        </div>

<?php
if(isset($_POST["senden"])){
$hostname='localhost:3306';
$username='****';
$password='*****!';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=*****",$username,$password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line


$sql = "INSERT INTO kontakt (name, email, frage)
VALUES ('".$_POST["name"]."','".$_POST["email"]."','".$_POST["frage"]."')";


if ($dbh->query($sql)) {
     echo "<script type= 'text/javascript'>alert('Frage wurde gesendet, in kürzeste Zeit beantworten wir ihre Frage!');</script>";
}
else{
     echo "<script type= 'text/javascript'>alert('Frage konnte nicht gesendet werden, bitte melde dich bei Kevin4K!');</script>";
}

    $dbh = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

}
?>
<div class="container">
    <h2 align='center'>Die letzten Fragen von dir</h2><hr>
<?php
echo "<table align='center' class='table'>";
echo "<thead><tr><th>Frage</th><th>Status</th><th>Antwort</th></tr></thead>";

class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }

    function current() {
        return "<td>" . parent::current(). "</td>";
    }

    function beginChildren() { 
        echo "<tr>"; 
    } 

    function endChildren() { 
        echo "</tr>" . "\n";
    } 
} 

$servername = "localhost:3306";
$username = "kevin";
$password = "madarmadar22!";
$dbname = "mohammada_kevin";

$id = intval($_GET['userID']);

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']);
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table></div>";
?>

    </div>

    </div>

    <script src="asets/jquery-1.11.3-jquery.min.js"></script>
    <script src="asets/js/bootstrap.min.js"></script>


</body>
</html>

您在第一条语句中缺少一个括号,它应该是$stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']));

Syntax error in this line :

$stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']);

Change to :

$stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']));

missing ')'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM