猿问

在 PHP echo 中遇到引号问题

我在 PHP 上的引号有问题echo。我不确定是因为我使用了错误的引号/将它们放在错误的位置,还是因为我无法将按钮值设置为我拥有的值(请参阅代码)。


$sql = "SELECT * FROM calender WHERE userid=?;";

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {

  header ('Location: ../personalcalender.php?error=sqlerror');

  exit();

}

else {

  mysqli_stmt_bind_param($stmt, "s", $_SESSION['Id']);

  mysqli_stmt_execute($stmt);

  $result = mysqli_stmt_get_result($stmt);

  while ($row = mysqli_fetch_assoc($result)) {

    echo "<center>" . $row['type'] ." ". $row['subject'] ." ". $row['message'] ." ".

      "<button name='changeStatus' value=\". $row['calenderid'] .\">" . $row['mystatus'] . $row['calenderid'] . "</button>";

  }

}


梦里花落0921
浏览 481回答 1
1回答

肥皂起泡泡

保持您的 HTML 和您的 PHP 良好分离将防止此类问题。理想情况下,您将它们放在单独的文件中,但至少将您的 PHP 放在文件的顶部,并将 HTML 中的任何内容限制为仅显示。将您的数据库结果加载到一个数组中,然后在适当的时间循环遍历它们。如果您要输出到 HTML,请始终使用htmlspecialchars().&nbsp;注意替代控制结构语法和短回显标签的使用。此外,1999 年打电话,他们希望他们的<center>元素回来 ;)<?php$sql = "SELECT * FROM calender WHERE userid = ?";$stmt = $conn->stmt_init();if (!$stmt->prepare($sql)) {&nbsp; &nbsp; header ('Location: ../personalcalender.php?error=sqlerror');&nbsp; &nbsp; exit();}$stmt->bind_param("s", $_SESSION['Id']);$stmt->execute();$result = $stmt->get_result($stmt);while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; $data[] = array_map('htmlspecialchars', $row);}?><!DOCTYPE html><html><head>&nbsp; &nbsp; <title>My page</title></head><body><!-- some more HTML goes here --><?php foreach ($data as $row):?><div style="text-align: center">&nbsp;&nbsp; &nbsp; <?=$row['type']?> <?=$row['subject']?> <?=$row['message']?>&nbsp; &nbsp; <button name="changeStatus" value="<?=$row['calenderid']?>">&nbsp; &nbsp; &nbsp; &nbsp; <?=$row['mystatus']?> <?=$row['calenderid']?>&nbsp; &nbsp; </button></div><?php endforeach;?><!-- some more HTML goes here --></body></html>
随时随地看视频慕课网APP
我要回答