How do I process form checkbox into mysql? - php

I want to get the selected values from the processed into my database? How can Achieve that?
I'm just getting one result stored into the database. My goal is that each user can do a multiple selection from a list of receipts numbers under their accounts and then when they submit the request I want to get the selected receipt numbers in the database.
here is my form.
<form action="" method="post">
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="warehouse"><p><b><i class="fa fa-folder-open-o"></i> Selecciona a continuación los recibos de bodega que deseas enviar :</b></p></label><br>
<?php
// Get the Client List
$qry4 = "SELECT
projectId,
clientId,
projectName,
store,
projectDeatils
FROM
clientprojects
WHERE clientId = ".$clientId."";
$res4 = mysqli_query($mysqli, $qry4) or die('-4'.mysqli_error());
while ($d = mysqli_fetch_assoc($res4)) {
?>
<input type="checkbox" name="warehouse" value="<?php echo $d['projectName']; ?>" >
<?php echo clean($d['store']); ?> | <a target="_blank" href="index.php?page=viewProject&projectId=<?php echo clean($d['projectId']); ?>" data-toggle="tooltip" data-placement="right" title="Ver En Detalle">
<?php echo clean($d['projectName']); ?></a>
: <a data-toggle="tooltip" data-placement="right" title="<?php echo clean($d['projectDeatils']); ?>"> <?php echo ellipsis($d['projectDeatils'],50); ?></a><br>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="input" name="submit" value="requestShipment" class="btn btn-success btn-icon"><i class="fa fa-check-square-o"></i> <?php echo $reqQuoteBtn; ?></button>
<button type="button" class="btn btn-default btn-icon" data-dismiss="modal"><i class="fa fa-times-circle-o"></i> <?php echo $cancelBtn; ?></button>
</div>
Here is my database connection:
// New shipment Request
if (isset($_POST['submit']) && $_POST['submit'] == 'requestShipment') {
// Validation
if($_POST['warehouse'] == "") {
$msgBox = alertBox($projTitleReq, "<i class='fa fa-times-circle'></i>", "danger");
} else if($_POST['shipmentAddress'] == "") {
$msgBox = alertBox($addressStreetWarning, "<i class='fa fa-times-circle'></i>", "danger");
} else {
$warehouse = $_POST['warehouse'];
$shipmentAddress = $_POST['shipmentAddress'];
$requestDate = date("Y-m-d H:i:s");
$stmt = $mysqli->prepare("
INSERT INTO
shipmentrequest(
clientId,
warehouse,
shipmentAddress,
requestDate
) VALUES (
?,
?,
?,
?
)");
$stmt->bind_param('ssss',
$clientId,
$warehouse,
$shipmentAddress,
$requestDate
);
}
}

Related

How to display a post and its comments?

I want to view all posts, all comments for each post at once. While I have no problems with displaying posts, displaying comments is already a problem. One post can have multiple comments, so I have no idea how to create a query to display this. I tried to use LEFT JOIN but it didn't help. I include a table schema below the code to make my problem easier to understand.
<?php foreach ($tweet->userData as $user)
{ ?>
<div class='col-xl-8' id='posty'>
<div class='row' id='time'>
<div class='btn-block d-flex justify-content-between'>
<div class='imie'>
<?php echo $user['autor'] ?>
</div>
<div class='czas'>
<?php echo $user['date_to_add'] ?>
</div>
</div>
</div>
<?php echo $user['comment'] ?>
<form action='' method='post' id="myForm">
<div class='row'>
<div class='col-12 col-xl-12 d-flex justify-content-between' id='icon'>
<button class='button2' name='dodaj_like' style="background-color: <?php if($user['like_color']==1){echo '#00FA9A';}else{echo 'black';} ?>" ><i class='fas fa-heart' ></i><input type='hidden' name='like' value="<?php echo $user['id']?>" /><span id="font"><?php echo $user['likes']?></span></button>
<button class='button2' name='dodaj_dislike' style="background-color: <?php if($user['dislike_color']==1){echo '#00FA9A';}else{echo 'black';} ?>"><i class='fas fa-heart-broken'></i><input type='hidden' name='dislike' value="<?php echo $user['id']?>"/><span id="font"><?php echo $user['dislikes']?></span></button>
<button class='button2' name='dodaj_comment' id="com" ><i class='far fa-comment-dots'></i><input type='hidden' name='comment' value="<?php echo $user['id']?>"/><span id="font">Comment</span></button>
<button class='button2' name='dodaj_share' style="background-color: <?php if($user['share_color']==1){echo '#00FA9A';}else{echo 'black';} ?>" ><i class='far fa-share-square' ></i><input type='hidden' name='share' value="<?php echo $user['id']?>"/><span id="font"><?php echo $user['shares'] ?></span></button>
</div>
</div>
</form>
<div class="row d-flex">
<div class="col-xl-12 bg-success ">
<form method="post">
<textarea id="form103" class="md-textarea form-control" rows="5" placeholder="Co słychać?" name="komentarz"></textarea>
<div>
<?php echo $aabbcc ?? '' ?>
</div>
<div class="button">
<button class="btn btn-danger mt-2" name='dodaj_comment'><input type='hidden' name='com' value="<?php echo $user['id']?>"/>Publikuj</button>
</div>
</form>
</div>
</div>
</div>
<?php } ?>
$id = $_GET['id'];
$session = $_SESSION['id'];
$sql = $this->database->connect()->prepare("SELECT post.id, CONCAT(first_name,' ', last_name) AS author, post.comment, post.date_to_add, post_comment.comment, post_comment.date_to_add FROM user JOIN post ON user.id = post.user_id LEFT JOIN post_comment ON post.user_id=post_comment.post_id where post.user_id = :user_id order by post.id DESC");
$sql->bindParam(':user_id',$id, PDO::PARAM_INT);
$sql->bindParam(':id',$session, PDO::PARAM_INT);
$sql->execute();
if($sql->rowCount())
{
$this->userData = [];
while ($row = $sql->fetch())
{
$this->userData[] = $row;
}
}
}
You can get the Comment form database for specific Post using post_id by selecting Comment Table:
$query="select * form post_comment where post_id=". $post_id;
here is the PHP Implementation :
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_post = "SELECT * FROM post";
$result = $conn->query($sql_post );
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<br> post id: ". $row["id"]. " - <br> Post: ". $row["post"]<br>";
$comment_query=$conn->query("select * form post_comment where post_id=". $row["id"]);
if ($comment_query->num_rows > 0) {
// output data of each row
while($comment= $result->fetch_assoc()) {
echo " <p>Comment ".comment['comment']." </p>";
}
}
}
} else {
echo "0 results";
}
$conn->close();

