0

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?

7
  • Please, before you write any more SQL interfacing code, you must read up on proper SQL escaping to avoid severe SQL injection bugs. As written, someone could ruin your entire website in seconds. Commented Apr 23, 2014 at 12:57
  • 1
    What is mode W meant to be? The documentation of the fopen() function does not define any modes with capital letters. Commented Apr 23, 2014 at 12:59
  • What @PetrR. means is: learn about the security advantages of mysqli and the "prepared statement" feature it offers. Commented Apr 23, 2014 at 13:00
  • Take a look here and see if you can find the problem... Also, make sure XAMP is able to write to the map ( with read and write permisions) Commented Apr 23, 2014 at 13:02
  • 3
    '$_POST[color]' must be '" . $_POST['color'] . "'. The difference between $arr[test] and $arr['test'] is, one element has a constant as identifier, the other a string. Commented Apr 23, 2014 at 13:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This fixed it, it now writes it to the .txt file, however... the "\n" doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.