I'm attempting to create a simple admin page for a website. Where the content from the form below, will populate within the p tags in the html doc
<html>
<body>
<form action="write.php" method="post">
Site Description: <input type="text" name="Description">
<input type="submit">
</form>
</body>
</html>
write.php
<?php
$con=mysqli_connect("BlahBlah","website","password","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Site (Description)
VALUES
('$_POST[Description]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
index.html
<html><head></head><body>
<div id="description><h1>Title</h1>
<p class="body">INFORMATION FROM FORM DISPLAYED HERE</p>
</body></html>
I'm new to sql, any help will be appreciated. Thanks
The code below should retrieve the data for you. You will need to change your index.html file to index.php
<?php
$con=mysqli_connect("BlahBlah","website","password","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `Description` FROM SITE";
$myContent = mysql_query($sql, $con) or die(mysql_error());
$row_myContent = mysql_fetch_assoc($myContent);
mysqli_close($con);
?>
<html><head></head><body>
<div id="description">
<h1>Title</h1>
<p class="body"><?php echo $row_myContent['Description']; ?></p>
</div>
</body></html>
This will only retrieve a single row, so if you have multiple rows (ie multiple pages) you will need to add some more code to tell mysql which row to retrieve.
try this:
write.php
<?php
$con=mysqli_connect("BlahBlah","website","password","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `Site` (`Description`)
VALUES
('".$_POST[Description]."')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
When you do some MySQL queries in PHP, you have to set the table-ownames of the DB into these `` signs. And the arrays in "" you have to pack into {} try this
$sql="INSERT INTO `Site` (`Description`)
VALUES ('{$_POST['Description']}')";
and after you got it working, you should have an instant look at mysql_real_escape_string as mentioned before.