I am doing an exercise with PHP and MySQL database management, and I'm trying to return an amount from a field in a table with PHP. I am trying to get the value from the CalsPerServ in the foods table where the foodsID is equal to the foodsID the user has selected. All I'm getting now though is an array, when I should be returning one result. Any idea what went wrong?
if($tableName == "meals"){
$foodsID = filter_input(INPUT_POST, "foodsID");
$servings = filter_input(INPUT_POST, "Servings");
$foodsID = mysql_real_escape_string($foodsID);
$servings = mysql_real_escape_string($servings);
$query = "SELECT CalsPerServ FROM foods WHERE foodsID=$foodsID";
$result = mysql_query($query, $connect);
$CalsPerServ = mysql_fetch_assoc($result);
print $CalsPerServ;
$calories = ($CalsPerServ * $servings) * 100;
$sql .= "(foodsID, Servings, Calories) VALUES ('$foodsID', '$servings', '$calories')";
$sqltwo = "INSERT INTO daily_diary (foodsID, CaloriesPlus) VALUES ('$foodsID', '$calories')";
$resulttwo = mysql_query($sqltwo, $connect);
}
mysql_query
in 2013. Take a look at mysqli or PDO. Either one supports prepared statements, which for one thing means you don't have to do all this manual escaping crap and cobbling together SQL by hand.