gpt4 book ai didi

php 和 MySQL 初学者接受用户输入、修改数据库并返回值

转载 作者:行者123 更新时间:2023-11-29 21:55:54 24 4
gpt4 key购买 nike

我将用这样的事实作为这个问题的序言:我只有六周的 PHP 经验,只有两周的 MySQL 经验。我正在尝试创建一个演示网站,该网站将接受用户输入的软件错误。这些表单接受用户输入,并通过使用 PHP 连接到我建立的本地 SQL 数据库。数据库“bugs”已经建立,bugDetails表也已经建立,包括“版本”、“硬件”、“操作系统”和“描述”字段。我已经构建了一个 PHP 文档来概述表单并将信息传递到 SQL 数据库。我还创建了两个包含文件来保护数据库密码,并解决魔术引号问题。在我的一生中,我无法让代码工作,我不确定为什么。非常感谢任何帮助。这是我的代码:

错误.php

<html>
<head>
<title>Submit a bug</title>
<style>
input {
display: inline;
width: auto;
text-align: left;
}
</style>
</head>
<body>

Please enter info about the issue you have encounted:
<form id="add_rec" name="add_record" method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
<div>
<label for="version">Software Version:</label>
<input name="version" type="text" id="version"
size="128"
maxlength="127" />
</div>
<div>
<label for="hardware">Hardware Used: </label>
<input name="hardware" type="text" id="hardware"
size="128"
maxlength="127" />
</div>
<div>
<label for="os">Operating System: </label>
<input name="os" type="text" id="os" size="128"
maxlength="127" />
</div>
<div>
<label for="description">Describe the Issue: </label>
<input name="description" type="text" id="description" size="4000"
maxlength="3999" />
</div>
</form>


<?php

// Process data from the form
if(isset($_POST['add'])) {
include 'fix_magic_quotes.php';
include 'open_db.php';

$version = mysqli_real_escape_string($link,
$_POST['version']);
$hardware = mysqli_real_escape_string($link,
$_POST['hardware']);
$os = mysqli_real_escape_string($link,
$_POST['os']);
$description = mysqli_real_escape_string($link, $_POST['description']);

$sql = "INSERT INTO bugDetails SET
version = '$version',
hardware = '$hardware',
os = $os,
description ='$description',

if (!mysqli_query($link, $sql)){
$output = 'Could not submit your issue to table: ' .
mysqli_error($link);
include 'output.php';
exit();
}
$output = 'Your issue has been successfully logged to table:';
include 'output.php';
} // end if
?>
</body>
</html>

open_db.php

<?php
// Log into MySQL
$link = mysqli_connect('localhost', 'andrew', 'Heruka123!');
if (!$link){
$output = 'Unable to connect to the database server.';
include 'output.php';
exit();
}
// Set database encoding
if (!mysqli_set_charset($link, 'utf8')){
$output = 'Unable to set database connection encoding.';
include 'output.php';
exit();
}
// Open the database
if (!mysqli_select_db($link, 'bugs')){
$output = 'Unable to locate the joke database.';
include 'output.php';
exit();
}
// No errors found so inform user
$output = 'Database connection established.';
include 'output.php';
?>

fix_magic_quotes.php

<?php
// Fix Magic Quotes Problem
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);

return $value;
}// end function

$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}// end if
?>

最佳答案

在您的 bugs.php 中,您的 PHP 不完整。您没有使用 "; 关闭 $sql 的分配。该 SQL 不是 INSERT 的正确语法:

<?php
// Process data from the form
if(isset($_POST['add'])) {
include 'fix_magic_quotes.php';
include 'open_db.php';

$version = mysqli_real_escape_string($link, $_POST['version']);
$hardware = mysqli_real_escape_string($link, $_POST['hardware']);
$os = mysqli_real_escape_string($link, $_POST['os']);
$description = mysqli_real_escape_string($link, $_POST['description']);

$sql = "INSERT INTO bugDetails (version, hardware, os, description) VALUES ('$version', '$hardware', $os, '$description');";
if (!mysqli_query($link, $sql)){
$output = 'Could not submit your issue to table: ' .
mysqli_error($link);
include 'output.php';
mysqli_close($link);
exit();
}
$output = 'Your issue has been successfully logged to table:';
include 'output.php';
} // end if
?>

关于php 和 MySQL 初学者接受用户输入、修改数据库并返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161445/

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