0

I am trying to update mysql table with AJAX and php script, but I am still receiving "304 Not modified" message. I have searched many questions here and also searched for it with google but without any luck... I am 100% sure all variables are sent with AJAX and received by PHP in correct form, I have checked it like this:

$tabulka=$_SESSION["uzivatel"];
$slovensky=testVstupov($_POST["slovensky"]);
$prelozene=testVstupov($_POST["prelozene"]);
$druh=testVstupov($_POST["druh"]);
$lekcia=testVstupov($_POST["lekcia"]);
$reset=testVstupov($_POST["reset"]);
$id=testVstupov($_POST["id"]);
$nula=0;
//echo $tabulka."#".$slovensky."#".prelozene."#".$druh."#".$lekcia."#".$reset."#".$id."#".$nula;

function testVstupov($data)
{
    $data=trim($data);
    $data=stripslashes($data);
    $data=htmlspecialchars($data);
    return $data;
}

that echo is just to make sure all variables are filled with correct value

Then I try to update table, but it results with 304 message, variable $hodnoty is returned empty:

$pripojenie=mysqli_connect("localhost","user","password","database");

if (mysqli_connect_errno($pripojenie))
{
echo "Zlyhalo pripojenie do databázy: ".mysqli_connect_error();
}
if($reset == "false")
{
    $hodnoty = mysqli_query($pripojenie, "UPDATE `$tabulka` SET slovensky=$slovensky, preklad=$prelozene, druh=$druh, lekcia=$lekcia WHERE id='$id'");
}

if(mysqli_error()) 
{ 
die('Zápis do databázy sa nepodaril. Chyba: ' . mysqli_error() ); //skjurajozimy.
}
//ukncenie spojenia s databazou
mysqli_close($pripojenie);

Same code in other php file but only to modify different collumns in same table works. This code works:

$pripojenie=mysqli_connect("localhost","user","password","database");

if (mysqli_connect_errno($pripojenie))
{
echo "Zlyhalo pripojenie do databázy: ".mysqli_connect_error();
}

$vysledok=mysqli_query($pripojenie, "SELECT skusane,spravne FROM `$tabulka` WHERE preklad='$data1'");
if(mysqli_error()) 
{ 
die('Výpis z databázy sa nepodaril. Chyba: ' . mysqli_error() ); //`skjurajozimy`.
}

$hodnoty = mysqli_fetch_array($vysledok);
$noveSkusane = $hodnoty['skusane']+1;
$noveSpravne = $hodnoty['spravne']+$data2;
$uspesnost = round($noveSpravne*$noveSkusane);

$hodnoty = mysqli_query($pripojenie, "UPDATE `$tabulka` SET skusane=$noveSkusane, spravne=$noveSpravne, uspesnost=$uspesnost WHERE preklad='$data1'");
if(mysqli_error()) 
{ 
die('Zápis do databázy sa nepodaril. Chyba: ' . mysqli_error() ); //`skjurajozimy`.
}
//ukncenie spojenia s databazou
mysqli_close($pripojenie);

collumns in my table are: id slovensky preklad druh lekcia skusane spravne uspesnost , where id is autoincrement.

Please, any suggestions, why update code doesn't work?

Juro Ozimy
  • 33
  • 5
  • Is `session_start();` inside ALL your files and as the first line? – Funk Forty Niner Oct 15 '13 at 12:39
  • yes, I have, because I am distributing table name via $_SESSION["uzivatel"] variable – Juro Ozimy Oct 15 '13 at 13:45
  • Try `$hodnoty = mysqli_query($pripojenie, "UPDATE $tabulka SET skusane='$noveSkusane', spravne='$noveSpravne', uspesnost='$uspesnost' WHERE preklad='$data1'");` SO is playing tricks on me for the backticks, so place `$tabulka` inside backticks and try that. – Funk Forty Niner Oct 15 '13 at 13:51
  • Also try `SET slovensky='$slovensky'` and do the same for the other variables, putting them inside single quotes as I did here, and above. – Funk Forty Niner Oct 15 '13 at 13:53
  • I have tried maybe all combinations of ticks but no change. Thing is, the code `$hodnoty = mysqli_query($pripojenie, "UPDATE `$tabulka` SET skusane=$noveSkusane, spravne=$noveSpravne, uspesnost=$uspesnost WHERE preklad='$data1'");` does work, only code `$hodnoty = mysqli_query($pripojenie, "UPDATE `$tabulka` SET slovensky=$slovensky, preklad=$prelozene, druh=$druh, lekcia=$lekcia WHERE id='$id'");` does not. I am really confused because they are totally same, only variables are different – Juro Ozimy Oct 15 '13 at 14:26
  • Then try to rename one of the `$hodnoty` to something else. – Funk Forty Niner Oct 15 '13 at 14:42
  • I think the problem is this `$id=testVstupov($_POST["id"]);` - it shouldn't be declared because of your `id` column being autoincrement. It could be part of the problem. Other than that, I don't know where the error is. – Funk Forty Niner Oct 15 '13 at 14:50
  • @Fred-ii- thank you, you were right. finally I have solved it, it was problem of ticks and spaces (!!!) as you suggested. I must post it as separate answer, because so many thicks will mess up this comment – Juro Ozimy Oct 17 '13 at 12:49

2 Answers2

0

Have you tried:

header( 'Cache-Control: no-store, no-cache, must-revalidate' ); 
header( 'Pragma: no-cache' ); 

at the beginning of your PHP function?

ex3v
  • 3,518
  • 4
  • 33
  • 55
0

Finally solution, problem was with ticks and spaces, so correct code here:

$hodnoty = mysqli_query($pripojenie, "UPDATE  `$databaza`.`$tabulka` SET  `slovensky` =  '$slovensky',  `preklad` = '$prelozene',  `druh` = '$druh',  `lekcia` = '$lekcia'  WHERE  `$tabulka`.`id` =$id;");
Juro Ozimy
  • 33
  • 5