gpt4 book ai didi

php - HTML 和 PHP/MySQL 故障排除

转载 作者:行者123 更新时间:2023-11-29 05:16:54 25 4
gpt4 key购买 nike

长期读者,第一次海报。我是一个新手PHP爱好者,我有一个我一直在工作的页面。现在我的数据库连接运行良好,我的 SELECT 语句为我提供了所需的信息。我的问题有两个(在这篇文章之后可能更多;让你的移相器退缩):

  1. 有一次,我让 INSERT 正常工作,但它突然停止了,似乎没有多少调整可以让它恢复。我已验证 INSERT 语句在没有变量的单独 PHP 文件中有效。

  2. 当我确实让 INSERT 工作时,每次刷新页面都会复制最后一个条目。我已经尝试了多种方法来清除 $_POST 数组,但我认为我的一些实验又回到了问题 #1。

<?php 
$dbhost = "REDACTED";
$dbuser = "REDACTED";
$dbpass = "REDACTED";
$dbname = "guest_list";
// Create a database connection
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("DB's not here, man: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
// replacement for mysql_real_escape_string()
function html_escape($html_escape) {
$html_escape = htmlspecialchars($html_escape, ENT_QUOTES | ENT_HTML5, 'UTF-8');
return $html_escape;
}

// Posting new data into the DB
if (isset($_POST['submit'])) {
$first = html_escape($_POST['first']);
$last = html_escape($_POST['last']);
$contact = html_escape($_POST['contact']);
$associate = html_escape($_POST['associate']);

$insert = "INSERT INTO g_list (";
$insert .= "g_fname, g_lname, g_phone, g_association) ";
$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";
$i_result = mysqli_query($connection, $insert);
// I have verified that the above works by setting the varialble
// in the VALUES area to strings and seeing it update
}

$query = "SELECT * ";
$query .= "FROM g_list ";
$query .= "ORDER BY g_id DESC";
$q_result = mysqli_query($connection, $query);

?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Guest List</title>
<link href="guest.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1>REDACTED</h1>
<h2>Guest Registry</h2>
</header>
<div class="container">
<div class="registry">
<form name="formup" id="main_form" method="post">
<fieldset>
<legend>Please enter your name into the registry</legend>
<p class="first">First Name:
<input type="text" name="first" value="" placeholder="One or more first names" size="64"></p>
<p class="last">Last Name:
<input type="text" name="last" value="" placeholder="Last name" size="64"></p>
<p class="contact">Phone Number or Email:
<input type="text" name="contact" value="" placeholder="" size="32"></p>
<p class="associate">Your relation?
<input type="text" name="associate" value="" placeholder="" size="128"></p>
<p class="submit">
<input type="submit" name="submit" title="add" value="submit" placeholder=""></p>
</fieldset>
</form>
</div>
</div>

<h3>Guest List:</h3>
<table>
<tr>
<th>Firstname(s)</th><th>Lastname</th>
<th>Phone or Email</th><th>Association</th>
</tr>

<?php while($guest = mysqli_fetch_assoc($q_result)) {
echo "<tr>" . "<td>" . $guest["g_fname"] . "</td>"
. "<td>" . $guest["g_lname"] . "</td>"
. "<td>" . $guest["g_phone"] . "</td>"
. "<td>" . $guest["g_association"] . "</td>" . "</tr>";
} ?>

</table>
<footer>
<div>Copyright <?php echo date("Y"); ?>, REDACTED, LLC.</div>

<?php
if (isset($connection)) {
mysqli_close($connection);
}
?>
</footer>
</body>
</html>

最佳答案

这两行会失败:

$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";

这里有两个问题,都与第二行有关:

  • ) 和 LIMIT 之间没有空格:)LIMIT 1 是您的代码;
  • 不允许 INSERT 中的 LIMIT 1....

关于php - HTML 和 PHP/MySQL 故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31496701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com