PHP update of Query with Form - php

Here is code I use to update data of one row of form table. This is the code I wanna change.
The change should allows me update not only one row of the shown table but every line:
if(isset($_POST['update'])){
$UpraveneQuery = "UPDATE uctovnictvo SET meno='$_POST[meno]', datum='$_POST[datum]', obchod='$_POST[obchod]', druh='$_POST[druh]', cena='$_POST[cena]', Poznamka='$_POST[poznamka]' WHERE uct_id='$_POST[hidden]' "; //--------------
mysql_query($UpraveneQuery, $con);
};
here are data with which I work for this particular case:
$sql = "SELECT * FROM uctovnictvo WHERE meno IN ('" . implode('\', \'', $option_meno) . "') AND obchod IN ('" . implode('\', \'', $option_obchod) . "') AND druh IN ('" . implode('\', \'', $option_druh) . "') AND datum BETWEEN '$date_start' AND '$date_end' ORDER BY $order";
$mojeData = mysql_query($sql,$con) or die($sql."<br/><br/> Chyba 1 je:".mysql_error());//run query a ulozit to premennej
here is code which generates form table which allows me to change content of it. Every row of the table has its own button for deleting or updating the row.
NOW
When I clik on ulozit zmenu (update) on certain row it allows me to post values of that one row and update variables of the row (query).
PLAN
I dont want to have button ulozit zmenu (update) next to every row of the table but only one butto which will post values of every shown row of the table.
while($zaznam = mysql_fetch_array($mojeData))
{
echo "<form action=index-5.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name='meno[]' value='" . $zaznam['meno'] . "'>" . "</td>";
echo "<td>" . "<input type=date name='datum[]' value='" . $zaznam['datum'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='obchod[]' value='" . $zaznam['obchod'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='druh[]' value='" . $zaznam['druh'] . "'>" . "</td>";
echo "<td>" . "<input step=any type='number[]' name=cena value='" . $zaznam['cena'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='poznamka[]' value='" . $zaznam['poznamka'] . "'>" . "</td>";
echo "<td>" . "<input type=submit name='update[]' value='ulozit zmenu'" . ">" . "</td>";[]
echo "<td>" . "<input type=submit name='zmazat[]' value='zmazat'" . ">" . "</td>";
echo "</tr>";
}

Related

How do I send all the rows in the table?

You are going to send the full line value using form tag. However, if you send a value, only the value in the last row is sent. How should it be modified?
in this table code
echo "<form action='../verification/medical_bills_check.php' method='post'>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td> <input type='hidden' name='id' value ='" . $row['id'] . " '>" . $row['id'] . "</td>";
echo "<td> <input type='hidden' name='storename' value ='" . $row['storename'] . " '>" . $row['storename'] . "</td>";
echo "<td> <input type='hidden' name='corporate_num' value ='" . $row['corporate_num'] . " '>" . $row['corporate_num'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno' value ='" . $row['store_mbrno'] . " '>" . $row['store_mbrno'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno_van' value ='" . $row['store_mbrno_van'] . " '>" . $row['store_mbrno_van'] . "</td>";
echo "<td ><input type='hidden' name='past_cash_receipts' value ='" . $row['past_cash_receipts'] . " '>" . $row['past_cash_receipts'] . "</td>";
echo "<td> <input type='hidden' name='past_card' value ='" . $row['past_card'] . " '>" . $row['past_card'] . "</td>";
echo "<td> <input type='hidden' name='pg_select' value ='" . $row['pg_select'] . " '>" . $row['pg_select'] . "</td>";
echo "<td> <input type='hidden' name='program_select' value ='" . $row['program_select'] . " '>" . $row['program_select'] . "</td>";
echo "<td> <input type='hidden' name='authority' value ='" . $row['authority'] . " '>" . $row['authority'] . "</td>";
echo "<td> <input type='hidden' name='apikey' value ='" . $row['apikey'] . " '>" . $row['apikey'] . "</td>";
echo "<td > <input type='submit'> </td>";
echo "</tr>";
}
echo "</form>";
and receive page code
$_SESSION["corporate_num"] = $_POST["corporate_num"];
$_SESSION["store_mbrno"] = $_POST["store_mbrno"];
$_SESSION["store_mbrno_van"] = $_POST["store_mbrno_van"];
$_SESSION["program_select"] = $_POST["program_select"];
$_SESSION["authority"] = $_POST["authority"];
$_SESSION["apikey"] =$_POST["apikey"];
I want result
id | corporate_num | store_mbrno | submit
1 1 1 submit_button
2 2 2 submit_button
3 3 3 submit_button
4 4 4 submit_button
When I submit id 1 rows.
Received Page receive value id:1 / corporate_num:1/ store_mbrno:1
When I submit id 2 rows.
Received Page receive value id:2 / corporate_num:2/ store_mbrno:2
If I understood your question correctly, the issue is that you have a lot of input fields with the same name within one form. What happens when you click one of the buttons, is that all the data in the whole form gets sent. And, because every row has fields with the same name, the values get overwritten by the next row. Therefore only the data of the last row is sent.
If you only want to send the data of one row, you need to have the data in a separate form. So, one form for each row. Since, technically, you can't have a form element directly inside a table or tr element, I suggest putting the form element and all hidden inputs in the last table cell.
Furthermore, since the contents of the hidden inputs can be modified (you can't trust your visitors not to use the inspector), you should not trust the information that is sent. Instead, if possible, only send the id, and fetch the other information anew from the database.

insert input time content in mysql type time

this is my table in html php, i get the names of employees from DB
i need to insert in the database the content of input type=time
this is my php code
<?php
$nom='';
$prénom='';
$output='';
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
$sql="SELECT * FROM employés;";
$result = mysqli_query($connect, $sql);
echo "<table>
<tr>
<th>nom</th>
<th>prénom</th>
<th>Entrée matin</th>
<th>Sortie matin</th>
<th>Entrée soir</th>
<th>Sortie soir</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "</tr>";
}
$output="</table>";
echo $output;
?>
i have no idea how to insert the content of input type='time' in mysql DB
neither with type varchar or time.
can someone help me please?
Depending on the type of your field in your database. Assuming it's a DATETIME, you could format it with a date().
echo "<td>" . "<input type='time' value='" . date("H:i", strtotime($row['entree_matin'])) . "' /></td>";
insert your time inside the 'value' parameter in the time tag
exemple :''
<input id="time" type="time" value="12:13">
but first you have to insert the right format : hh:mm

Create a drop down menu in a form being created with php

I have a form created with php to edit records in a MySQL db. I would like to add a drop down menu to this form for the "type" field but am not sure how to create it without losing the data already in the db field.
<?php
require_once '../php/dbconfig.php';
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['update'])) {
$UpdateQuery = "UPDATE members SET name='$_POST[name]', type='$_POST[type]', physicaladdress='$_POST[physicaladdress]', billingaddress='$_POST[billingaddress]', contact='$_POST[contact]', phone='$_POST[phone]', fax='$_POST[fax]', email='$_POST[email]', web='$_POST[web]', description='$_POST[description]' WHERE id='$_POST[id]'";
mysqli_query($conn, $UpdateQuery);
}
$result = mysqli_query($conn, "SELECT * FROM members ORDER BY name");
echo "<br />";
echo "<table border=0>
<tr>
<th>Name</th>
<th>Type</th>
<th>Physical Address</th>
<th>Billing Address</th>
<th>Contact Name</th>
<th>Phone</th>
<th>Fax</th>
<th>Email</th>
<th>Web</th>
<th>Description</th>
</tr>" ;
while($record = mysqli_fetch_array($result)) {
echo "<form action = admin-update.php method = post>";
echo "<tr>";
echo "<td>" . "<input type=text size=42 name=name value='" . $record['name'] . "' </td>";
echo "<td>" . "<input type=text size=30 name=type value='" . $record['type'] . "' </td>";
echo "<td>" . "<input type=text size=60 name=physicaladdress value='" . $record['physicaladdress'] . "' </td>";
echo "<td>" . "<input type=text size=60 name=billingaddress value='" . $record['billingaddress'] . "' </td>";
echo "<td>" . "<input type=text size=20 name=contact value='" . $record['contact'] . "' </td>";
echo "<td>" . "<input type=text size=10 name=phone value='" . $record['phone'] . "' </td>";
echo "<td>" . "<input type=text size=10 name=fax value='" . $record['fax'] . "' </td>";
echo "<td>" . "<input type=text size=25 name=email value='" . $record['email'] . "' </td>";
echo "<td>" . "<input type=text size=25 name=web value='" . $record['web'] . "' </td>";
echo "<td>" . "<input type=text 50 name=description value='" . $record['description'] . "' </td>";
echo "<td>" . "<input type=hidden name=id value='" . $record['id'] . "' </td>";
echo "<td>" . "<input type=submit name= update value=Update" . " </td>";
echo "</form>";
}
echo "</table>";
$conn->close();
?>
Any assistance would be greatly appreciated. Thank you.
You should be able to change the type from an input to a select with options and include the value from the database as one of the options.
I suggest this:
$selectType = '<select name="type">';
$selectType .= '<option value="'. $record['type'] .'">"'. $record['type'] .'"</option>';
$selectType .= '<option value="Opt1">Option1</option>';
$selectType .= '<option value="Opt2">Option2</option>';
$selectType .= '<option value="Opt3">Option3</option>';
$selectType .= '</select>;
Then, inside your while loop:
echo "<td>" . $selectType . "</td>";
I hope this works for you. Please let me know if it does not or if it does not answer your question.

