My app (iOS swift 2.0) communicates successfully with a server side PHP-script. I want user input (through a HTML-form) to be saved to my MySQL-database. This works too.
Furthermore, I want all the table-data to be saved to a JSON-file on my server, so that app-users can load this into an iOS UITableview. The latter worked for me yesterday.
Today the JSON-file is empty and I'm unable to save data to it. I'm currently unable to retrieve any MySQL table-data (which I could yesterday).
My goal: I want the JSON-file to mirror the database at any given moment; hence the following process as a whole needs to be triggered by user submit: User submits data (demand_form.html) -> user input is saved to database (demand_process.php) -> database content is fetched and saved to JSON-file on server (demand_to_JSON.php).
This is the server side code (HTML/PHP):
demand_form.html
:
<form method="post" action="demand_process.php">
Category : <input type="text" name="category" placeholder="Enter photo category" /><br />
Expires : <input type="text" name="expires" placeholder="When do you need this?" /><br />
Location : <input type="text" name="location" placeholder="In what city?"/><br />
Country : <input type="text" name="country" placeholder="In what country?"/><br />
Free Text : <input type="text" name="freetext" placeholder="Specify demand, if needed"/><br />
<input type="submit" value="Submit" />
</form>
demand_process.php
:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {//Check it is comming from a form
//mysql credentials
$mysql_host = "****.com.mysql";
$mysql_username = "****";
$mysql_password = "****";
$mysql_database = "****";
$category = filter_var($_POST["category"], FILTER_SANITIZE_STRING); //set PHP variables like this so we can use them anywhere in code below
$expires = filter_var($_POST["expires"], FILTER_SANITIZE_STRING);
$location = filter_var($_POST["location"], FILTER_SANITIZE_STRING);
$country = filter_var($_POST["country"], FILTER_SANITIZE_STRING);
$freetext = filter_var($_POST["freetext"], FILTER_SANITIZE_STRING);
//Open a new connection to the MySQL server
$mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$statement = $mysqli->prepare("INSERT INTO Demand_data (category, expires, location, country, freetext) VALUES(?, ?, ?, ?, ?)"); //prepare sql insert query
//bind parameters for markers, where (s = string, i = integer, d = double, b = blob)
$statement->bind_param('sssss', $category, $expires, $location, $country, $freetext); //bind values and execute insert query
if($statement->execute()){
print "Hello " . $u_name . "!, your message has been saved!";
}else{
print $mysqli->error; //show mysql error if any
}
}
?>
demand_to_JSON.php
:
<?php
//open connection to mysql db
try {
$connection=mysqli_connect("****.mysql", "****", "****", "****");
echo "Connected to database successfully.";
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
//fetch table rows from mysql db
$sql = "SELECT * FROM `Demand_data` LIMIT 0, 30";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray []= $row;
}
echo json_encode($emparray);
$fp = fopen('demand_data.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
I tried to include the demand_to_JSON.php to the demand_process.php, within the final if statement (upon success).
My questions somewhat summarized: How can I save the user input to my JSON-file (demand_data.json) by submitting new data, and then keep this JSON-file continuously mirroring the MySQL table-data?
</div>