I want to edit mysql data using a contact form. When the user clicks edit on the specific post they want to change, it takes them to a form that has the post values in the input fields, and they can change whatever they want about the post. I already have this part working but now I need the change to actually take effect once the user clicks submit. How can I accomplish this?
Here is the PHP that displays all the text from the blog post in mysql into the input fields when the user clicks edit.
<?php
include "../php/db_connect.php";
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM articles WHERE id='$id'");
while($row = mysql_fetch_array($sql)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$written_by = $row['written_by'];
$category = $row['category'];
}
?>
<div class="content_wrapper">
<h1 class="content_heading">Edit A Blog Post</h1>
<form method="post" action="php/edit_article_process.php" enctype="multipart/form-data" id="upload_article_form">
<span>Title</span>
<input type="text" name="title" class="input" value="<?php echo $title; ?>"/>
<span>Article</span>
<textarea rows="20" name="ckeditor1" class="input" style="resize:none;"><?php echo $content ?></textarea>
<span>Author</span>
<input type="text" name="written_by" class="input short_input" value="<?php echo $written_by; ?>"/>
<span>Category - <i>Spell it right nigga</i></li></span>
<input type="text" name="category" class="input short_input" value="<?php echo $category?>"/>
<input type="submit" name="update" class="input submit_button"/>
</form>
<?php } ?>
</div>
Now all I need is for the change to take effect when the user clicks submit.
Your form attribute method has a value of post which is not the same method you used inside the isset() function
You should use:
$_GET['id'] //if your method="get"
and
$_POST['id'] //if your method="post"
if you want the change to appear directly you should re-fresh the page after editing the values
header('Location: www.samepage.php');
Related
I'm trying to update a category name with no luck...
on the functions.php i have a function to show all categories:
function get_all_categories(){
global $connection;
$query = 'SELECT * FROM categories';
$select_categories = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<tr>";
echo "<td>$cat_id</td>";
echo "<td>$cat_title</td>";
?>
<form method="post">
<input type="hidden" name="delete_cat_id" value="<?php echo $cat_id; ?>">
<?php
echo '<td class="text-center"><input class="btn btn-danger" type="submit" value="Delete" name="delete"></td>';
?>
</form>
<form method="post">
<input type="hidden" name="update_category_id" value="<?php echo $cat_id; ?>">
<?php
echo '<td class="text-center"><input class="btn btn-info" type="submit" value="Update" name="update_category"></td>';
?>
</form>
<?php
// echo "<td class='text-center'><a class='btn btn-info' href='categories.php?update=$cat_id'>Update</a></td>";
echo "</tr>";
}
}
the function is shown in the file categories.php:
<form action="" method="post">
<div class="form-group">
<label for="my_new_cat">Add a new category
<input class="form-control" type="text" name="my_new_cat">
</label>
</div>
<input type="hidden" name="category_id" value="<?php echo $cat_id; ?>">
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Add Category">
</div>
</form>
<?php
if (isset($_POST['update_category'])) {
include "includes/update_categories.php";
}
?>
my code in the update_categories.php is:
<?php
$selected_cat_id = $_POST['update_category_id'];
$query = "SELECT * FROM categories WHERE cat_id = $selected_cat_id";
$select_edit_categories = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_edit_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
}
?>
<form method="post">
<div class="form-group">
<label for="updated_cat_name">Update category</label>
<input class="form-control" type="text" name="updated_cat_name" value="<?php echo $cat_title; ?>">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="update_cat_title" value="Update Category">
</div>
</form>
<?php
if(isset($_POST['update_cat_title'])){
$updated_cat_title = $_POST['updated_cat_name'];
$edited_category_id = $_POST['category_id'];
$update_cat_query = "UPDATE categories SET cat_title = '$updated_cat_title' WHERE cat_id = $edited_category_id ";
$update_query = mysqli_query($connection,$update_cat_query);
checkQuery($update_query);
}
?>
there is a button to update the category, the category name is getting into the input field in the code above.
when I click the UPDATE button a new field is revealed via include "includes/update_categories.php"; containing the category name inside it.
when I change the name in the input field and click "Update Category" under the new field of category name I get nothing and nothing changes in the DB.
also, no errors presented...
any ideas?
thank you!
$_POST['update_category_id'] is accessed first thing in your code but there is no input for update_category_id.
Did you missed to set a hidden input for update_category_id in the form?
You may edit your code to add hidden input around
<input class="form-control" type="text" name="updated_cat_name" ...
<input type="hidden" name="update_category_id"
value=<?php echo $selected_cat_id; ?> />
Edit :
form containing update_category_id in "functions.php" should have action="categories.php".
where is update_category set in "categories.php" that is being checked in if (isset($_POST['update_category'])) ?
Can you confirm if the file "includes/update_categories.php" is being included ?
The hidden input is still missing in form in file "includes/update_categories.php"
<input type="hidden" name="update_category_id" ... in "functions.php" just passed to "categories.php" to be able to display information of selected "update_category_id", where as <input type="hidden" name="update_category_id" ... is required save the changes made in form in file "includes/update_categories.php".
OK
After a long searching for the answer including the wonderful people here trying to help, I have figured it out.
The problem was in this code:
if (isset($_POST['update_category'])) {
include "includes/update_categories.php";
}
The problem was that when i clicked the "Update" button created in the code above, it included the query for getting the value from the SQL field AND the query to update the value in the SQL.
The thing is when i click the "Update" button again in the code above, the POST is changing to if(isset($_POST['update_cat_title'])) instead.
The result is that the if (isset($_POST['update_category'])) POST in no longer avaliable, which causes the include of the form to be removed.
Therefore, the UPDATE query is no longer in this page and the results are not sending.
So i put this:
if(isset($_POST['update_cat_title'])){
$newCatName = $_POST['updated_cat_name'];
$catId = $_POST['category_id'];
update_category($newCatName, $catId);
}
(could have put all the function in the same file but decided to move it to the "functions.php" file instead).
So i created this function:
function update_category($newCatName, $catId){
global $connection;
$updated_cat_title = escape($newCatName);
$edited_category_id = escape($catId);
$update_cat_query = "UPDATE categories SET cat_title = '$updated_cat_title' WHERE cat_id = $edited_category_id ";
$update_query = mysqli_query($connection,$update_cat_query);
checkQuery($update_query);
header("Location: categories.php");
}
Now everything works great,
Thanks All!
new to PHP here
I have a page that loops through a DB and displays the contents of the table.
When the delete button is pressed I would like to delete that entry in the DB.
Right now when the button is pressed and the page goes to delete.php I keep getting Undefined index: userid
Here is the first page:
<?php
foreach($rows as $row){
$userid = $row['userid'];
$pName = $row['pName'];
$pDesc = $row['pDesc'];
$dDate = $row['dDate'];
?>
<div class="project-container">
<label>Project ID:</label>
<span><?php echo $userid; ?></span><br>
<label>Project Owner:</label>
<span><?php echo $pName; ?></span><br>
<label>Project Description:</label>
<span><?php echo $pDesc; ?> </span><br>
<label>Project Due Date:</label>
<span><?php echo $dDate; ?> </span><br>
<br>
<form action="#" method="GET">
<input type="submit" name="delete" value="Delete Project">
</form>
<form action="index.php">
<input type="submit" name="update" value="Update Project">
</form>
</div>
<br>
<br>
</div><br><br><?php } ?>
and this is delete.php:
include('connect.php');
$userid = $_GET['userid'];
echo $userid;
$sql = "DELETE FROM projecttable WHERE userid = '$userid'";
$conn->exec($sql);
Any help is appreciated, thank you
Try this. It will send a variable in the GET array named userid with the userid value to delete.php:
<form action="delete.php" method="GET">
<input type="hidden" name="userid" value="<?php echo $userid; ?>">
<input type="submit" name="delete" value="Delete Project">
</form>
The action attribute in the form tag will direct where to sent the request which is the delete.php script and the method will tell it to use the GET array which basically puts the keys and values in the URL itself. You could alternately do this by modifying the action field instead of using an input field. I just like this way for readability.
Because input fields are usually visible, using a hidden field prevents it from being displayed on the web page, but it's easy to manage in the code. The name attribute of the input field determines the name of the key in the GET array and the value attribute determined the value of that element in the GET array. So it ends up being $_GET['name_attribtue'] = value_attribute in your PHP script.
Just for example. If you changed the form method attribute to POST, your PHP script would need to use the $_POST array instead of the $_GET array.
I have a functioning "Account Notes" page that allows the user to add notes to their account for their own uses. I want to allow my admins to input notes into user accounts without logging in. I have a Dropdown on the Admin page that allows the admins to select a user. After selecting the user I want to echo the notes already in their notes section into the TextArea below and allow an update to the notes.
function getNotes(){
global $id;
$result = query("SELECT * FROM `extras` WHERE `userid`=".$id);
while ($row = mysql_fetch_assoc($result)){
return $row['notes'];
}
}
<form action="update.php" method="post">
<h4 class="widgettitle title-primary">Account Notes</h4>
<br/>
<textarea rows="20" scroll="auto" class="txt" name="notes" style="width: 80%;"/><?=getNotes();?></textarea>
<div style="text-align:center;"><button class="btn btn-primary">Update</button></div>
<input type="hidden" name="formtype" value="updatenotes" />
<input type="hidden" name="redir" value="notes.php" />
</form>
</form>
</div>
</div><!--row-fluid-->
All of the above code works correctly, so there are no issues above. My issue lies below...
The following code is what I have on my admin.php page for the user notes addition code...
<div id="addusernotes">
<form action="update.php" method="post">
<h4 class="widgettitle title-primary"><center>Add User Notes</center></h4><br/>
<?php
echo '<select id="user" name="user">';
$user = query("SELECT * FROM `users` ORDER by `user`");
if($user && mysql_num_rows($user))
{
while ($row = mysql_fetch_assoc($user))
{
echo '<option value="'.$row['id'].'">'.$row['user'].'</option>';
}
}
echo '</select>';
?><br/>
Notes:<br/>
<textarea rows="5" scroll="auto" class="txt" name="adminusernotes" style="width: 80%;"/></textarea><br/>
<input type="submit" name="adminnotes" value="Add Notes"><br/><br/>
<input type="hidden" name="formtype" value="adminusernotes" />
<input type="hidden" name="redir" value="admin.php" />
</form>
What JQuery do I need to add to the admin.php file to allow the Notes from the DB to display in the TextArea? I have been stumped on this for nearly 3 days.....Any help will be more than appreciated!!!
FYI....I have intentionally left out some of my "proprietary code for security reasons...
To alter a textarea's content with jQuery, you simply use:
$("#element-id-here").val("whatever value...");
But give your textarea an ID first. And you can change the value to a variable too.
If you want PHP to echo the textarea contents:
<textarea><?php echo "contents here"; ?></textarea>
I have following code on the top of product detail page
<?php
include("include/connection.php");
session_start();
$ses_id = session_id();
$ID = (int)$_REQUEST['id'];
if (!isset ($_POST['submit']))
{
?>
<div class="row">
<div class="span9">
<div class="span6 ">
<h4>Review(0)</h4>
<p>There are no review for this product</p>
<h4>Write a Review</h4>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Your Name:</p>
<input type="text" id="txtname" name="Rname" placeholder="write your name..." />
<p>Your Review</p>
Excelent<input type="radio" name="reviewbtn" class="radio" value="Excelent" />
Good<input type="radio" class="radio" name="reviewbtn" value="Good" />
Poor<input type="radio" class="radio" name="reviewbtn" value="Poor" /><br/>
<textarea id="txtreview" name="txtbx" cols="50" rows="10"
class="container-fluid"></textarea>
<input type="submit" value="submit" class="btn" />
</form>
</div>
</div>
</div>
<?php
} else {
$Rname = mysql_real_escape_string(stripslashes(htmlentities($_POST["Rname"])));
$Reviewbtn = $_POST["reviewbtn"];
$Txtbox= mysql_real_escape_string(stripslashes(htmlentities($_POST["txtbx"])));
$sql=mysql_query ("INSERT INTO reviews (Name, Comments,Rating) VALUES('$Rname','$Reviewbtn','$Txtbox')", $con) or die (mysql_error());
echo "Record added succesfully. You will be redirected to previous page in 5 seconds";
header( "refresh:5;url= product_detail.php" );
}
mysql_close($con);
?>
here I am getting error
Notice: Undefined index: id in \product_detail.php on line 5
( which is $ID = (int)$_REQUEST['id']; )
basically I am building a rating form on a product,please if someone can modify the code so form submission could be done using ajax jquery call so page should not load again
remove int from it there no need of it.
$ID = $_REQUEST['id'];
There is no Element named ID in your form
So when you are going to catch this it occurred error
$ID = $_REQUEST['id'];
^ provide actual form element's name here
When you coming to this page from previous page it is not showing error
after submitting from it is showing error.
If it is, add an hidden element into your form
<input type='hidden' name='id' value="<?php echo $ID; ?>">
It will solve the problem
Use some other word as name attribute rather than naming it as id you can set it to user_id or something else you want,because it is not a good practice
if(isset($_REQUEST['user_id'])){
$ID = $_REQUEST['user_id'];
}
I am creating a faq panel for there can be multiple answers for question and i want to take the answer id .because i am storing comment by answer id
the problem is that how to sent the $answer_id to the comment_submit_process.php and how to recognize the answer ?
$selected_ques= mysql_prep($_GET['ques']);
$query = "SELECT * FROM formanswer where question_id = {$selected_ques}";
$ans= mysql_query($query);
if($ans){
while($answer = mysql_fetch_array($ans))
//here is the form
<form id="add-comment" action="comment_submit_process.php" >
<textarea class="comment-submit-textarea" cols="78" name="comment" style="height: 64px;"></textarea>
<input type="submit" name="submitbutton" value="Add Comment" class="comment-submit-button" >
<br> <?php
$ans_id= $answer['id']; //i am fatching the $answer['id'] from database
?>
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>" />
<span class="counter ">enter at least 15 characters</span>
<span class="form-error"></span>
</form>
<?php }} ?>
You might have typo here !! it should be..
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
Other thing, you can add get param to action link it self.
<form id="add-comment" action="comment_submit_process.php?<?php echo $answer['id']; ?>" >
Instead of setting the ans_id, every time to the hidden field .
Generate a string of ans_id seperated with "," until while loop ends append the string and assign that value to the hidden field and in form action page you can get that value and generate van array from that string with delimiter ",".Now you can have the array of ans_id in your form action page
$answer_array = "nothing";
while($answer = mysql_fetch_array($ans))
{
if( $answer_array == "nothing")
$answer_array = $answer;
else
$answer_array .= ",".$answer;
}
<input type="hidden" name="answer_arr" value="<?=$answer_array?>">
In Form action page you can get that hidden value
$ans_array= explode(",",$_GET['answer_arr']);
You can echo answer_id in form action tag as additional parameter like this:
<form id="add-comment" action="comment_submit_process.php?ans_id=$ans_id" >
//Your stuff here
</form>
in comment_submit_process.php you can identify answer by
$ans_id=$_GET['ans_id'];
You can do further processing by using $ans_id
Edit:
change this line:
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>"
to:
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
so that value of that field would be $ans_id fetched from DB.