Update 1 column of all rows in the database table - php

Hopefully someone can help me with this "easy" question.
I have data in a table that has been gathered from a mysql database. I want the user to be able to edit the "PRICE" column to any and all rows, then press the update button which will send the data to the table and update all the rows in the "PRICE" column.
For the life of me I can't get the database to update. I know it has to be something that I'm missing. And it has to be something so easy it's laughable.
Please help?
if (isset($_POST['update']))
{
echo '<pre>';
print_r($_POST);
echo '</pre>';
if (is_array($ID))
{
foreach($_POST['hidden'] AS $ID)
{
echo "ID is: " . $ID . "</br>";
echo "Price is: " . $pricing . "</br>";
$ID = mysqli_real_escape_string($conn, $_POST['hidden'][$ID]);
$pricing = mysqli_real_escape_string($conn, $_POST['price'][$ID]);
$updateQuery = 'UPDATE `bathroom_price` SET `price` ="' . $pricing . '" WHERE `ID`=' . $ID;
mysqli_query($conn, $updateQuery) or die(mysql_error());
}
}
}
?>
</head>
<?php
mysqli_select_db($conn, "table_name");
?>
<div class="row center-xs">
<div style="margin-top:100px;" class="col-xs-12 col-sm-12 col-md-12 col-lg-8">
<div class="box">
<form method=POST>
<h1>Price List for Bathroom Form</h1>
<table>
<?
$secondSQL = "SELECT question, question_ID, ID, form_ID, form_name FROM bathroom_price GROUP BY question_ID, form_name ORDER BY ID ";
$result1 = mysqli_query($conn, $secondSQL);
while ($row = mysqli_fetch_assoc($result1))
{
$question_ID = $row['question_ID'];
$question = $row['question'];
$formID = $row['form_ID'];
$form_name = $row['form_name'];
?>
<input type=hidden value="<? echo $question_ID ?>">
<tr class='questionHeading'>
<td colspan='3'>
<h2><? echo $question ?></h2>
<h3>Questions for the <? echo $form_name ?> Form</h3></td>
</tr>
<tr>
<th>Options:</th>
<th>Price:</th>
<th>Update:</th>
</tr>
<?
$thirdSQL = "SELECT question_ID, options, price, ID FROM bathroom_price WHERE question_ID = $question_ID";
$replies = mysqli_query($conn, $thirdSQL);
while ($rows = mysqli_fetch_assoc($replies))
{
$price = $rows['price'];
$options = $rows['options'];
$ID = $rows['ID'];
?>
<input type=hidden name="hidden[]<?echo $ID ?>" value="<?echo $ID ?>" />
<input type=hidden value="<? echo $question_ID ?> " />
<tr>
<td style='width:60%;'>
<input readonly type=text value="<? echo $options ?>">
</td>
<td style='width:10%;'>
<input type=text name="price[]<?echo $ID ?>" value="<?echo $price ?>">
</td>
</tr>
<?}?>
<?}?>
</table>
<div class="start-xs" style="margin: 0 0 50px 0;">
<button type=submit name="update" class="admin-style" value="Update Price Form">
<i class="fa fa-save"></i> Update Price Form
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<?php mysqli_close($conn);?>

Maybe this is a problem:
<input type=hidden name="hidden[]<?echo $ID ?>" value="<?echo $ID ?>" />
Your input name is "hidden[]1" etc.

Related

Php doesn't catch results for the content but only for the title

