I am creating a carpool website and I need help with something. I have created a button and if people click on that button than the value of free spaces in the car will decrement by one(meaning the one who will click it will reserve the place). So this value must be changed in the database and also when I refresh the page it will change on the table where I display info.This is what I have written so far but doesn't seem to make a change.Thanks in advance!
<tbody>
<?php while ($row=mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['username_krijim']; ?> </td>
<td><?php echo $row['nisja']; ?> </td>
<td><?php echo $row['destinacioni']; ?> </td>
<td><?php echo $row['data_krijim']; ?> </td>
<td><?php echo $row['ora_krijim']; ?> </td>
<td ><?php echo $row['vende_krijim']; ?> </td>
<td><?php echo $row['cmimi_krijim']; ?> </td>
<td><?php echo $row['mesazhi_krijim']; ?> </td>
<td><form action="index_show.php" method="POST"><input class="btn btn-primary" style="background:#f2545f;" type="submit" name="rezervo" value="Rezervo"></form></td>
<?php
if(isset($_POST['rezervo'])){
$id_krijim=$row['id_krijimi'];
$sql = "UPDATE krijo_itinerar SET vende_krijim=vende_krijim-1 WHERE id_krijimi='$id_krijim'";
mysqli_query($db, $sql);
}
?>
</tr>
<?php } ?>
</tbody>
You can use a form to send the POST request to tell the PHP to initialize.
<td><form action="" method="POST"><input class="btn btn-primary" style="background:#f2545f;" type="button" name="rezervo" value="Rezervo"></form></td>
<?php
if (isset($rezervo)){
$sql = "UPDATE krijo_itinerar SET vende_krijim=vende_krijim-1 WHERE username_krijim='".$username_krijim."'";
mysqli_query($db, $sql);
}
?>
I also don't see the variable $username_krijim being set. I don't know if it's somewhere else in your code but that may also be contributing to your problem.
Edit:
Ok so the problem I see are that that the variable is not being sent through PHP. That previous edit I provided won't work I'll try and think of something.
Related
I have question about how i shall do this.
First i have taken out a list from a mysql database which.
What I want to do is to be able to click on one of these items in the list so that I can get the underlying facts for this particular item.
Now i want to use this as a link to get one row from the database.
The database is done and the list i can see in a website but i dont get the last part to work
show_all_symbol.php
//This show the list
<?php
include 'connect.php';
$sql = "SELECT * FROM t1_symbolism ";
$q = $pdoconn->query($sql);
$q ->setfetchmode(PDO::FETCH_ASSOC);
?>
<table>
<head>
<tr><th>Symbol</th><th>Alternativtnamn</th> <th>Symboltyp</th></tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>"/></td>
<tr>
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
show_symbol.php
<body>
<?php
$funamn = trim($_POST['symbname']);
include 'connect.php';
$sqlanswer = $pdoconn->query(" SELECT * from t1_symbolism where symb_name '$funamn'");
$row = $sqlanswer->fetch();
?>
<table >
<thead>
<tr><th>Symbol</th><th>Symbolnamn</th><th>Alternativtnamn</th> <th>Symboltyp</th><th>Fakta</th></tr>
</thead>
<tr>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>" width="250" height="250"/></td>
<td><?php echo ($row['symb_name']); ?></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
<td><?php echo ($row['symb_history']); ?></td>
</tr>
</table>
</body>
What i want to do is to use this part as Use this part as a clickable link so I can view a record in the table.
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
Thank in the advance!
it looks like you are looking for a way to pass this value $row['symb_name'] to this value $_POST['symbname']. Here's what i think you should try :
in your show_all_symbol.php
<td>
<a id="leftside_meny" href="<?php echo "/symbol/show_symbol.php?symb_name=" . $row['symb_name'];?>" target="texten">
<?php echo ($row['symb_name']); ?>
</a>
</td>
in your show_symbol.php
$funamn = trim($_GET['symb_name']);
If you want to use $_POST then you will have to either figure a way to make a form out of each clickable element and configure it to make a POST or do it using javascript or jquery.
How would I be able to add an Edit to the table after the table.
Any help would be greatly appreciated. I've tried a number of variations such as : Trying to add Edit
<center><?php
//connect to mysql for search
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `2016server` WHERE CONCAT(`ServerGroup`,
`Week`, `Status`, `ServerName`, `IP`, `DateComplete`, `DateSched`,
`HeatTicket`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `2016server`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
//start the webpage
<html>
<head>
<title>SEARCH 2016 Servers</title>
</head>
<body>
<center><h3>Type in your query to search all of the 2016
Servers</h3><br>
<form action="search2016.php" method="post">
<input type="text" name="valueToSearch" placeholder="SEARCH">
<input type="submit" name="search" value="GO!"><br><br>
table
<table>
<tr>
<th>ServerGroup</th>
<th>Server Name</th>
<th>Date Scheduled</th>
<th>Heat Ticket</th>
<th>Status</th>
<th>Date Complete</th>
<th>Week</th>
<th>Downtime</th>
<th>IP Address</th>
<th>Operating System</th>
</tr>
populate table from mysql database
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['ServerGroup'];?></td>
<td><?php echo $row['ServerName'];?></td>
<td><?php echo $row['DateSched'];?></td>
<td><?php echo $row['HeatTicket'];?></td>
<td><?php echo $row['Status'];?></td>
<td><?php echo $row['DateComplete'];?></td>
<td><?php echo $row['Week'];?></td>
<td><?php echo $row['DT'];?></td>
<td><?php echo $row['IP'];?></td>
<td><?php echo $row['os'];?></td>
This is the section that I'm trying to add the edit to in the table. From the edit code above, it's trying to go to the 2016edit.php page with the ID how ever since there is no such page, it gets a 404 error.
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
close the first <form tag early (after the <input type="submit")
add new column named action where you'll put your edit button
use this for the edit
<form action="youreditform.php" method="post">
<button type="submit" name="edit" value="<?= $theidofthis; ?>">Edit</button>
</form>
Add another "td" tag and make it a link and it will go to the next edit page on click. but for editing purpose you have to post id in the url to check that which record was clicked to edit. hope it will be helpful.
<tr>
<td><?php echo $row['ServerGroup'];?></td>
<td><?php echo $row['ServerName'];?></td>
<td><?php echo $row['DateSched'];?></td>
<td><?php echo $row['HeatTicket'];?></td>
<td><?php echo $row['Status'];?></td>
<td><?php echo $row['DateComplete'];?></td>
<td><?php echo $row['Week'];?></td>
<td><?php echo $row['DT'];?></td>
<td><?php echo $row['IP'];?></td>
<td><?php echo $row['os'];?></td>
<td><a href='edit.php'>edit</a></td>
</tr>
i know this question probably been asked and answered, and i also not sure what proper question i should ask, so pardon me :D
what im trying to do is:
i got a table, i get the value from mysql and paste it into the table through a LOOP
for each row i give it a button (edit button)
i give each button the id of value through a variable $id (eg. 1 2 3 4 )
my question is how can i get the specify button that i press by $_POST? i tried every way i could think of but it not work ( in my code below i warp the id with another variable=asdf so that it static but i can only get the last value of the id)
$viewvalue = mysqli_fetch_all ($result, MYSQLI_ASSOC);
<?php if (count($viewvalue) > 0): ?>
<table id="t01">
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($viewvalue))); ?></th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($viewvalue as $row): array_map('htmlentities', $row); ?>
<tr>
<td><?php echo implode('</td><td>', $row);?></td>
<td><input type="submit" class="edit" name="<?php $a=$row['id']; $$a='asdf'; echo $$a; ?>" value="Edit" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php
if(isset($_POST['asdf'])){
echo $a;
}
?>
i know theres a way to use jquery and ajax but sadly i cant get it to work, is it a way to do it with php alone?
I'll offer to replace this row:
<td><input type="submit" class="edit" name="<?php $a=$row['id']; $$a='asdf'; echo $$a; ?>" value="Edit" /></td>
by:
<td><button class="edit" name="act" value="<?php echo $row['id'];?>">Edit</button></td>
after that you'll simply check the value of the $_POST['act']
So I'm creating a database for my website and I want to create an admin section that allows you to add or delete from the table. Here is a snapshot of what I want to achieve...
In my php file I have if($_POST['delete_category']) which correctly gets the delete button clicks, but then I'm not sure how to distinguish which delete button was actually clicked. I'm sure this is a very simple solution, but I'm stuck. Thanks!
You can discern which button is submitted by this following markup (based on your example fetched results from DB):
<?php
if(isset($_POST['delete_category'])) {
$id = $_POST['delete_category']; // the value="1" or value="3" goes in here
echo $id;
}
?>
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<tr>
<td>1</td>
<td>Senior Pictures</td>
<td><button typpe="submit" name="delete_category" value="1">Delete</button></td>
<!-- each delete button has the same name, but different values -->
</tr>
<tr>
<td>3</td>
<td>Engagements</td>
<td><button typpe="submit" name="delete_category" value="3">Delete</button></td>
</tr>
</table>
</form>
If I had to guess this makes sense on the fetching: (Note: Just a sample!)
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<?php while($row = $results->fetch_assoc()): ?> <!-- assuming mysqli() -->
<?php while($row = mysql_fetch_assoc($result)): ?> <!-- assuming on mysql (bleh) -->
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<button typpe="submit" name="delete_category" value="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
When a user clicks a submit button a popup should appear asking them if they wish to submit the selection. I've been adding to the view, and I just noticed that although the form works properly, I no longer receive a popup asking to confirm the selection.
This consists of two tables. An upper one to display info where each row has a checkbox. The bottom table allows you to choose if you want to submit only selected checkboxes, or all of the rows in the table.
<table>
<?
echo $this->Form->create(null,array(
'onsubmit'=>'return confirm("Are you sure you want to archive?'));
?>
<th>Order ID</th><th>Order Date</th><th>Order Total</th><th>Status</th><th>View</th><th>Select to Archive</th>
<?php foreach ($orders as $order): ?>
<tr>
<td><?php echo $order['Order']['id'];?> </td>
<td><?php echo $order['Order']['date']; ?></td>
<td><?php echo $order['Order']['total'];?> </td>
<td><?php echo $order['Order']['order_status'];?> </td>
<td><a href="/orders/details/<?php echo $order['Order']['id']; ?>"/>View Order</a> </td>
<td><? echo $this->Form->checkbox('archive_value.', array('hiddenField' => false, 'value' => $order['Order']['id'])); ?></td>
</tr>
<?php endforeach; ?>
</table>
<table class = "table_order_status">
<tr>
<td width="350">
<?
echo $this->Form->input('archive_values', array('options' => $archive_options, 'value' => $select_value, 'name' => 'archive'));
?>
</td>
<td>
<?
echo $this->Form->end(__('Submit'));
?>
</td>
</tr>
</table>
You have a typo in the onsubmit option. Look at your quotes. Im guessing this line:
'onsubmit'=>'return confirm("Are you sure you want to archive?'));
should be:
'onsubmit'=>'return confirm("Are you sure you want to archive?")');