mysqli query delete - php

I have all data from a database table display as an HTML table on a page.
When an admin selects a row, it adds a class of 'remove' to the parent row in the HTML table.
I then want to delete it from the mysqli database. Would I set the row fields as variables and delete the row that matches it in the database? I'm not sure how to do it, could someone please point me in the right direction?
HTML
<table>
<tr class="remove">
<td>field1</td>
<td>field2</td>
<td>field3</td>
</tr>
<tr>
<td>another field</td>
<td>another field</td>
<td>another field</td>
</tr>
</table>
Mysqli (relevant sample)
$mysqli->query("DELETE FROM $table WHERE Date="??" LIMIT 1");

If you're using JavaScript, which I assume you are as you're adding a class to a row when it's selected, you could do the following:
1. Include a hidden disabled input in each row:
<tr class="remove">
<td>field1</td>
<td>field2</td>
<td>
field3
<input type="hidden" disabled name="delete[]" value="<?php echo $id; ?>">
</td>
</tr>
2. Enable the hidden input when it is selected to be removed:
I'm not going to guess how you're adding the remove class to the row, but when doing so have it remove the disabled attribute of the hidden delete[] input contained within it.
3. Post the form:
Once posted you can run the query to delete all the rows with an ID in the $_POST['delete'] array.
If you're using prepared statements you can use the following query where ? is implode(',', $_POST['delete']):
"DELETE FROM " . $table . " WHERE FIND_IN_SET(id, ?) != 0"
I'm assuming here for the sake of simplicity that you have a unique id field (that doesn't contain ,s as part of its value). Obviously change to whatever is relevant for your database.

Related

Inserting data into DB column from an HTML table

