I would like my page to allow me to fill up a form and then press submit button that initiates another PHP file that firstly inserts the data into the database, and secondly creates an entirely new page using the given pageName fetched from the form and adds specified contents to it using PHP.
My codes are : The database connection:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'hm';
// Create connection
$conn = mysqli_connect($servername, $username, $password , $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The form:
<form action="add_recipe_script.php" method="post" enctype=”multipart/form-data” >
<label>Page Name <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="pageName" placeholder="Page name" required>
</div><br/>
<label>Recipe Name <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rName" placeholder="Recipe name" required>
</div><br/>
<label>Recipe Discription:<span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rDisc" placeholder="Recipe Description" required>
</div><br/>
<label>Recipe Image:<span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rImg" placeholder="Recipe Image" required>
</div><br/>
<label >Recipe Category: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rCategory" placeholder="Recipe Category" required>
</div><br/>
<label>No. of Calories: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rTotalCalories" placeholder="Recipe Calorie" required>
</div><br/>
<label>No. of Servings: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rServing" placeholder="Recipe Servings" required>
</div><br/>
<label>Cook time: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rTime" placeholder="Cook time" required>
</div><br/>
<label>Recipe Ingredients: <span style="color: #FF0000">*</span></label>
<div>
<textarea name="rIngre" placeholder="Recipe Ingredients" required></textarea>
</div><br/>
<label>Recipe Instructions: <span style="color: #FF0000">*</span></label>
<div>
<textarea name="rSteps" placeholder="Recipe Instructions" required> </textarea>
</div><br/>
<label>Fat: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rFat" placeholder="Recipe fat" required>
</div><br/>
<label>Carbs: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rCarb" placeholder="Recipe carbs" required>
</div><br/>
<label>Protein: <span style="color: #FF0000">*</span></label>
<div>
<input type="text" name="rPro" placeholder="Recipe protein" required>
</div><br/>
<button type="submit" name="addRecipe">Add Recipe</button>
</form>
The PHP file:
<?php
include 'connect.php';
if(isset($_POST['addRecipe']))
{
$rName = $_POST['rName'];
$rDisc = $_POST['rDisc'];
$rImg = $_POST['rImg'];
$rCategory = $_POST['rCategory'];
$rTotalCalories = $_POST['rTotalCalories'];
$rServing = $_POST['rServing'];
$rTime = $_POST['rTime'];
$rIngre = $_POST['rIngre'];
$rSteps = $_POST['rSteps'];
$rFat = $_POST['rFat'];
$rCarb = $_POST['rCarb'];
$rPro = $_POST['rPro'];
$pageName = $_POST['pageName'];
$sql = "INSERT INTO recipes ( rName, rDisc, rImg, rFat, rCarb, rPro, rTotalCalories, rTime, rIngre,
rSteps, rCategory, rServing, pageName)
VALUES ('$rName', '$rDisc', '$rImg', '$rFat', '$rCarb', '$rPro', '$rTotalCalories',
'$rTime', '$rIngre ', '$rSteps', '$rCategory', '$rServing', '$pageName')";
if (mysqli_query($conn, $sql)) {
echo "New record has been added successfully !";
} else {
echo "Error: " . $sql . ":-" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
<?php
$pageName = $_POST['pageName'];
$newpagecontent = '<html><head><title></title></head><body><p>J</p></body></html>';
$file = fopen($pageName . '.php', "x");
fwrite($file, $newpagecontent);
?>
It does work and creates a new page but the content won't show up and the URL bar doesn't have a .php extension it just gives this error: Not Found The requested URL was not found on this server. Please any help?