So i have a code of a CRUD form where i can publish topics with titles and edit/update and exclude, everything is working , the only problem im having is with the update of the topic content, in which when i click to change an already published topic, it only displays the topic title, but not the content, it still works though if i add another content and update, it will normally change the previously published subject for the one i added, i've been trying to find an answer but its difficult since im still just a beginner with php, most of the code was taken from the internet which i change the somethings to fit my idea.
This is the update php code.
<?php include('connect.php');
// get values to update
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$rec = mysqli_query($db,"SELECT * FROM news WHERE id=$id");
$record = mysqli_fetch_array($rec);
$title = $record['title'];
$content = $record['content'];
$id = $record['id'];
}
?>
The html
<form method="post" action="connect.php" >
<div class="input-group">
<input type="hidden" name="id" value="<?php echo $id; ?>">
</div>
<div class="input-group">
<input type="text" name="title" placeholder="Title" value="<?php echo $title; ?>">
</div>
<div class="input-group">
<textarea name="content" placeholder="Content" value="<?php echo $content; ?>"></textarea>
</div>
<!-- form buttons -->
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update">Update</button>
<button class="btn" type="button" value="cancel" onclick="history.back();">Cancel</button>
<?php else: ?>
<button class="btn" type="submit" name="send">Send</button>
<?php endif ?>
</div>
</form>
<table>
<div class="form">
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr style="border:0;font-size:30px;";>
<td><b><?php echo $row['title']; ?></b></td>
</tr>
<tr style="border-radius: 5px;background-color: #ebebeb;">
<td><?php echo $row['content']; ?></td>
</tr>
<!-- Delete/edit buttons -->
<tr style="border:0;">
<td>
<a href="news.php?edit=<?php echo $row['id']; ?>" class="fix_btn" >Edit</a>
<a style="float:right;" href="news.php?del=<?php echo $row['id']; ?>" class="fix_btn"
onclick="return confirm('Are you sure you want to delete this topic?')">Delete</a>
</td>
</tr>
<tr style="height:60px; border:0;"><td></td></tr>
<?php } ?>
</div>
</table>
database connection (connect.php)
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'orbita');
// initialize variables
$title = "";
$content = "";
$id = 0;
$update = false;
if (isset($_POST['send'])) {
$title = $_POST['title'];
$contet = $_POST['content'];
mysqli_query($db, "INSERT INTO news (title, content) VALUES ('$title', '$content')");
$_SESSION['message'] = "Success!";
header('location: news.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
mysqli_query($db, "UPDATE news SET title ='$title', content ='$content' WHERE id=$id");
$_SESSION['message'] = "Success update!";
header('location: news.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM news WHERE id=$id");
$_SESSION['message'] = "Message deleted";
header('location: news.php');
}
?>
textarea does not have a value attribute.
Try changing it to this and see if it works
<div class="input-group">
<textarea name="content" placeholder="Content" >
<?php echo $content; ?>
</textarea>
</div>
You cannot use attribute for textarea elements
replace
<textarea name="content" placeholder="Content" value="<?php echo $content; ?>"></textarea>
with
<textarea name="content" placeholder="Content"><?php echo $content; ?></textarea>

Dropdown Menu Selection to Populate Form

Hi all,
I am trying to populate a form from MySQL using a dropdown menu to select the row I want displayed.
The dropdown is displaying the items I need. But what I want to do is select one of the items in the dropdown, which will fill in the form with the items I need for editing.
Here is the code I have so far, including the form. Any help you guys can give would be really appreciated.
The Select:
<div class="panel panel-primary">
<div class="panel-heading"> Manage Tours</div>
<div class="panel-body">
<form role="form" method="post" action="">
<label for="singleSelect">Choose Tour to Edit</label>
<select class="form-control" name="tour_select">
<?php
$result = mysqli_query($mysqli,"SELECT tour_name FROM tours order by id");
while($row = mysqli_fetch_array($result))
echo "<option value='" . $row['tour_name'] . "'>" . $row['tour_name'] . "</option>";
?>
</select>
This works fine.
The form, which also works fine with a simple select query:
<div class="form-group">
<label>Tour Name</label>
<input class="form-control" name="tour_name" value="<?php echo $row['tour_name']; ?>" />
</div>
<div class="form-group">
<label>Destination</label>
<textarea class="form-control" name="tour_to" rows="4"><?php echo $row['tour_to']; ?></textarea>
</div>
<div class="form-group">
<label>Collection</label>
<textarea class="form-control" name="tour_from" rows="4"><?php echo $row['tour_from']; ?></textarea>
</div>
<div class="form-group">
<label>Date</label>
<input class="form-control" name="tour_date" value="<?php echo $row['tour_date']; ?>" />
</div>
<div class="form-group">
<label>Pickup Time</label>
<input class="form-control" name="tour_time" value="<?php echo $row['tour_time']; ?>" />
</div>
<div class="form-group">
<label>Itinerary</label>
<textarea class="tinymce" id="tinymce" name="tour_details" rows="12" ><?php echo $row['tour_details']; ?></textarea>
</div>
<button type="submit" name="Submit" class="btn btn-primary">Save Changes</button>
</form>
But, when I select an option from the dropdown, I need the form to be populated with the data from that database table row. I hope I am making sense.
I have searched for days on Google but found nothing.
I have changed the form from a dropdown to an html table with a Delete button.
Here is the code with table:
<?php
if (isset($_GET['id'])) {
if (isset($_POST['Delete'])) {
$remove = $mysqli->prepare("DELETE FROM `tours` WHERE `id` = $id");
$id = $_POST['id'];
$remove->bind_param('ssssss', $id);
if(!$remove->execute() === true) {
echo $mysqli->error;
}
}
}
$sql = "SELECT * FROM tours";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['tour_name']; ?></td>
<td><?php echo $row['tour_to']; ?></td>
<td><?php echo $row['tour_from']; ?></td>
<td><?php echo $row['tour_date']; ?></td>
<td><?php echo $row['tour_time']; ?></td>
<td><input type="submit" name="Delete" value="Delete" onclick="" /></td>
</tr>
</tbody>
<?php
}
?>
</table>
</form>
I appreciate any help.
Say you want to preselect a particular tour with $tour_id = 5. You can then modify your code to:
echo "<option value='" . $row['tour_name'] . "'" . ($row['id'] == $tour_id ? " selected" : "") . ">" . $row['tour_name'] . "</option>";
I have found the solution should anyone need it in future.
<?php
if (isset($_POST['Delete'])){
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i=0;$i<$count;$i++){
if(!empty($checkbox[$i])){ /* CHECK IF CHECKBOX IS CLICKED OR NOT */
$id = mysqli_real_escape_string($mysqli,$checkbox[$i]); /* ESCAPE STRINGS */
mysqli_query($mysqli,"DELETE FROM tours WHERE id = '$id'"); /* EXECUTE QUERY AND USE ' ' (apostrophe) IN YOUR VARIABLE */
} /* END OF IF NOT EMPTY CHECKBOX */
} /* END OF FOR LOOP */
} /* END OF ISSET DELETE */
$sql = "SELECT * FROM tours";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = mysqli_real_escape_string($mysqli, $row['id']);
?>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['tour_name']; ?></td>
<td><?php echo $row['tour_to']; ?></td>
<td><?php echo $row['tour_from']; ?></td>
<td><?php echo $row['tour_date']; ?></td>
<td><?php echo $row['tour_time']; ?></td>
<?php echo "<td><input type='checkbox' name='checkbox[]' value='$id'></td>"; ?>
</tr>
</tbody>
<?php
}
?>
Thanks all for the help. Really appreciate it.

