-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>
deividas
  • 7
  • 4

1 Answers1

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.

Manu
  • 185
  • 4
  • 11
  • 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 – deividas May 28 '17 at 12:04
  • 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. – Manu May 28 '17 at 12:14
  • 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 – deividas May 28 '17 at 12:38
  • Try printing out `print_r($_POST)` to see what exists in there. – Manu May 28 '17 at 12:49
  • " Array ( ) " I'm not sure – deividas May 28 '17 at 12:54
  • Well silly me...! I just re-checked your main.php and you are actually not sending a POST request...You will have to use GET instead of POST at your update.php code... – Manu May 28 '17 at 13:31
  • Alright, now I'm only getting Notice: Undefined index: sandely with Array ( [id] => 1 ) if I choose my first row. – deividas May 28 '17 at 13:52
  • You are not passing `sandely` as a url parameter that's why. You are just passing the `id`: `update.php?id=....`. The way you are trying to achieve things would need JavaScript or to turn this into a mini-form so you can send a POST request instead. Start a new question for converting your code. – Manu May 28 '17 at 14:02
  • I don't understand, so you're saying that I can't simply update my table with the code I have? What's the difference then between "insert" and "update" in terms of code? My "insert" code looks similar and works fine..[My code of insert](https://stackoverflow.com/questions/44145618/php-sql-inserting-data-to-sql) – deividas May 28 '17 at 14:16
  • Does your INSERT code include a **form** that is submitted? – Manu May 28 '17 at 14:26
  • That's the difference as you can see. When you INSERT you have a form that you submit and the you are sending a POST request. On the other hand when you update you do not have a form and you try to do that by adding the variables as url parameters. You will have to modify your main.php to have a form for each row. Try something like that https://stackoverflow.com/questions/4035966/create-a-html-table-where-each-tr-is-a-form – Manu May 28 '17 at 15:14