my question is, how could I insert data from a table with the click of a button into mysql DB?
The table on my HTML is as follows:
<table>
<tr>
<th>Name</th>
<th>Number</th>
</tr>
<tr>
<td>data</td>
<td>data</td>
</tr>
</table>
The rows of course continue, depending on the data inserted.
In my DB, the tables are as follows:
Person (ID, Name, Number)
The number table in the DB is blank, while the Name and ID tables are already filled up with data.
I have a global variable where the number of total people is stored, which I think can come in handy
var totalPeople
As I mentioned before, I would like to make an ajax request and send the "Number" data from the HTML table into the Number column inside my DB, matching the name each person has (So, if for example, on my HTML table I have "David" with number 3 and "James" with number 5, I want the button to insert 3 into the David row in my DB and 5 into the James row).
I thought of creating an array from the table and sending that array through ajax and making the SQL query, but I'm quite stuck as to how to do this...
Thanks in advance
Well, here is how you should do, in my opinon:
persons_table.php: the page where you display your data table with hidden inputs with persons ids, example (use loops of course):
<form method="post" action="save_persons.php">
<table>
[headers...]
<tr>
<td><input type="hidden" name="id[]" value="<?= $id ?>" /> John</td>
<td><input type="text" name="number[]" value="<?= $number ?>" /> </td>
</tr>
</table>
[submit button...]
</form>
save_persons.php: the page which gonna be requested, you can loop on $_POST (I suppose you're doing this way) and UPDATE your rows like this:
UPDATE Person SET number = $number WHERE id = $id;
I would suggest you to have a look at PDO, prepared statements and Model View Controller design pattern.
Regards.

Send "Button"-ID on Submit

I have an PHP page, which contains a form with some different input fields, e. g. day, month, year etc.. The form method is POST, only one non-editable field (The user ID) is sent via GET.
Of course, there is a "Submit"-Button, which triggers the form Action (PHP Script on Server).
The form tags contain a table with empty cells too. Now comes my question:
If the user clicks into one of the table cells, the form should be submitted, but additional to the regular form data the ID of the table cell should be transmitted too (If via POST or GET doesn't matter to me). How can I do that?
//Edit 2:
...
<form method="post" action="<?= DOMAIN?>/.../addUserTimetable.php?uid=<?= $user->getUserID() ?>">
<select id="day" name="day">
...
</select>
...
<input name="yearend" id="yearend" ...>
<button type="submit">...</button>
<table class="bordered">
<tr>
<th>Std.</th>
<th>Montag</th>
<th>Dienstag</th>
<th>Mittwoch</th>
<th>Donnerstag</th>
<th>Freitag</th>
</tr>
<?php
for($i=1; $i<13;$i++) {
echo "<tr>";
echo "<th>".$i. "</th>";
for($j=1;$j<6;$j++) {
echo "<td id='h".$i. "d".$j. "' onclick='???'></td>";
}
echo "</tr>";
}
?>
</table>
</form>
...
The server sided procession is fine, but I haven't got any ideas - even after two hours google - how I could transmit the cell id additionally.
That shouldn't be to hard. Have a look at the following example:
<form>
<input type="text" name="something">
<table>
<tr>
<td><input type="submit" name="cel1">
</tr>
<tr>
<td><input type="submit" name="cel2">
</tr>
<tr>
<td><input type="submit" name="cel13">
</tr>
</table>
<input type="submit" value="save">
</form>
By giving the submit buttons in the table cells a name attribute, that name will also be present as a key on the $_REQUEST. Go ahead and var_dump the $_REQUEST and you'll see you can find out in the backend which button got pushed by checking which key exists.
Note that POST / GET is completely irrelevant here, both will work just the same. And obviously you could apply some css to those buttons to make them transparent and lay them on top of the table cells, so they don't look like buttons, but just "capture" the user's click.
One last side note, are you sure you want to send the userID as a GET parameter? That would be very easy for someone with bad intentions to manipulate. Consider not sending the ID at all, but keeping it in the session on the server.

Input hidden doesn't work properly

I'm developing a simple PHP page, this is a small part of my code that doesn't work properly.
I want to use DB and print some products and for each product, I want to show a "buy" button.
If I press this button, a hidden input must be set with the product id(which later has to be sent to another page).
But, if I use var_dump to control(if my data was correct), I can see that the ID is wrong (I see that its always shown in the last)
<form id="products_list" method="post" action="step2.php">
<table>
<tr>
<td align="center">Immagine</td>
<td align="center">Nome prodotto</td>
<td align="center">Descrizione prodotto</td>
<td align="center">Prezzo unitario</td>
<td align="center">Taglie disponibili</td>
<td align="center">Colori disponibili</td>
<td align="center">Nickname disponibili</td>
</tr>
<?php
$product = mysqli_query($mysqli, "SELECT * FROM products");
while ($row = mysqli_fetch_array($product)) {
$id = $row[id_products];
print("
<tr>
<td align=\"center\">".$row[img_products]."</td>
<td align=\"center\">".$row[name_products]."</td>
<td align=\"center\">".$row[description_products]."</td>
<td align=\"center\">".$row[price_products]."</td>
<td align=\"center\">".$row[size_products]."</td>
<td align=\"center\">".$row[color_products]."</td>
<td align=\"center\">".$row[nick_products]."</td>
<input type=\"hidden\" name=\"id_products\" value=\"".$id."\"/>
<td><input type=\"submit\" name=\"buy\" value=\"Acquista\"/></td>
</tr>");
}
?>
</table>
</form>
You should open and close your form within the while-loop.
EDIT
A little bit more explanation why you should open and close from within the while loop:
If the while-loop is inside the form tags, that means the hidden field is outputted multiple times within the same form. Since they are all named the same, you're only retrieving one value after the submit (the value of the last hidden input).
If you open and close the form within the while-loop, each hidden input and button are in their own form. Which means, when that form gets submitted, you're only retrieving the value of that specific hidden field. :-)

PHP variables entered as 0 in MySQL database

I'm relatively new to php and mysql. What I'm trying to do in this section of code is update 2 columns of information in a database based on the inputs of 2 text boxes. Whenever I try and update the values in the database they update to 0. I placed echo statements after I declared the $ variables and the values of both variables was the same as what I had typed into the boxes. But when I run the sql_query the new values in the database are 0 rather than the values of the 2 $ variables. Any help would be much appreciated! Thanks in advance!
<?php
$result = mysql_query("SELECT * from place_order ORDER BY item_name;");
echo "<form action='' method='POST'>Select an item:<select name='selection'><option>Select...</option>";
while ($row = mysql_fetch_assoc($result))
{
$item_name = $row["item_name"];
echo "<option>$item_name</option>";
}
echo "</select>
<input type='submit' value='Select Item' style='float:right;'/>";
$selection = $_POST['selection'];
echo "<br><br>Type the updated information into the text fields<br><br>";
echo "
<table width='300'>
<tr>
<td align='left'>Item Cost(€): </td>
<td align='left'><input type='text' name='cost'></td>
</tr>
<tr>
<td align='left'>Item Quantity: </td>
<td align='left'><input type='text' name='quantity'></td>
</tr>
<tr>
<td align='left'></td>
<td align='left'><input type='submit' name='button' value='Submit'></td>
</tr>
</table>
";
$cost = $_POST['cost'];
$quantity = $_POST['quantity'];
$selection = $_POST['selection'];
$sql = "UPDATE place_order SET item_cost='$cost', quantity='$quantity' WHERE item_name='$selection' ORDER BY item_name;;";
$query_update = mysql_query($sql);
if($query_update)
{
echo "Table updated! Click View Stock in the menu to view the updated table";
}
echo"</form>";
?>
There are two forms in this script:
one with the select box
another with the input boxes
each of these forms post individually.
Let's have some kind of state transition table:
forms are showing first time initially, all values are either empty
or default.
you select an item name and submit the first form.
option list is posted and $_POST['select'] has a value which then
is properly populated in the second form
the script runs til the end and immediately inserts an empty row to
your database with the selected item name
you make your inputs in the second form and submit it
this has two input variables: cost and quantity which get posted
due to the fact that the option list is in the other form, it will
not be posted and $select is empty at that state
the update statement will run but with no result since there is no
empty item_name in the db - useless update.
simple solution: make just one form out of that and you're done (except error handling which is missing entirely ...)
second solution: if only $_POST['select'] is set, put the value of $select in a hidden field and exit the script after echo-ing the second form.

PHP to take information from one page to another

i am quite new to php and i have this shopping cart up which shows images. product name and details from mysql database. the code for it is the following:
<table border="0" cellpadding="2px" width="600px">
<?
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><img src="<?=$row['picture']?>" /></td>
<td> <b><?=$row['name']?></b><br />
<?=$row['description']?><br />
Price:<big style="color:green">
£<?=$row['price']?></big><br /><br />
<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<? } ?>
</table>
What i want is for a user to be able to click on the picture/name of the product in which it should transfer the user to another page with the selected product being shown.
By the way, my mysql table consists of the following fields:
serial
name
description
price
picture
how should i go about doing this? thank u
Make a link like this:
<a href="product.php?product_id=<?=$row['serial']?><?=$row['name']?></a>
Then in the product.php page insert this:
$id = $_GET['product_id'];
$result=mysql_query("select * from products WHERE serial = '$id'");
$row=mysql_fetch_array($result);
Under that code you can add stuff like the picture and price etc etc :)
The serial of the product is being held ni the URL (product_id) and then called by the $_GET variable. SO product_id=1 will load the product with the serial number 1.
If you look at the URL of this current page it has the number 8142009 in the middle. If you change it to 8142008 it will load the previous question before yours. This example works in the exact same way.
You need to use either POST or GET parameters to do this.
I'll leave the actual logic of displaying the product up to you, but if you look at web addresses, they sometimes take the form of http://www.example.com/index.php?foo=bar&stuff=no. In this case, everything after the "?" is called the query string and has a "&" delimited list of variables. In this case, there are two variables, foo and stuff, which have the values of bar and no, respectively.
This will also call a script called "index.php". PHP exposes a superglobal (that is, a variable which is magically just available from everywhere) called $_GET. This is an array containing a mapping between GET variable names and their values. In this case, it would be the array ("foo"=>"bar", "stuff"=>"no"), so you can access the required values using $_GET['foo'] and $_GET['stuff'].
POST parameters are similar, but slightly different. These are submitted from HTML forms (with the "method" attribute set to "post"). In this case, the superglobal $_POST is filled out in the same way, but the array keys are the names of input elements in the HTML and their values are the values of the respective elements, so <input type="hidden" name="foo" value="bar" /> would create an entry in $_POST called "foo" with the value "bar".
From this, you should be able to have a decent go at implementing what you need.
you will probably need a new field in your mysql table to store product page url, then do the following
<table border="0" cellpadding="2px" width="600px">
<?
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><img src="<?=$row['picture']?>" /></td>
<td> <b><?=$row['name']?></b><br />
<?=$row['description']?><br />
Price:<big style="color:green">
£<?=$row['price']?></big><br /><br />
<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<? } ?>
</table>
or you could easily use peters answer lol

Categories