Updating all rows at once in codeigniter, error in query

I am trying to update multiple rows simultaneously and I think my query is somewhat messed up while i am trying to update all my rows simultaneously. I am using CodeIgniter framework. I want to work on my query and the data being sent from the view but unable to get a logic to update all rows simultaneously. Please note that there should be only one update button as this is a requirement
My View code is:
<form action="" method="post">
<table border="1" style="background:none;width:100%;" RULES="ROWS" class="tab_data">
<thead>
<th width="30px">No</th>
<th >Action Item</th>
<th>Responsibility</th>
<th>Order</th>
<th>Mandatory?</th>
<th width = "100px" align="center">Actions</th>
</thead>
<tbody>
<?php
$serial_no = 1;
if(count($rows))
{
foreach($rows as $row)
{
?>
<tr>
<td><?php echo $serial_no; ?></td>
<td>
<?php
echo "<input type='hidden' class='col-md-4 form-control' name='checklist_id' value='"
.$row['checklist_id']."' />";
echo $row['action_item'];
?>
</td>
<td>
<?php echo $row['responsibility']; ?>
</td>
<td>
<input type="hidden" name="row_id[]" value="<?php echo $row['sequence']; ?>">
<input type="text" class="form-control" name="order[]" value="<?php echo $row['sequence']; ?>">
</td>
<td>
<input type="checkbox" class="" name="if_checklist<?php echo $row->checklist_id; ?>" value="1" echo 'checked'; >
</td>
<td align="center">
<?php
echo anchor('exits/delete_absconding_checklist/'.$row['checklist_id'],
"<i class='fa fa-trash-o' alt='Delete' title='Delete' rel='".$row['id']."' ></i>",
array('rel' => $row->id, 'class' => 'edit_row'));
?>
</td>
</tr>
<?php
$serial_no++;
}
}
?>
<tr>
<td></td><td></td><td></td><td></td>
<td>
<input type="hidden" name="action" value="update_order">
<button type="submit" name="submit" class="btn btn-info pull-right">Update</button>
</td>
</tr>
</tbody>
</table>
</form>
My Controller Code is:
function display_absconding_checklist()
{
if($this->input->post('action') == '_doDelete' || $this->input->post('action') == '_doChangeStatus')
{
$this->admin_init_elements->delete_rows('applicant_status');
}
if($this->input->post('action') == 'update_order')
{
$this->exit_common->update_absconding_checklist();
}
$data['rows'] = $this->exit_common->get_all_absconding_checklists();
$this->data['maincontent'] = $this->load->view('maincontents/backend_display_absconding_checklist', $data, true);
$this->load->view('layout', $this->data);
}
My Model code is:
function update_absconding_checklist($post_array)
{
$post_array = $this->input->post();
foreach($post_array['checklist_id'] as $key => $rowid)
{
if($rowid > 0)
{
if(isset($post_array['if_checklist'.$rowid]))
$in = '1';
else
$in = '0';
$sql1 = "UPDATE pr_absconding_checklists SET `action_item` = '"
.$post_array['action_item'][$key]
."', `sequence` = '".$post_array['sequence'][$key]
."', `if_checklist` = '".$in
."' WHERE `checklist_id` = ".$rowid;
}
else
{
$sql1 = "UPDATE pr_absconding_checklists SET `action_item` = '"
.$post_array['action_item'][$key]
."', `sequence` = '".$post_array['sequence'][$key]
."', `if_checklist` = '".$in."'";
}
$query1 = $this->db->query($sql1);
}
}
I am getting no errors but there are many errors in my code and i am messed up, i am attaching my table snapshot also, please recommend improvements
My Table name is pr_absconding_checklists