Looking for assistance with passing multiple variables using POST command

I have the following code:
{
echo "<tr>";
echo "<td>" . $row1['supervisiondate'] . "</td>";
echo "<td>" . $row1['level'] . "</td>";
echo "<td>" . $row1['division'] . "</td>";
echo "<td>" . $row1['supervisor'] . "</td>";
echo "<td>" . $row1['totalscore'] . "</td>";
echo "<form action='refereesupervision.php' method='post'>";
echo "<td>
<input name='refereeID' value='" . $row['refereeID'] . "'/>
<input name='supervisionID' value='" . $row1['supervisionID'] . "'/>
<input type='submit' value='View'/>";
echo "</form></td>";
echo "</tr>";
} </br>
When refereesupervision.php receives the data using post for each of the two variables, only the refereeID is received. It should be noted that there are two separate queries of two separate tables that produce $row and $row1.
Looking for some guidance or references to help me understand.
Cheers
according to comment change input name from ID to supervisionID
<input name='ID' value='" . $row1['supervisionID'] . "'/>
<input name='supervisionID' value='" . $row1['supervisionID'] . "'/>
According to comments and editing original question. Answer is
$refereeID is not the same as $refereeid

Edited all entries in database [php]

I have a problem in editing data in my database. I only want to edit one entry but after I clicked edit, it shows that all the data in my database were edited.
eventlist.php
<?php
$con=mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con,"select * from events");
//echo "<a href='dashboard.php'>Home</a><br>";
echo "<table border=1 id='hor-minimalist-a' width='100%'>";
echo "<tr align='center'><td><b>Date</b></td><td><b>Event</b></td><td><b>Special Note</b></td><td colspan='2'>Options</td></tr>";
$a=0;
while($row = mysqli_fetch_array($result))
{
if($a%2==0){
echo "<tr bgcolor='#b2d5ff' width='100'>"."<td>" . $row['date'] . "</td> <td>" . $row['event'] . "</td> <td>" . $row['note'] . "</td>"
. "</td><td><a href='editevent.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deleteevent.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
}
else{
echo "<tr>"."<td>" . $row['date'] . "</td> <td>" . $row['event'] . "</td> <td>" . $row['note'] . "</td>"
. "</td><td><a href='editevent.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deleteevent.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
}
$a++;
}
echo '</table>';
echo "<center><a href='addevent.php'><button type='submit' class='button'>Add New</button></a></center>";
?>
editevent.php
<?php
while($row = mysqli_fetch_array($result))
{
echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
echo "Date: <input type='text' name='date' value='$row[date]'><br/>";
echo "Event: <textarea type='text' name='event'>".$row['event']."</textarea><br/>";
echo "Note: <input type='text' name='note' value='$row[note]'><br/>";
}
?>
updateevent.php
<?php
$id = $_POST['id'];
$date = $_POST['date'];
$event = $_POST['event'];
$note = $_POST['note'];
$con = mysqli_connect("localhost","root","root","chess");
mysqli_query($con,"update events set date='$date', event='$event', note='$note' where id = id");
header('location: eventlist.php');
?>
"update events set date='$date', event='$event', note='$note' where id = id"
where id = id is true for all rows, so all rows get updated. You probably ment to write where id = $id.
Also note that your query is open to SQL injection. Use prepared statements instead.

Categories