使用PHP将MySQL数据库转换为.TXT

使用PHP将MySQL数据库转换为.TXT,php,html,mysql,Php,Html,Mysql,我在html5中有一个表单,提交时它运行php脚本,然后连接到MySQL数据库,将其插入表中,然后将表中的所有行写入.txt文件 出于某种原因,它会发出以下警告: 1 record added Warning: fopen(C:/xampp2/htdocs/bap000/opdr002_config.txt): failed to open stream: No error in C:\xampp2\htdocs\bap000\opdr002_input.php on line 25 Warn

我在html5中有一个表单,提交时它运行php脚本,然后连接到MySQL数据库,将其插入表中,然后将表中的所有行写入.txt文件

出于某种原因,它会发出以下警告:

1 record added
Warning: fopen(C:/xampp2/htdocs/bap000/opdr002_config.txt): failed to open stream: No error in C:\xampp2\htdocs\bap000\opdr002_input.php on line 25

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp2\htdocs\bap000\opdr002_input.php on line 28

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp2\htdocs\bap000\opdr002_input.php on line 33
表格:

<html>
<head>
<title>bap les</title>
</head>
<body>

<form name="formOne" method="post" action="opdr002_input.php">
Color:
<select name="color">
<option value="blue">Blue</option>
<option value="red">Red</option>
</select>
<br />
X:
<input type="number" name="xCord" maxlength="3" />
<br />
Y:
<input type="number" name="yCord" maxlength="3" />
<br />
Z:
<input type="number" name="zCord" maxlength="3" />
<br />

<input type="submit" />
</form>

</body>
</html>

巴普莱斯
颜色:
蓝色
红色

X:
Y:
Z:
PHP脚本:

<?php
// Make connection
$con = mysqli_connect("localhost","root","","map_db");

// Check connection
if (mysqli_connect_error()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Insert values into the Database
$sql = "INSERT INTO `map_db`.`lines` (`color`, `xCords`, `yCords`, `zCords`) VALUES
('$_POST[color]','$_POST[xCord]','$_POST[yCord]','$_POST[zCord]')";

// Check for errors
if (!mysqli_query($con, $sql)) {
    die('Error: ' . mysqli_error($con));
}

// Redirect user to page saying:
echo "1 record added";

// $result will contain everything inside the lines table
$result = mysqli_query($con,"SELECT * FROM lines");
$data = null;
$theFile = fopen("C:/xampp2/htdocs/bap000/opdr002_config.txt", "W");

// Loop through the lines using $row
while ($row = mysqli_fetch_array($result)) {
    $data = $row['color'] . "," . $row['xCords'] . "," . $row['yCords'] . "," . $row['zCords'] . '\n';
    $addData = fputs($theFile, trim($data));
}

fclose($theFile);

// Close the connection
mysqli_close($con);
?>
检查“opdr002_config.txt”的权限-很可能php没有写权限

还要检查一下

mysqli_查询($con,“从行中选择*)

机器运转正常。 尝试:

mysqli_query($con,“从
map_db
行中选择*”

您也可以检查第一个查询


有关检查查询错误的信息,请参阅。

请在编写更多SQL接口代码之前,您必须仔细阅读以避免出现严重错误。如前所述,有人可能在几秒钟内毁掉你的整个网站。模式
W
意味着什么?
fopen()
函数的文档中没有用大写字母定义任何模式。方法是:了解mysqli的安全优势和它提供的“准备好的声明”功能。看看你是否能找到问题。。。另外,请确保XAMP能够写入映射(具有读写权限)
'$\u POST[color]'
必须是
'.$\u POST['color'.'”
$arr[test]
$arr['test']
之间的区别在于,一个元素有一个常量作为标识符,另一个元素有一个字符串。谢谢!这修复了它,它现在将它写入.txt文件,但是。。。“\n”不起作用。