when opening accordion, GET url by ID from that article

I want to display the article ID in my URL when i press the a href in code beneath
Update, i included the php section and the while loop with the article ID
require_once("inc/connection.php");
mysql_select_db("nieuws");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_GET['id'];
$datum = $_POST["datum"];
$titel = $_POST["titel"];
$artikel = $_POST["artikel"];
$checkbox = $_POST["checkbox"];
$titel = mysql_real_escape_string(nl2br(htmlentities($_POST["titel"])));
$artikel = mysql_real_escape_string($_POST["artikel"]);
$id = mysql_real_escape_string($_POST['id']);
date_default_timezone_set('GMT');
$datum = date('Y-m-d', strtotime(str_replace('-', '/', $datum)));
if(isset($_POST['add'])){
if(!empty($_POST['titel']) && !empty($_POST['artikel']) && !empty($_POST['datum'])){
$query="INSERT INTO nieuws (id,datum,titel,artikel) VALUES ('$id','$datum','$titel','$artikel')";
$datum = date('Y-m-d', strtotime(str_replace('-', '/', $datum)));
str_replace('<br />', "\n", $textarea);
$result=mysql_query($query);
$juist1 = true;
}else{
$fout1 = true;
}
}if(isset($_POST['delete'])){
foreach($_POST['checkbox'] as $del_id){
$sql="DELETE FROM nieuws WHERE id='$del_id'";
$result = mysql_query($sql);
$juist2 = true;
}
}
}
This is the accordion script that opens on click
$('.acc_container').hide(); //Hide/close all containers
$('.acc_trigger').click(function(){
if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
$('.acc_trigger').removeClass('active').next().slideUp();
$(this).toggleClass('active').next().slideDown();
}
return false;
});
});
This is the FORM part
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<table style="width:950px; margin-bottom:5px;">
<tr>
<td>Datum:<br />
<input type="text" name="datum" size="20" style="width:100px;" value="<?php echo $datum; ?>" id="datepicker" placeholder="Kies datum"/><br /></td>
<td>Titel:<br />
<input type="text" name="titel" size="200" style="width:500px;" maxlength="45" value="<? php echo $titel; ?>" placeholder="Max. 50 characters toegelaten"/><br /></td> </tr>
</table>
Artikel: <br />
<textarea id="textarea" name="artikel" style="width:500px; height:150px;" value="<?php echo $artikel; ?>" ></textarea><br />
<input type="submit" name="add" value="Artikel toevoegen" />
</form>
This is the while loop where i ADD the row ID
$query="SELECT id,datum,titel,artikel FROM nieuws ORDER BY id DESC";
$result=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo ("<div id=\"artikeltitel\" align=\"center\">
<div id=\"containerdatum\">".$row['datum']."</div>
<div id=\"containertitel\">".$row['titel']."</div>
<div id=\"container3\" style=\"font-size:12px;\">".$row['id']."
<input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox\" value=\"".$row['id']."\" />
</div>
</div>
<div class=\"container\" align=\"center\">
<h2 class=\"acc_trigger\"> ยป </h2>
<div class=\"acc_container\">
<div class=\"block\">".$row['artikel']."</div>
<div class=\"fb-comments\" data-href=\"http://www.zpb-polonez.be/user.php\" data-num- posts=\"10\" data-width=\"678\" style=\"margin-top:2px;\"></div>
</div>
</div>
");
}
I don't know what to add more to explain what i'm trying to achive
We lack some information here to be able to help but let's try anyway.
Let's assume that your article id is $row['id'] you could build your link this way:
echo 'Link';

