I was able to succsess fully write to a database. The problem was the same information kept getting written so I created a json file that stored the id of each object and wrote some php to check if the id was already in the file and if not it would write the whole object to a database and just write the id to the json file. The checking of the json object and writing to the json file work but not the writing to my database. Any help would be much appreciated
Here is the code. From the beginning of the if statement to fclose is the code i added to perform the check that now stops me from writing to a databse.
<?php
$host="xxxxxxxxxxxx"; // Host name
$username="xxxxxxxxxxxx"; // Mysql username
$password="xxxxxxxxxxxxxx"; // Mysql password
$db_name="xxxxxxxxxxxxx"; // Database name
$name= $_POST['postname'];
$user= $_POST['postuser'];
$status= $_POST['poststatus'];
$id= $_POST['postid'];
$img= $_POST['postimg'];
//$id= $_POST['postid'];
//file_put_contents("index.json", $id, FILE_APPEND);
$list = file_get_contents('index.json');
$json = json_decode($list, true);
if (in_array($id, $json)) {
echo "already exists";
} else{
array_push($json, $id);
$file ="index.json";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, json_encode($json));
fclose($fh);
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$db_insert = mysql_query("INSERT INTO tweet(name, user, status, Img, tweetid) VALUES ('$name', '$user', '$status', '$img', '$id')");
}
?>