我在编写脚本,但这不起作用,我不知道为什么。
所以,这就是守则,谁不工作:
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;
}
}
我认为这是因为("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']);
我希望你能帮助我!
这是完整的代码:
<?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>
答案 0 :(得分:5)
您在第一个语句中缺少一个括号,它应该是$stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']));
答案 1 :(得分:1)
此行中的语法错误:
$stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']);
更改为:
$stmt = $conn->prepare("SELECT frage, status, antwort FROM kontakt WHERE userID = " . intval($row['userID']));
缺少')'