Reputation: 41
I have a form in html5 and on submit it runs the php script which then connects to the MySQL database, insert it into a table and then write down all the lines that are in the table to a .txt file.
For some reason it gives the following warnings:
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
The form:
<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>
the PHP script:
<?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);
?>
Could anyone help me please?
Upvotes: 0
Views: 115
Reputation: 401
Check the permissions of "opdr002_config.txt" - most likely php doesn't have write permission.
Also check if
mysqli_query($con,"SELECT * FROM lines");
is running properly. try with:
mysqli_query($con,"SELECT * FROM
map_db
.lines
");
You might check the first query too.
See this function for cheching the query errors.
Upvotes: 1