PHP PDO Update with 2 tables

I have a little problem. This is what I want to achive:
I have 2 mysql tables (categories, channels), the channel table has a cat_id in it. I want to update/edit a product and place it in another category but the code that I've made shows just one category (id=1) even if the product has a parent id(cat_id) of 5.
try {
//prepare query
$query = "select channel_id, name, category_id from channels where channel_id = ? limit 0,1";
$stmt = $pdo->prepare( $query );
//this is the first question mark
$stmt->bindParam(1, $_REQUEST['id']);
//execute our query
$stmt->execute();
//store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//values to fill up our form
$channel_id = $row['channel_id'];
$name = $row['name'];
$category_id = $row['category_id'];
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
$query2 = "SELECT * FROM categories";
$stmt2 = $pdo->prepare( $query2 );
$stmt2->execute();
$results = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Channel Name</td>
<td><input type='text' name='name' value='<?php echo $name; ?>' /></td>
</tr>
<tr>
<td>Category</td>
<td>
<?php foreach($results as $rows) {?>
<select name="fileselect">
<option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
<!-- <input type='text' name='category_id' value='<?php //echo $category_id; ?>' /> -->
<?php } ?>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='channel_id' value='<?php echo $channel_id ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</td>
</tr>
</table>
</form>
Instead of
<?php foreach($results as $rows) {?>
<select name="fileselect">
<option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
<!-- <input type='text' name='category_id' value='<?php //echo $category_id; ?>' /> -->
<?php } ?>
</select>
Try:
<select name="fileselect">
<?php foreach($results as $rows) {?>
<option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
<!-- <input type='text' name='category_id' value='<?php //echo $category_id; ?>' /> -->
<?php } ?>
</select>

Categories