-1

I'm trying to update records in my table, but I'm not sure where I'm missing to mention my indexes in the code. I do believe that my connection is correct, although it should fetch indexes then. I've done the same for insert function for connecting the table, everything works.

This is my update.php

<?php
$connect = mysqli_connect("localhost", "root", "", "produktai");
{
$sql = "UPDATE dazai SET sandely='" . $_POST['sandely'] ."' WHERE id='" . $_POST['id'] ."'";

  if(!mysqli_query($connect,$sql)) 
   {
      echo "pakeista"; 
    }
      else {
      echo "nepakeista" . mysqli_error($connect);
    } 
}
header("refresh:10; url=Dazai.php");
?>

Ok, so I modified my main.php into this. I'm getting this error

Array ( [sandely] => 0 [Keisti] => Keisti ) What could be wrong. The 0 changes to whatever digit i put in.

  <div class="prekiu_lentele">
  <table class="lenetele_prekems">
    <form action="update.php" method="post">
  <tr>
    <th>ID</th>
    <th width="30%">Prekes pavadinimas</th>
    <th>Gamintojas</th>
    <th>Spalva</th>
    <th>Kiekis</th>
    <th>Blizgumas</th>
    <th width="10%">Kaina</th>
    <th>Kategorija</th>
    <th width="20%">Kiekis sandelyje</th>
    <th>Ištrinti</th>
    <th width="20%">Pakeisti</th>

  </tr>


</div>

<?php
$query = "SELECT * FROM dazai ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>

  <tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['pavad'];?></td>
    <td><?php echo $row['Gamintojas'];?></td>
    <td><?php echo $row['Spalva'];?></td>
    <td><?php echo $row['Kiekis'];?></td>
    <td><?php echo $row['Blizgumas'];?></td>
    <td><?php echo $row['Kaina'];?>€</td>
    <td><?php echo $row['Kategorija'];?></td>
    <td><input type="text" style="width: 50px; margin-right:10px;"  name="sandely" value="<?php echo $row['sandely'];?> "</td>
    <td><a href="delete.php?recordID=<?php echo $row['id'];?>">X</a>
    <td><input type="submit" name="Keisti" value="Keisti" /></td>

  </tr>
</div>
</form>
4
  • The logic in your update query doesn't make sense Commented May 28, 2017 at 11:54
  • First of all use print_r to check what you are getting in POST array, the you can proceed further action. It"s good to know what data available for process. I am sure you will fix error by urself easily. ... Commented May 28, 2017 at 12:51
  • " Array ( ) " I'm not getting anything there. Commented May 28, 2017 at 12:57
  • href does not have a method, forms do and I don't see a form anywhere. Commented May 28, 2017 at 15:02

1 Answer 1

1

You are sending a request with ID at

<a href="update.php?ID=<?php echo $row['id'];?>" method=post>

while you are trying to retrieve id at

$sql="UPDATE dazai SET sandely='$_POST[sandely]' WHERE id='$_POST[id]'";

That is happening due to the fact that string keys are case sensitive.

Quick-n-dirty example: https://ideone.com/KFMD4j

PS:

  1. mysqli_select_db($connect, "produktai"); is not needed as you already define the same database for use at the connection mysqli_connect("localhost", "root", "", "produktai");

  2. In addition, be aware that PHP will throw a warning if you use $_POST[sandely] as is, since it will need to convert sandely to string as you have not defined a sandely constant variable. Use $_POST['sandely'] instead. In that case I would suggest using this query instead:

    $sql = "UPDATE dazai SET sandely='" . $_POST['sandely'] ."' WHERE id='" . $_POST['id'] ."'";

    Read more at Is it okay to use array[key] in PHP?

  3. You should use GET instead of POST as you are not using a means for a POST request but merely attaching variables at the url.

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

11 Comments

Thanks for pointing out those mistakes, but I still have no clue why it tells me these errors Notice: Undefined index: sandely in C:\xampp\htdocs\Saitas\update.php on line 6 Notice: Undefined index: id in C:\xampp\htdocs\Saitas\update.php on line 6
These are probably warnings caused by the use of sandely as a constant instead of a string as "sandely" (same for id). PHP converts it to string but gives you this warning. I edited my answer to add this as well.
No luck so far, it keeps giving me error on Undefined index: for both id and sandely. I've changed the code you gave me aswell
Try printing out print_r($_POST) to see what exists in there.
" Array ( ) " I'm not sure
|

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.