i want to update my database - php

1-now already i added some texts on my database then after i want to update my database
anybody can help me?
i hope someone can help me
<?php
mysqli_query($db,"INSERT INTO tasks (task,status)
VALUES ('$task','$status')");
header('location:SIMPLE.PHP');
}
}
if (isset($_GET['check_selected'])) {
foreach ($_GET['check_selected'] as $key => $value) {
$row = mysqli_query($db,"DELETE FROM tasks WHERE id =
$value;") or die(mysqli_error($db));
}
}
$tasks = mysqli_query($db,"SELECT * FROM tasks ORDER BY id DESC");
?>
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="SIMPLE.PHP">
<input type="text" name="search" class="status_input" placeholder="TEXT" />
<input id='textbox' type='checkbox' value='1' name='status'>
<button type="submit" class="add_btn" name="submit">Add</button>
<p><?php echo $errors; ?></p>
</form>
<form action="SIMPLE.PHP" method="GET">
<tbody>
<tr>
<p><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
<?php
foreach ($rows as $row_id => $row)
{
?>
<input name="check_selected[]" type="checkbox" value=<?php echo $row['id'];?>"/>
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['status'];?>"
<?PHP if($row['status']){echo 'checked';
}
?>
>
<td class= "task"><?php echo $row['task']?></td>
<?PHP
}
?>
</tr>
</tbody>
</form>
</body>
</html>

Related

Update database from inserted table fields

I have made a table with editable fields and i have to add functionality to the update button. I have looked up many posts but cant seem to figure out how it works.
Heres what i have at this moment:
<?php
if (isset($_GET['update'])) {
$query = mysql_query("UPDATE iekartas SET
iernosauk='$Ierices_Nosaukums', tips='$Tips', razotajs='$Razotajs',
izgatdat='$Izgatavosanas_datums', adrese='$Adrese' where id='$Iekartas_ID'", $connection);
}
$query = mysql_query("select * from iekartas", $connection);
while ($row = mysql_fetch_array($query)) {
echo "<b><a href='iekartas.php?update={$row['Iekartas_ID']}'>{$row['Ierices_Nosaukums']}</a></b>";
echo "<br />";
}
?>
<tr>
<form action="ierices.php" method="post">
<td><input class="checkbox" type="checkbox" id="<?php echo $row['Iekartas_ID'] ?>" name="id[]"></td>
<td> <input type="text" name="iernosauk" value=" <?php echo $row["Ierices_Nosaukums"]; ?>"></td>
<td> <input type="text" name="tips" value=" <?php echo $row["Tips"]; ?>"></td>
<td> <input type="text" name="razotajs" value=" <?php echo $row["Razotajs"]; ?>"></td>
<td> <input type="text" name="izgatdat" value=" <?php echo $row["Izgatavosanas_datums"]; ?>"></td>
<td> <input type="text" name="adrese" value=" <?php echo $row["Adrese"]; ?>"></td>
<td> <input type="hidden" name="id" value=" <?php echo $row["Iekartas_ID"]; ?>"></td>
<?php
$i++;
}
?>
<button type="button" action="update.php" name="updatedb" class="btn btn-danger" id="updatedb" value=update>Atjaunot</button>
</tr>
</form>
Button is client side so your "update.php" won't be triggered when you click on button. What you have to do is change your button type to submit so your form will actually send a POST request when you click on the button and it will call the action on the form (ierices.php)
<form action="ierices.php" method="post">
...
<button type="submit" class="btn btn-danger" id="updatedb">Atjaunot</button>
</form

Saving a list of POSTed checkboxes

I have a (MySQL) database table that looks like:
CREATE TABLE `dm_webcategory` (
`cat_id` int(10) DEFAULT NULL,
`PUB_CATEGORY` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`web_only` tinyint(1) DEFAULT '0',
UNIQUE KEY `dm_webcategory_pk` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to display and save ALL rows in this table as checkboxes
If checked, will mark web_only as 1, and unchecked will mark web_only as 0.
Displaying them is simple:
<form method="post" action="">
<?php foreach($categories as $category): ?>
<input type="checkbox" name="category[]" value="<?php echo $category->cat_id;?>" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
But how should I go about saving this list of checkboxes?
I can access all of them using $_POST['category'], but this will only $_POST the checked boxes?
To answer my own question, I could update the whole table web_only field to 0, and then foreach the posted result to update individually to 1, assuming all those not included were unchecked.
SQL: UPDATE dm_webcategory SET web_only = 0
Then PHP:
foreach($this->input->post('category') as $category)
{
$this->db->update('dm_webcategory', array('web_only' => '1'), array('cat_id' => $category));
}
The above doesn't feel very 'safe' though, is there a better way? What about the scenario when I wasn't displaying / saving every row in the table?
Try this.
HTML:
<form method="post" action="">
<?php foreach($categories as $category): ?>
<input type="hidden" name="category[<?php echo $category->cat_id;?>]" value="0"/>
<input type="checkbox" name="category[<?php echo $category->cat_id;?>]" value="1" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
PHP:
foreach($this->input->post('category') as $id => $value)
{
$this->db->update('dm_webcategory', array('web_only' => $value), array('cat_id' => $id));
}
Try this form:
<form method="post" action="">
<?php foreach($categories as $category): ?>
<input type="hidden" name="category[<?php echo $category->cat_id;?>]" value="0" />
<input type="checkbox" name="category[<?php echo $category->cat_id;?>]" value="1" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
and then:
foreach($this->input->post('category') as $cat_id => $web_only)
{
$this->db->update('dm_webcategory', array('web_only' => $web_only), array('cat_id' => $cat_id));
}
You try to use following code
controller Code
$array = $this->input->post('s2');
$data['subject'] = implode(',',$array);
$this->student_model->saveForm($data);
view Code
<form method="post" action="">
Categories :
<?php foreach($categories as $category): ?>
<input type="checkbox" name="s2[]" value="<?php echo $category->cat_id;?>">English
<?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>
model
$this->db->insert('Tablename', $data);

Can't get value from post method to SQL query

spent long hours trying to add delete functionality to my to-do list. I can add todo to sql, also it apears on page, but cant find a way to delete my todo. Maybe someone can help me? here's the code:
<!DOCTYPE html>
<html>
<head>
<title>To Do</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="task" placeholder="New task"/>
<input type="submit" name="add" value="Add" autocomplete="off" required/>
</form>
<?php
if (isset($_POST['add'])) {
$sql = "INSERT INTO list (todo) VALUES ('$_POST[task]')";
$conn->query($sql);
}
if(isset($_POST['delete'])){
$sql ="DELETE FROM list WHERE todo={$_POST['todo']}";
$conn->query($sql);
}
$query = "SELECT * FROM list";
$result = mysqli_query($conn, $query);
?>
<?php
while ($row = mysqli_fetch_assoc($result)) { ?>
<?php echo $row['todo'] ?>
<form action='index.php?todo="<?php echo $row['todo']; ?>"'method="post">
<input type="hidden" name="todo" value="<?php echo $row['todo']; ?>">
<input type="submit" name="delete" value="Delete">
</form> <br>
<?php }
$conn->close();
?>
</body>
</html>

Deleting specific div with PHP

I'm making a To do application in PHP for a school assignment.
At this point, I can add new tasks. But the problem is:
When I click at a div (to delete it), the div is deleted, BUT all the other tasks get invisible. They only show up again if I create a new task.
This is my code
File Index.php
<div class="container">
<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
<input class="addText title" type="text" value="Click to add a task" name="nextToDo">
<input class="clickablePlus" type="submit" value="+" name="submit"></div>
</form>
<?php if(!$empty): ?>
<?php foreach ($_SESSION["todoList"] as $_SESSION["key"] => $toDo): ?>
<div class="toDo">
<form action="/Periodeopdracht/index.php" method="POST">
<button value="<?php echo $_SESSION["key"] ?>" name="delete" class="delete" type="submit" >X</button>
<button value="<?php echo $_SESSION["key"] ?>" name="done" class="done" type="submit" >V</button>
<div value="<?php echo $_SESSION["key"] ?>" class="textToDo"><?= $toDo ?></div>
</form>
</div>
<?php endforeach ?>
<?php endif ?>
</div>
Application.php
<?php
session_start();
$GLOBALS["empty"] = true;
$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();
if(isset($_POST["submit"]))
{
$empty = false;
array_unshift($_SESSION['todoList'], $_POST["nextToDo"]);
}
if(isset($_POST["delete"]))
{
unset($_SESSION['todoList'][$_SESSION["key"]]);
//var_dump($key);
}
?>
Try to handle it like a normal post, since this key will be available on submission:
<!-- no need to create every form -->
<form action="/Periodeopdracht/index.php" method="POST">
<?php foreach ($_SESSION["todoList"] as $key => $toDo): ?>
<div class="toDo">
<button value="<?php echo $key; ?>" name="delete" class="delete" type="submit" >X</button>
<button value="<?php echo $key; ?>" name="done" class="done" type="submit" >V</button>
<div value="<?php echo $key; ?>" class="textToDo"><?= $toDo ?></div>
</div>
<?php endforeach; ?>
</form>
Then, upon submission:
if(isset($_POST["delete"])) {
$key = $_POST["delete"];
unset($_SESSION['todoList'][$key]);
}
Sidenote: Are you sure your form action is /Periodeopdracht/index.php? Maybe this operation is intended to use Application.php like you posted the $_POST processes above.
Also, this is superfluous/unneeded.
$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();
This reassignment makes no sense.
Just use a simple initialization:
if(!isset($_SESSION['todoList'])) {
$_SESSION['todoList'] = array();
}
A Little bit of rewrite:
Form:
<div class="container">
<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
<input class="addText title" type="text" value="Click to add a task" name="nextToDo">
<input class="clickablePlus" type="submit" value="+" name="submit"></div>
</form>
<!-- no need to create every form -->
<form action="/Periodeopdracht/index.php" method="POST">
<?php foreach($_SESSION["todoList"] as $key => $toDo): ?>
<div class="toDo">
<button value="<?php echo $key; ?>" name="delete" class="delete" type="submit" >X</button>
<button value="<?php echo $key; ?>" name="done" class="done" type="submit" >V</button>
<div class="textToDo"><?php echo $toDo; ?></div>
</div>
<?php endforeach; ?>
</form>
</div>
Then in /Periodeopdracht/index.php:
<?php
session_start();
// simple initialization
if(!isset($_SESSION['todoList'])) {
$_SESSION['todoList'] = array();
}
// addition / push inside items
if(isset($_POST["submit"], $_POST["nextToDo"])) {
$_SESSION['todoList'][] = $_POST["nextToDo"];
}
// delete key
if(isset($_POST["delete"])) {
$key = $_POST["delete"];
unset($_SESSION['todoList'][$key]);
}
?>

How to retrieve value on from in php?

I am trying to retrieve value on php page but it's not retrieving value.
Here is my code
retrieve
<html>
<body>
<?php
include('conn.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
$select_table = "select * from clientreg order by id limit $start,$per_page";
$variable = mysql_query($select_table);
?>
<form name="frmUser" method="post" action="">
<div style="width:100%;">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblListForm">
<tr class="listheader">
<td></td>
<td width="230" >*****</td>
</tr>
<?php
$i=1;
$j=0;
while($row = mysql_fetch_array($variable))
{
if($j%2==0)
$classname="evenRow";
else
$classname="oddRow";?>
<tr class="<?php echo $classname;?>">
<td><input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?>" ></td>
</tr>
<?php
$j++;}
?>
<tr class="listheader">
<td colspan="9"><input type="button" name="update" id="onclick" value="Update" /> <input type="button" name="delete" value="Delete" onClick="setDeleteAction();" />
<input type="button" name="assign" id="assign" value="Assign" onClick="setLeadAssignAction();" />
<?php
$sql = mysql_query("SELECT *FROM login where role=1");
while ($row = mysql_fetch_array($sql)){
?>
<tr class="listheader">
<td><input type="checkbox" name="eid[]" value="<?php echo $row["eid"]; ?>" ><?php echo $row["username"]; ?></td>
</tr>
<?php
}
?>
</td>
</tr>
</table>
</div>
</form>
<form class="form" method ="Post"action="" id="contact">
<?php
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
for($i=0;$i<$rowCount;$i++) {
$result = mysql_query("SELECT * FROM clientreg WHERE Id='" . $_POST["users"][$i] . "'");
$row[$i]= mysql_fetch_array($result);
echo "shakti";
echo $row[$i]['id'];
}
}
?>
<img src="button_cancel.png" class="img" id="cancel"/>
<div id="left" style="height:400px;width:47%;float:left;margin-left:20px;margin-top:15px;border-radius:10px;">
<label>Lead Owner: <span>*</span></label>
<br/>
<input type="text" name="leadowner[]" id="lead" placeholder="Lead Owner"value=""/><br/>
<br/>
<label>First Name: <span>*</span></label>
<br/>
<input type="text" name="fname"id="fname" placeholder="Fname"/><br/>
<br/>
<label>Last Name: <span>*</span></label>
<br/>
<input type="text" name="lname" id="lname" placeholder="Lname"/><br/>
<br/>
<label>Mobile No: <span>*</span></label>
<br/>
<input type="text" name="mobile"id="mobile" placeholder="Mobile"/><br/>
<br/>
<label>Email Id: <span>*</span></label>
<br/>
<input type="text"name="email" id="email" placeholder="Email"/><br/>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Where am I wrong in this code?
Please sort out my problem
My problem is here
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
Any help will be appreciated
I have updated my code and added submit button.
You have two forms. Only the second form has a submit.
Form 1:
<form name="frmUser" method="post" action="">
<input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?> ">
</form>
Form 2:
<form class="form" method ="Post"action="" id="contact">
<input type="submit" name="submit" value="Submit">
</form>
The if construct does not receive the $_POST["users"], because it receives only the POST of the second submitted form.
$rowCount = count($_POST["users"]);
$rowCount will always be 0.
Form 2:
...
<?php
$rowCount = 0;
if ($_POST["users"] != "") {
$rowCount = count($_POST["users"]);
} else if ($_POST["rowcount"] != "") {
$rowCount = $_POST["rowcount"];
}
?>
...
<form class="form" method ="Post"action="" id="contact">
...
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="rowcount" value="<?php echo $rowCount; ?>">
</form>
Then the variable $rowCount will contain the count of the rows after the submit of any of the 2 forms.

Categories