How to delete related data one by one in my PHP?

How to delete related data one by one in my PHP?
functions.php
$conn = mysqli_connect("localhost:3305","root","1234","dj"); //connect database
function getRealIpUser(){
switch(true){
case(!empty($_SERVER['HTTP_X_REAL_IP'])) : return $_SERVER['HTTP_X_REAL_IP'];
case(!empty($_SERVER['HTTP_CLIENT_IP'])) : return $_SERVER['HTTP_CLIENT_IP'];
case(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) : return $_SERVER['HTTP_X_FORWARDED_FOR'];
default : return $_SERVER['REMOTE_ADDR'];
}
}
cart.php
<include ("functions.php");>
<div class="shopping-cart">
<h6>My Cart</h6>
<hr>
<?php
$ip_add = getRealIpUser(); //getRealIpuser from functions.php
$select_cart = "select * from cart where ip_add='$ip_add'";
$run_cart = mysqli_query($conn,$select_cart);
while($row_cart = mysqli_fetch_array($run_cart)){
$pro_id = $row_cart['p_id'];
$pro_size = $row_cart['size'];
$pro_quantity = $row_cart['quantity'];
?>
<form action="cart.php" method="post" class="cart-items">
<div class="border rounded">
<div class="row bg-white">
<div class="col-md-6">
<button type="submit" class="btn btn-warning">Save for Later</button>
<button type="submit" id="<?php echo $pro_id;?>" class="btn btn-danger mx-2" name="remove">Remove</button>
</div>
</div>
</div>
</form>
<?php }?>
</div>
database:
loading page:
my problem is, I want to delete related data one by one. But my
ability limited, so how to write the PHP code with MySQL.
Here are my wrong codes:
<?php
global $conn;
if(isset($_POST['remove'])){
$delete_product = "delete from cart where p_id='$pro_id'";
$run_delete = mysqli_query($conn,$delete_product);
if($run_delete){
echo "<script>window.open('cart.php','_self')</script>";
}
}
?>
you Form has to be like this with a hidden parameter, that can be identified by its name.
In this case productid
<form action="cart.php" method="post" class="cart-items">
<div class="border rounded">
<div class="row bg-white">
<div class="col-md-6">
<input type="hidden" name="productid" value=<?php echo $pro_id;?>>
<button type="submit" class="btn btn-warning">Save for Later</button>
<button type="submit" id="<?php echo $pro_id;?>" class="btn btn-danger mx-2" name="remove">Remove</button>
</div>
</div>
</div>
</form>
In you cart.php You then uset he hidden productid to delete
I changed you vulnerable code to Procedural style preprared statement.
<?php
global $conn;
if(isset($_POST['productid'])){
if ($stmt = mysqli_prepare($conn, "delete from cart where p_id=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $_POST['productid']);
mysqli_stmt_execute($stmt);
if(mysqli_affected_rows($conn) > 0 ){
echo "<script>window.open('cart.php','_self')</script>";
}else{
echo mysqli_error($connection);
}
}
}
?>
You should read urgently how to prevent sqlinjection
And you can also take a look at the Object oriented style of pho encoding

How do I approve pending records?

I am working on a video-hosting website in which user-uploaded videos (called "answers" as they will be made in response to requests for help in hobby-making) are put into a pending list for a web administrator to examine and decide whether to approve for public viewing or not.
All video records have a boolean piece of data called "approved" that is set to either true ('1') or false ('0').
What I'm having trouble with is the actual approval process. No matter what I do, I can never get to set on the video's "approved" status.
Here is the code I have written...
answer.php (Main video page)
<a class="btn btn-success btn-lg btn-block" href="<?php echo url_for('/approve.php?id=' . h(u($answer['id']))); ?>"><i class="mr-2 fas fa-check fa-2x"></i>Approve</a>
approve.php
if(!isset($_GET['id'])) {
redirect_to(url_for('/pending.php'));
}
$id = $_GET['id'];
if(is_post_request()) {
$answer = [];
$answer['id'] = $id;
$answer['approved'] = $_POST['approved'];
$result = approve_answer($id);
$_SESSION['message'] = '<div class="text-success">The answer was successfully approved and ready for public viewing.</div>';
redirect_to(url_for('/pending.php'));
} else {
$answer = find_answer_by_id($id);
$user = find_user_by_id($answer['user_id']);
}
?>
<?php $page_title = 'Confirm Approval'; ?>
<?php include(SHARED_PATH . '/public_header.php'); ?>
<div class="container my-2">
<div class="my-2">
<a class="back-link my-2" href="<?php echo url_for('/answer.php?id=' . h(u($answer['id']))); ?>"><i class="mr-2 fas fa-arrow-circle-left fa-2x"></i> Back to Answer</a>
</div>
<div class="answer approve">
<h1>Confirm Approval</h1>
<p>Are you sure you want to approve this answer?</p>
<p class="item"><strong><?php echo h($answer['title']); ?></strong> by <emphasis><?php echo h($user['username']); ?></emphasis></p>
<p><italics><?php echo h($answer['content']); ?></italics>
<form action="<?php echo url_for('/approve.php?id=' . h(u($answer['id']))); ?>" method="post">
<div id="operations">
<input type="hidden" name="approved" value="1" />
<input class="btn btn-success btn-lg btn-block" type="submit" name="approved" id="approved" value="Confirm" />
</div>
</form>
</div>
</div><!-- container -->
function approve_answer (query_functions.php)
function approve_answer($answer) {
global $db;
$errors = validate_answer($answer);
if(!empty($errors)) {
return $errors;
}
$sql = "UPDATE answers SET ";
$sql .= "approved='" . db_escape($db, '1') . "', ";
$sql .= "WHERE id='" . db_escape($db, $answer['id']) . "' ";
$sql .= "LIMIT 1";
$result = mysqli_query($db, $sql);
// For UPDATE statements, $result is true/false
if($result) {
return true;
} else {
// UPDATE failed
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
So where have I gone wrong?
Replace below code to query.
$sql = "UPDATE answers SET ";
$sql .= "approved='" . db_escape('1') . "', ";
$sql .= "WHERE id='" . db_escape($answer['id']) . "' ";
$sql .= "LIMIT 1";
if(!isset($_GET['id'])) {
redirect_to(url_for('/pending.php'));
}
if(is_post_request()) {
$result = approve_answer($_POST['id']);
$_SESSION['message'] = '<div class="text-success">The answer was successfully approved and ready for public viewing.</div>';
redirect_to(url_for('/pending.php'));
} else {
$answer = find_answer_by_id($_GET['id']);
$user = find_user_by_id($answer['user_id']);
}
?>
<?php $page_title = 'Confirm Approval'; ?>
<?php include(SHARED_PATH . '/public_header.php'); ?>
<div class="container my-2">
<div class="my-2">
<a class="back-link my-2" href="<?php echo url_for('/answer.php?id=' . h(u($answer['id']))); ?>"><i class="mr-2 fas fa-arrow-circle-left fa-2x"></i> Back to Answer</a>
</div>
<div class="answer approve">
<h1>Confirm Approval</h1>
<p>Are you sure you want to approve this answer?</p>
<p class="item"><strong><?php echo h($answer['title']); ?></strong> by <emphasis><?php echo h($user['username']); ?></emphasis></p>
<p><italics><?php echo h($answer['content']); ?></italics>
<form action="<?php echo url_for('/approve.php?id=' . h(u($answer['id']))); ?>" method="post">
<div id="operations">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input class="btn btn-success btn-lg btn-block" type="submit" name="approved" id="approved" value="Confirm" />
</div>
</form>
</div>
</div><!-- container -->
Query
$sql = "UPDATE answers SET ";
$sql .= "approved=1";
$sql .= "WHERE id='" . db_escape($db, $answer) . "' ";
You can try this code. I am still not sure where you are setting the value for $answer['user_id'] ? But this should solve your issue.

Show all equal values from table inside same div

This code gets all values from a table and for each row it shows its details inside a alert div and i can click a "order ready button" for that single product.
What I need to do is put in a single div all the products that are from the same order, and for that I'm thinking about using all the rows that have the same date value and when this value changes create a new div.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM kitchen");
while ($row = mysqli_fetch_array($result)) {
$table = $row['table'];
$customer = $row['customer'];
$product = $row['product_name'];
$code = $row['product_code'];
$size = $row['size'];
$id = $row['id'];
$date = $row['date'];
// It would have to open here in each first distinct $date
echo '<div class="alert alert-info" role="alert" id="'.$code.'">';
echo '<h4>'.'Table '.$table.'</h4>';
echo '<h4>'.'Name: '.$name.'</h4>';
// Repeat this for each equal $date value
if($code=="A01"||$code=="A02"||$code=="A03"||$code=="A04"){
echo '<h4>'.$code.' - '.$product.' ('.$size.')'.'</h4>';
}
else{
echo '<h4>'.$code.' - '.$product.'</h4>';
}
// Close here before each next distinct $date
echo '<form action="actionkitchen.php" method="post">';
echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data' type='submit'>Order Ready</button>";
echo '</form>';
echo '</div>';
}
?>
This is what I ended up with, not the most elegant solution but it's working.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM kitchen");
while ($row = mysqli_fetch_array($result)) {
$table[] = $row['table'];
$name[] = $row['name'];
$product[] = $row['product_name'];
$code[] = $row['product_code'];
$size[] = $row['size'];
$date[] = $row['date'];
}
$count = array_count_values($date);
$y = 0;
foreach ($count as $item){
for($i=0;$i<$item;$i++){
if($i==0){
echo '<div class="alert alert-info">';
echo '<h4>'.'Table '.$table[$y].'</h4>';
echo '<h4>'.'Name: '.$name[$y].'</h4>';
}
if($code[$y]=="A01"||$code[$y]=="A02"||$code[$y]=="A03"||$code[$y]=="A04"){
echo '<h4>'.$code[$y].' - '.$product[$y].' ('.$size[$y].')'.'</h4>';
}
else{
echo '<h4>'.$code[$y].' - '.$product[$y].'</h4>';
}
if($i==$item-1){
echo '<form action="actionkitchen.php" method="post">';
echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data[$y]' type='submit'>Order Ready</button>";
echo '</form>';
echo '</div>';
}
$y++;
}
}
?>
To set your products in the same order, I would group them by the key in an array. For our purposes, we'll use a multidimensional array so that we can add our products within the unique key (using "date" in the example). Below you will see me set the array, fetch the rows from the database (sorting by our group key so that we have some consistency on the front end) and begin placing them in their unique groups. When pushing a product into the date array, I am using array_merge() in combination of in_array() and a ternary operator to set the "product string" within the HTML.
<?php
/* Fetch/Set Kitchen */
$kitchen = array();
$sql = "SELECT * FROM `kitchen` ORDER BY `date`";
$query = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($query)) {
$kitchen[$row['date']][] = array_merge($row, array(
'product_string' => (in_array($row['product_code'], array('A01', 'A02', 'A03', 'A04')) !== FALSE)
? $row['product_code'] . ' - ' . $row['product_name'] . ' (' . $row['size'] . ')'
: $row['product_code'] . ' - ' . $row['product_name']
));
}
?>
To keep our HTML tidy and readable apart from our PHP, you'll see that I've chosen to use an alternative syntax for the control structures. This helps by using tab indentations from having put any awkwardly placed curly brackets in our code.
<?php foreach($kitchen as $date => $items): ?>
<div class="alert alert-info" role="alert" id="<?php echo $date; ?>">
<?php foreach($items as $item): ?>
<h4>Table <?php echo $item['table']; ?></h4>
<h4>Name: <?php echo $item['customer']; ?></h4>
<h4><?php echo $item['product_string']; ?></h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="<?php echo $item['data']; ?>" type="submit">Order Ready</button>
</form>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The above reference code will output HTML similar to:
<div class="alert alert-info" role="alert" id="2016-10-21">
<h4>Table Table 1</h4>
<h4>Name: Name 1</h4>
<h4>XXX1 - Product 1 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX1" type="submit">Order Ready</button>
</form>
<h4>Table Table 2</h4>
<h4>Name: Name 2</h4>
<h4>XXX2 - Product 2</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX2" type="submit">Order Ready</button>
</form>
<h4>Table Table 3</h4>
<h4>Name: Name 3</h4>
<h4>XXX3 - Product 3 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX3" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-10-27">
<h4>Table Table 4</h4>
<h4>Name: Name 4</h4>
<h4>XXX4 - Product 4</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX4" type="submit">Order Ready</button>
</form>
<h4>Table Table 5</h4>
<h4>Name: Name 5</h4>
<h4>XXX5 - Product 5 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX5" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-11-06">
...etc.

UPDATE table and show ticked checkboxes

So I have the ability to submit a form which allows me to set which category my post belongs to which works great - yet if I want to edit the categories it belongs to the update is not changing the join table? I also am looking on how to show which categories are checked when you go in to update the document and pull down which categories the post belongs to.
Here is the form to update:
<?php
require_once '../../db_con.php';
if(!empty($_GET['doc_id'])){
$doc = intval($_GET['doc_id']);
try{
$results = $dbh->prepare('SELECT * FROM doc_list WHERE doc_id = ?');
$results->bindParam(1, $doc);
$results->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$doc = $results->fetch(PDO::FETCH_ASSOC);
if($doc == FALSE){
echo '<div class="container">';
echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
echo 'Get me outta here!';
echo'</div>';
die();
}
}
?>
<div class="container">
<br>
<i class="fa fa-angle-double-left"></i> Previous page
<br>
<h3 class="subTitle">
<i class="fa fa-pencil"></i></span> Edit Document
</h3>
<?php
if(isset($doc)){
?>
<form action="actions/update_doc.php" method="POST" id="rtf" name="">
<input type="hidden" value="<?php echo $doc['doc_id'] ?>" name="doc_id" />
<input type="text" value="<?php echo $doc['doc_title'] ?>" name="doc_title" required />
<br />
<?php
try{
// Selecting entire row from cat_list table
$results = $dbh->query("SELECT cat_id, cat_title FROM cat_list");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$category = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<br>
<label><input type="checkbox" name="" class="selectall"/> Select all</label>
<div id="checkboxlist" >
<?php
foreach($category as $cat){
?>
<input type="checkbox" value="<?php echo $cat["cat_id"]; ?>" name="cat_no[]" id="box1"> <?php echo $cat["cat_title"]; ?></a><br>
<?php
}
?>
</div>
<br><br>
<button class="postEditBtn" type="button" onclick="ibold()" title="Bold Text"><i class="fa fa-bold"></i></button>
<button class="postEditBtn" type="button" onclick="iitalic()" title="Italic Text"><i class="fa fa-italic"></i></button>
<button class="postEditBtn" type="button" onclick="iunderline()" title="Underline Text"><i class="fa fa-underline"></i></button>
<button class="postEditBtn" type="button" onclick="ifontName()" title="Font Family"><i class="fa fa-font"></i></button>
<button class="postEditBtn" type="button" onclick="ifontsize()" title="Font Size"><i class="fa fa-text-height"></i></button>
<button class="postEditBtn" type="button" onclick="ifontcolor()" title="Font Colour"><i class="fa fa-eraser"></i></button>
<button class="postEditBtn" type="button" onclick="ihiliteColor()" title="Highlight Text"><i class="fa fa-magic"></i></button>
<button class="postEditBtn" type="button" onclick="ilink()" title="Add/Edit Link"><i class="fa fa-link"></i></button>
<button class="postEditBtn" type="button" onclick="iunlink()" title="Remove Link"><i class="fa fa-chain-broken"></i></button>
<button class="postEditBtn" type="button" onclick="ijustifyLeft()" title="Text align-left"><i class="fa fa-align-left"></i></button>
<button class="postEditBtn" type="button" onclick="ijustifyCenter()" title="Text align-center"><i class="fa fa-align-center"></i></button>
<button class="postEditBtn" type="button" onclick="ijustifyRight()" title="Text align-right"><i class="fa fa-align-right"></i></button>
<button class="postEditBtn" type="button" onClick="iUnorderedList()" title="Unordered List"><i class="fa fa-list-ul"></i></button>
<button class="postEditBtn" type="button" onClick="iOrderedList()" title="Ordered List"><i class="fa fa-list-ol"></i></button>
<button class="postEditBtnUndo" type="button" onClick="iUndo()" title="Undo last change"><i class="fa fa-rotate-left"></i></button>
<button class="postEditBtnRedo" type="button" onClick="iRedo()" title="Redo last change"><i class="fa fa-rotate-right"></i></button>
<br><br>
<textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
<iframe name="editor" id="editor" style="width:100%; height: 600px;"></iframe>
<br />
<input onclick="formsubmit()" type="submit" value="Update Document" name="submit"/>
</form>
Here is my update script:
<?php
/******************************************************************
** ACTION SCRIPT TO UPDATE THE DOCUMENT AFTER CHANGES ARE MADE **
******************************************************************/
if(isset($_POST["submit"])){
include_once'../../config.php';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=dashboardr",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "UPDATE doc_list SET doc_title = :doc_title, doc_content = :doc_content, doc_updated=CURRENT_TIMESTAMP WHERE doc_id = :doc_id";
$query = $dbh->prepare($sql);
$query->execute(array(":doc_title"=>$_POST["doc_title"],":doc_content"=>$_POST["doc_content"], ":doc_id"=> $_POST["doc_id"]));
if ($query) {
header ('Location: ../list_doc.php?success=2');
}
/***********************************************************************
** INSERTS THE ARRAY DEPENDING ON WHICH WAS CHECKED WITHIN THE FORM **
***********************************************************************/
$sql = "UPDATE `cat_doc_link_table`(`link_cat_id`, `link_doc_id`) VALUES";
$values = "";
$params = [];
foreach($_POST["cat_no"] as $cat)
{
$values.= "(?, ?), ";
$params[] = $cat; // correct here
$params[] = $docId;
}
$values = substr($values, 0, -2);
$sql.= $values;
$query = $dbh->prepare($sql);
$query->execute($params);
if ($dbh->query($sql)) {
}else{}
$dbh = null;
}catch(PDOException $e)
{
header ('Location: ../list_doc.php?success=2');
}
}
?>
So I have two three tables, one called doc_list (for the posts) cat_list which handles the categories and a joint able which takes the id from both the doc_list and cat_list which marries up the two tables.
As mentioned above, in the update doc form (the first script) a way to pull in and tick the checkboxes from which category they are associated too. the main issue I have is updating the tables when you go in and want to update.

Categories