deleting rows in mysql table with php - php

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.

Related

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

Form Action Process.php file not echoing results from mySQL DB

Currently, I have an index.php file with a form and a text input from the user as well as a submit button. When the submit button is pressed, the process.php file is supposed to get the data and echo it out. However, it just sends me to a blank page and does not echo anything out. I am well aware that I would need to style it the page, etc... But it just isn't echoing out at all. I am already connected to the mySQL DB with another php script and have tested that and it works fine so I know I am connected. What am i doing wrong?
index.php
<form action="process.php" form method="post" id="myForm">
<div class="col-xs-12 col-md-6">
<div class="input-group">
<span class="input-group-addon">
<input aria-label="..." type="checkbox" id="checkbox1">
</span>
<input aria-label="..." class="form-control" type="text" id="food1">
</div>
</div>
<input type="submit" value="Submit">
</form>
process.php
<?php
//Check whether the form has been submitted
if($_POST['submit'] == "Submit")
{
$varFood1 = $_POST['food1'];
$sql = "SELECT menu.dish FROM menu WHERE menu.description LIKE '%varFood1%'"; // sql query
$result = mysql_query($sql);
// Loop the recordset $result
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($result)) {
echo "Items : {$row['dish']}\n";
}
}
?>
Use name attribute not id. <input aria-label="..." class="form-control" type="text" id="food1"> Try printting out the POST in the future.
<input aria-label="..." class="form-control" type="text" id="food1" name="food1">
Additional changes...
$varFood1 = mysql_real_escape_string($_POST['food1']);
$sql = "SELECT menu.dish FROM menu WHERE menu.description LIKE '%$varFood1%'";
without escaping you open yourself to injections. You should consider switching driver to mysqli or PDO as well.
The problem is that you are checking for $_POST['submit'], which the process.php file does not see, because it does not exist in the POST array. Solution: give your submit button a name, instead of this:
<input type="submit" value="Submit">,
which is what you have, do this:
<input type="submit" name="submit" value="Submit">

Edit Mysql Table With A Form

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');

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

how to send value to the from action page from database

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.

Categories