Reputation: 319
I want to edit single line not all, but now i add ID and now he do nothing, nothing change :(, before i give him ID, they edit all rows.
variables
$titletxt = ($_POST['title_txt']);
$value1 = ($_POST['value1_txt']);
$value2 = ($_POST['value2_txt']);
$ID = ($_POST['id']);
<?php
if( isset($_POST['edit']) ){
$con=mysqli_connect("localhost","root","","hanza");
if (mysqli_connect_errno()){
echo "Neizdevas savienoties ar MySQL: " . mysqli_connect_error();
}
$sql="UPDATE dati SET title='$titletxt', value1='$value1', value2='$value2' WHERE ID = '$ID'";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
echo '<script>
alert(" Ieraksts ir veiksmigi labots! ");
window.location.href = "index.php";
</script>';
mysqli_close($con);
}
?>
That code is from form and i show results from database.
<form action="insert.php" method="post">
<table>
<tr>
<td class="td_opt">Title</td>
<td class="td_opt">Value 1</td>
<td class="td_opt">Value 2</td>
<td class="td_opt"> </td>
</tr>
<tr>
<input type="hidden" name="id" value="">
<td class="td_title"><input type="text" name="title_txt" value=""></td>
<td class="td_value"><input type="text" name="value1_txt" value=""></td>
<td class="td_value"><input type="text" name="value2_txt" value=""></td>
<td class="td_value"></td>
</tr>
<?php
$con=mysqli_connect("localhost","root","","hanza");
if (mysqli_connect_errno()) {
echo "Neizdevas savienoties ar MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM dati");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<input type='hidden' value='" . $row['ID'] . "'>";
echo "<td><input type='text' value='" . $row['title'] . "'></td>";
echo "<td><input type='text' value='" . $row['value1'] . "'></td>";
echo "<td><input type='text' value='" . $row['value2'] . "'></td>";
echo "<td><button name='edit' class='frm_btns'>Edit</button></td>";
echo "</tr>";
}
mysqli_close($con);
?>
</table>
<div class="atp"></div>
<button class="frm_btns" name="pievienot">New</button> // add new row
</form>
Maybe some variable is not corect to edit only one row?
Upvotes: 0
Views: 2714
Reputation: 1235
<input type="hidden" name="id" value="">
in this line there is no value is set for the argument id.
Please make sure you are setting the proper value for "id" before the form submit. (Using javascript)
Also I have noticed that you are using a button to edit. So make sure you are invoking a click event to set id and submit form.
As a suggestion you have to specify a value attribute for each row button. So it will be easy to retrieve the currently clicked "row id" by using getAttribute() property.
<button name='edit' class='frm_btns' value='" . $row['ID'] . "'>Edit</button>
Upvotes: 0
Reputation: 64496
The problem is in your html you have used button the values of type button will not send to server instead you need to use type="submit"
echo "<td><button name='edit' class='frm_btns'>Edit</button></td>";// is wrong
Therefore it is not passing your if check because $_POST['edit']
is not set
if(isset($_POST['edit'])){.... }
change button to submit
echo "<td><input name='edit' class='frm_btns' type='submit' value='Edit'/></td>";
Also you have missed to add the names to your input
elements you just have assigned the values, name them also like
echo "<input name='id' type='hidden' value='" . $row['ID'] . "'>"; ...for remaining elements also
Upvotes: 1
Reputation: 4506
what i can see . you have not given any name to
// echo "<input type='hidden' value='" . $row['ID'] . "'>";
so you will not get value in $_POST['id']
this element it should be name as id.
before update you should check if id is not blank.
Upvotes: 1