<?php
$sel22 = mysql_query("SELECT id1nev, id2nev, id2, accept FROM barat WHERE id1= '$kariidje' AND accept = '1' ") or die("CANNOT FETCH DATA FOR ADMIN " . mysql_error());
if (mysql_num_rows($sel22) > 0) {
$i=0;
while ($data22 = mysql_fetch_array($sel22)) {
$i++;
?><tr>
<td>
<?php
include 'elemek/connection.php';
$idkitfogadel = $data22['id2'];
if(isset($_POST["submitelutasit"])){
$updatebaratt1 = "DELETE FROM barat WHERE id1 = $kariidje and id2 = $idkitfogadel";
mysql_query($updatebaratt1);
//header("Location: friends.php");
}
?>
<form id="form<?php echo $i; ?>" method="POST" action="#">
<Button type="SUBMIT" name="submitelutasit" id="submitelutasit<?php echo $i;?>" value="!"/></button>
</form>
</td>
<td align="center">
<a>Something</a>
</td>
</tr>
<?php
}
This is my code in my page. The problem is if I press the button in any line its delete all my data and not only what i want. Each line each button when I press a button its delete that line where is it. This is what i want. I cant solve this problem (sad).
Thank you
I will just assume that you are just playing around, and that's not even near to the production code.
Add input field to your form with id2 so you can use it later.
<form id="form<?php echo $i; ?>" method="POST" action="#">
<input type="hidden" name="id2" value="<?php echo $data22['id2'] ?>">
<Button type="SUBMIT" name="submitelutasit" id="submitelutasit<?php echo $i;?>" value="!"/></button>
</form>
And use it like this:
$idkitfogadel = $_POST['id2'];
Inside your if(isset($_POST["submitelutasit"])) statement.
I'm sure that little kitty died somewhere right now :(
Related
I'm trying to update multiple rows using a form and a foreach loop. I've tried solutions found on stackoverflow, but just can't seem to get it to work. I'm working on page project.php?id=13
My HTML form:
<form method="post" action="project.php?id=<?php echo $id; ?>">
<?php while($row = $shops->fetch_assoc()) : ?>
<input type="hidden" name="shop[]" value="<?php echo $row['id']; ?>">
<input type="text" name="boxposition[]" class="form-control" value="<?php echo $row['box_position']; ?>" style="width: 50px;">
<input type="submit" name="update" value="Update Order" class="btn btn-primary">
<?php endwhile; ?>
</form>
My PHP code (on the same page) which is executed on if(isset($_POST['update'])) :
$shopid = $_POST['shop'];
$boxorder = $_POST['boxposition'];
foreach ($shopid as $index => $value) {
$query = "UPDATE shops SET box_position = '".$boxorder[$index]."' WHERE id = '".$shopid[$index]."'";
$update_row = $db->update($query);
Unfortunately, it only updates the first row (lowest id). The very strange thing is that I get the correct values when I echo them using. E.g.:
echo $shopid[$index].'<br>';
echo $boxorder[$index].'<br>';
An example:
I update the $boxorder for 2 rows:
Row 1 (for shop 1): new value = 6 (old value was 7)
Row 2 (for shop 2): new value = 8 (old value was 5)
Using foreach to echo the result, it shows the correct values:
1 (shop id)
6 (new boxorder value)
2 (shop id)
8 (new boxorder value)
What am I missing? Thanks in advance for helping me out!
Try following
<?php while($row = $shops->fetch_assoc()) : ?>
<form method="post" action="project.php?id=<?php echo $id; ?>">
<input type="hidden" name="shop[]" value="<?php echo $row['id']; ?>">
<input type="text" name="boxposition[]" class="form-control" value="<?php echo $row['box_position']; ?>" style="width: 50px;">
<input type="submit" name="update" value="Update Order" class="btn btn-primary">
</form>
<?php endwhile; ?>
to
<form method="post" action="project.php?id=<?php echo $id; ?>">
<?php while($row = $shops->fetch_assoc()) : ?>
<input type="hidden" name="shop[]" value="<?php echo $row['id']; ?>">
<input type="text" name="boxposition[]" class="form-control" value="<?php echo $row['box_position']; ?>" style="width: 50px;">
<input type="submit" name="update" value="Update Order" class="btn btn-primary">
<?php endwhile; ?>
</form>
$shopid = $_POST['shop'];
$boxorder = $_POST['boxposition'];
$query = "UPDATE shops SET box_position =? WHERE id =?";
$db->prepare($query);
foreach ($shopid as $index => $value) {
$db->execute(array($boxorder[$index], $shopid[$index]));
}
This should do it. This way you prepare the statement with ? and fill it inside your for each.
you can do it with also as the following :
$query = "UPDATE shops SET box_position = :position WHERE id = :id ";
$db->prepare($query);
foreach ($shopid as $index => $value) {
$db->execute(
array(
':position' => $boxorder[$index],
':id' => $shopid[$index]
)
);
}
I have a form that is being created dynamically
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{ ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<?}?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
</form>
But i a not able to understand how to carry the value of input to the addtable.php page
Can anyone please tell how to submit these values from this form
In your addtable.php, you need to print_r $_POST and see what inputs you're getting.
// addtable.php
echo "<pre>";
print_r($_POST);
print_r($_FILES); // If you're expecting a file input
Also, you should modify your HTML a little:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) { ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<? } ?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button> <!--Place submit button outside loop -->
<?php } ?>
I think there is issue in submit button, you looping submit button with same name, try to put submit button out of while loop.
One of the reasons your PHP is not functioning correctly is because your PHP block, is not placed inside of PHP tags.
Your code should look like this:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php // added the PHP tags
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
?> // escaped the php tag
<input type="text" placeholder="<?= $row['heading']; ?>" name="<?= $row['heading']; ?>" />
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
}
?>
Notice how I also escaped PHP when I wanted to display any HTML. Of course, you could simply echo these out, but you can do this too! Personal preference, really.
Another little alteration I made was, rather than using <?php echo ... ?> you can simply use <?= $row['...']; ?>. It is faster and cleaner too!
In your addtable.php:
<?php
$inputValue = $_POST['input_name'];
//the $inputValue is the input value that you posted from form.
echo $inputValue;
?>
To receive data in addtable.php you have to get them by name property of inputs in the form. So you have to define the names of inputs or to retrieve them again from db in addtable.php
try this ,
addtable.php
<?php
foreach ($_POST as $key => $value){
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
?>
i hope it will be helpful.
You can create array of heading and access it in php:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<input type="text" placeholder="<?php echo $row['heading']; ?>" name="heading[]"> />
<?
}
?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
?>
</form>
In addtable.php:
$headingArray = $_POST['heading']; // here you will get all the heading in array format
The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>
I have a db table called 'ice_flavours' with the columns: 'id', 'flavours', 'date', 'selected'.
and another table called 'ice_today' like: 'id', 'flavours', 'date', 'selected'.
Now, on the left of the page I want to call from 'ice_flavours' and print the entries in a form together with a checkbox.
On the right of the page it shall say SHOWCASE where I want the todays selected flavours to be shown and they shall just stay in there until the end of today´s date and then automatically be discarded. Also when they were selected to show in SHOWCASE, i want them to not show on the left side.
So, when I select the checkbox next to a flavour, the values 'id', 'flavours', 'date', 'selected' ought to be entered into 'ice_today' but for some reason just always the last row of the ice_flavours table is entered and i get the message "Duplicate entry '0' for key 'PRIMARY'"
Maybe this can all be done by just using a single db table but I havent figured it yet. Can someone help please
edit.php:
<?php ob_start();
include_once("dbinfo.inc.php");
include_once("config.php");
if(isset($_POST['submit'])){
$selected = $_POST['selected'];
$id = $_POST['id'];
$flavour = $_POST['flavour'];
$date = $_POST['date'];
if($_POST["submit"] == "submit") {
for($i=0;$i<sizeof($selected);$i++) {
if(!empty($flavour)) {
echo"";
echo "<pre>";
print_r($_POST);
echo "</pre>";
$query="INSERT INTO ice_today VALUES('$id','$flavour','$date','$selected[$i]')";
mysql_query($query) or die(mysql_error());
}
}
echo "Entry added";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Edit</title>
</head>
<body>
<form name="form1" id="form1" method="POST" action="edit.php">
<table border="1" width="200px">
<td>
<table border="1" width="200px">
<?php
$result = mysql_query("SELECT * FROM ice_flavours WHERE ('selected' != '1') ORDER BY flavour ASC");
$results = mysql_num_rows($result);
if ($results > 0)
{
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
$id=mysql_result($result,$i,"id");
$flavour=mysql_result($result,$i,"flavour");
?>
<tr>
<td align="center" valign="middle">
<?php echo $flavour; ?>
<input type="text" name="id" id="id" value="<?php echo $id; ?>">
<input type="text" name="flavour" id="flavour" value="<?php echo $flavour; ?>">
<input type="text" name="datum" id="datum" value="<?php echo date('Y-m-d'); ?>">
<input type="checkbox" name="selected[]" id="selected" value="<?php echo '1'; ?>">
</td></tr>
<?php
$i++;
}
?>
<input type="submit" value="submit">
<?php } ?>
</td>
<td>
<table border="1" width="200px">
<tr><td align="center" valign="middle">
SHOWCASE
</td></tr>
<?php
include_once("dbinfo.inc.php");
include_once("config.php");
$result2 = mysql_query("SELECT * FROM ice_today ORDER BY flavour ASC");
$results2 = mysql_num_rows($result2);
if ($results2 > 0)
{
$num2=mysql_numrows($result2);
$j=0;
while ($j < $num2)
{
$id=mysql_result($result2,$j,"id");
$flavour=mysql_result($result2,$j,"flavour");
?>
<tr><td align="center" valign="middle">
<?php echo $flavour; ?>
</td></tr>
<?php
$j++;
}
}
echo '</td></tr></table>';
?>
</body>
</html>
<?php ob_end_flush(); ?>
Please take a look at the MySQL Reference Manual. You need to specify each column in your INSERT statement:
$query="INSERT INTO ice_today(id, flavours, date, selected) VALUES('$id','$flavour','$date','$selected[$i]')";
I also suggest you to not use the original MySQL-extension anymore, as it is deprecated as of PHP 5.5.x. When using another MySQL extension (MySQLi or PDO for instance: mysqli or PDO - what are the pros and cons?) you can also use prepared statements to become safe against SQL injections (which you aren't right now).
POST-values can be manipulated and as you just feed them into your query via string concatenation, you're not safe against them.
If the field for ID is an autoincrement field, you need to leave it off the insert because the db server will determine the value itself. You should list the fields being inserted, and leave that one off:
$query="INSERT INTO ice_today (flavours, date, selected) VALUES('$flavour','$date','$selected[$i]')";
Listing the fields you are inserting to also prevents insert statements from breaking when you add new fields later. So its good practice to start now.
There are lots of issues in your code.
You text boxes having same name with different values if there are two flavors.
Therefore flavor or id will be always from last row.
You can do something like this.
1.Replace your text boxes with this,
<input type="text" name="id_<?php echo $id; ?>" value="<?php echo $id; ?>">
<input type="text" name="flavour_<?php echo $id; ?>" value="<?php echo $flavour; ?>">
<input type="text" name="datum_<?php echo $id; ?>" value="<?php echo date('Y-m-d'); ?>">
<input type="checkbox" name="selected[]" value="<?php echo $id; ?>">
2.Replace your top if block (i.e. after submit)
if (isset($_POST['submit']) && $_POST["submit"] == "submit") {
$selected = $_POST['selected'];
for($i=0; $i < sizeof($selected); $i++) {
$id = $selected[$i];
$flavour = $_POST['flavour_' . $id];
$date = $_POST['date_' . $id];
$query="INSERT INTO ice_today VALUES('$id','$flavour','$date','$id')";
mysql_query($query) or die(mysql_error());
}
}
I have made an Event calender but now I'm facing a problem when the user deletes a event.
Screenshot: http://i.stack.imgur.com/LBLJ9.png
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php
// event weer gegeven
while ($events = mysql_fetch_array($resultEvents)){
echo "<strong>Event ID:</strong> ".$events['id']."<br>";
echo "Added: ".$events['added']."<br>";
echo "Title: ".$events['titel']."<br>";
echo "Detail: ".$events['content']."<br>";
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
<?php
if(isset($_POST['delete'])){
$user = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM kalender_contents WHERE `id` = '$events'") or die(mysql_error());
if($delet_query) {
echo "event deleted";
echo $events;
}
}
}
}
}
?>
But the WHEREid= '$events' does not work. How can I specify the ID on the button click.
Try to put its relevant event id into the form like
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="event_id" id="event_id" value="<?php echo $events['id'];?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
And in your submit query use like
$delet_query = mysql_query("DELETE FROM kalender_contents
WHERE `id` = $_POST['event_id']");
Whenever you are submitting the form then its event id will be passed and you need to pass that post variable to your delete query.
Further you need to escape the id to prevent the sql injection with mysql_real_escape_string at your query like
$delet_query = mysql_query("DELETE FROM kalender_contents
WHERE `id` = ".mysql_real_escape_string($_POST['event_id']));
Create a hidden field with value of event id.
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php
// event weer gegeven
while ($events = mysql_fetch_array($resultEvents)){
echo "<strong>Event ID:</strong> ".$events['id']."<br>";
echo "Added: ".$events['added']."<br>";
echo "Title: ".$events['titel']."<br>";
echo "Detail: ".$events['content']."<br>";
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="event_id" value="<?php echo $events['id'];?>">
<input type="submit" name="delete" value="Delete"><br >
<input type="submit" name="edit" value="Edit"><br >
</form>
<?php
if(isset($_POST['delete'])){
$user = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM kalender_contents WHERE `id` = '$events'") or die(mysql_error());
if($delet_query) {
echo "event deleted";
echo $events;
}
}
}
}
}
?>