php - $_POST doesnt get inserted into posts table using bindValue while other field gets inserted
Get the solution ↓↓↓posts table in the databse has fields(id,datetime,title,category,author,image,post) all in lower case.
"Category" name feild picked from select option from the form using $_POST doesnt get inserted into posts table using bindValue while other field gets inserted. this code is of the form where the category get updated on the form from the category table from the database and updated in the select option element.
<div class="form-group">
<label for="CategoryTitle"><span class="FieldInfo">Choose Category </span></label>
<select class="form-control" id="CategoryTitle" name="Category">
<?php
global $ConnectingDB;
$sql = "SELECT id,title FROM category";
$stmt = $ConnectingDB->query($sql);
while($DataRows = $stmt->fetch()){
$Id = $DataRows["id"];
$CategoryName = $DataRows["title"];
?>
<option value=""><?php echo $CategoryName; ?></option>
<?php } ?>
</select>
</div>
this part is where the insertion into database happens when submit button clicked, using POST method the fields are fetched from form and then SQL query insert using bindValue.
<?php
if(isset($_POST["Submit"])){
$PostTitle =$_POST["PostTitle"];
$Category=$_POST["Category"];
$Image =$_FILES["Image"]["name"];
$Target ="Uploads/".basename($_FILES["Image"]["name"]);
$PostText =$_POST["PostDescription"];
$Admin = "Rahul";
date_default_timezone_set("Asia/Kolkata");
$CurrentTime=time();
$DateTime=strftime("%B-%d-%Y %H:%M:%S",$CurrentTime);
if(empty($PostTitle)){
$_SESSION["ErrorMessage"]="Title cannot be empty";
Redirect_to("AddNewPost.php");
}elseif (strlen($PostTitle)<5) {
$_SESSION["ErrorMessage"]="Post title should be greater than 5 character";
Redirect_to("AddNewPost.php");
}elseif (!isset($_POST["Category"])) {
$_SESSION["ErrorMessage"]="category not set.";
Redirect_to("AddNewPost.php");
}
elseif (strlen($PostText)>999) {
$_SESSION["ErrorMessage"]="Post Description should not be more than 1000 character.";
Redirect_to("AddNewPost.php");
}else {
// query to insert
$sql ="INSERT INTO posts(datetime,title,category,author,image,post)";
$sql .="VALUES(:datetime,:postTitle,:categoryName,:adminName,:imageName,:postDescription)";
$stmt = $ConnectingDB->prepare($sql);
$stmt->bindValue(':datetime',$DateTime);
$stmt->bindValue(':postTitle',$PostTitle);
$stmt->bindValue(':categoryName',$Category);
$stmt->bindValue(':adminName',$Admin);
$stmt->bindValue(':imageName',$Image);
$stmt->bindValue(':postDescription',$PostText);
$Execute=$stmt->execute();
if($Execute){
$_SESSION["SuccessMessage"]="Category with id: ".$ConnectingDB->lastInsertId()." added succesfully";
Redirect_to("AddNewPost.php");
}else {
$_SESSION["ErrorMessage"]="Something went wrong.";
Redirect_to("AddNewPost.php");
}
}
}//ending of submit
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.