0

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">&nbsp;</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?

1
  • UPDATE dati SET title='$titletxt', value1='$value1', value2='$value2' WHERE ID = '' 0 row(s) updated. Commented Sep 7, 2013 at 11:20

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

When i give all names, and now i add new line, they copy first line and i cant add diferrent information and i cant edit ;/, something goes wrong with ID
Because you are doing wrong all fields are in loop and with same name you have to make a single form for each record if want to know how then let me know i will edit my answer
0
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.