Jump to content

Problem with updateing table


ale
 Share

Recommended Posts

Hi :)

 

When I am doing the table update I do not see that one article is updated called naziv_artikla. Well, the article is updated in the database when I open my phpmyAdmin, but when I am showing it on the screen, selecting all the articles from the database I do not see the update of naziv_artikla.

 

Is there problem with the hidden field? I removed it from the code, but then is displayed some error (code is not working properly). Can anybody tells me what should I fix??? Thanks :)

 

This is my code for selecting data from the database.

 

<?php

$con=mysqli_connect("localhost","root","aco123","sifrarnik");
// Provjeri konekciju
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQLi: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM artikli ORDER BY id_artikla ASC");
echo "<table border='1'>
<tr>
<th>Id artikla</th>
<th>Naziv artikla</th>
<th>Cijena artikla</th>
<th>Na stanju</th>
<th>Ukupno u KM</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<form action = update1.php method = post>";
echo "<tr>";
echo "<td>" . $row['id_artikla'] . " </td>";
echo "<td>" ."<input type = text name = naziv_artikla value = ". $row['naziv_artikla'] . " </td>";
echo "<td>" ."<input type = text name = cijena_artikla value = ". $row['cijena_artikla'] . " </td>";
echo "<td>" ."<input type = text name = na_stanju value = ". $row['na_stanju'] . " </td>";
echo "<td>" . $row ['cijena_artikla'] * $row ['na_stanju'] . " </td>";
echo "<td>" ."<input type = hidden name = hidden value = ". $row['id_artikla'] . " </td>";
echo "<td>" ."<input type = submit name = update value = Update>";
echo "</tr>";
echo "</form>";
}
$ukupno_u_km = $row ['cijena_artikla'] * $row ['na_stanju'];
mysqli_close($con);
?>
And this is my update code.
<?php
$servername = "localhost";
$username = "root";
$password = "aco123";
$dbname = "sifrarnik";
// Kreiranje konekcije
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Provjera konekcije
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Sql upit za update podataka
$sql = "UPDATE artikli SET naziv_artikla = '$_POST[naziv_artikla]', cijena_artikla = '$_POST[cijena_artikla]', na_stanju = '$_POST[na_stanju]' WHERE naziv_artikla = '$_POST[hidden]'";
mysqli_query ($conn, $sql);
// Provjera upita
if (mysqli_query($conn, $sql)) {
echo header ("Location: tabela_sa_podacima1.php");
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Edited by ale
Link to comment
Share on other sites

shouldn't this

 

WHERE naziv_artikla = '$_POST[hidden]'";

 

be

 

WHERE id_artikla = '$_POST[hidden]'";

 

I take this as being a id ref being placed in hidden input, so i would assume it should reference the same when updating.

 

You won't get any error message cause there is no matching record of title against id, so it is not a error, the record does not exist.

Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...