0
<?php 

  require('dbconnect.php');

  $indexno = $_POST['indexno'];
  $cevap = $_POST['cevap'];
  $cevapdate = gmdate("Y-m-d\TH:i:s\Z");
  $query = "UPDATE soru 
               SET cevap = '$cevap', 
                   cevapdate = '$cevapdate' 
             WHERE `index` = '$indexno'";

$link = mysql_query($query);
if(!$link) {
  die('not worked: ' . mysql_error());
} else {
  mysql_close($con);    
  echo 'worked';
}

?>

Outcome of this php code is "Worked." but there is no change in the database. The thing is Im trying to update the cevap and cevapdate fields on a row by index id.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
user552828
  • 769
  • 1
  • 8
  • 17
  • There should be no quotes for `index`. Try removing it – Balanivash Jul 05 '11 at 20:28
  • The field names shouldn't be wrapped in quotes. Try removing them around the "index" field in the `WHERE` statement. **Note: For safety reasons, you should use `mysql_real_escape_string` when using user input in SQL queries.** – Francois Deschenes Jul 05 '11 at 20:28
  • You also can use `mysql_affected_rows` to see if it actually did anything. Your test is just to see if it threw an error. – ldg Jul 05 '11 at 20:29
  • @Francois Deschenes: The OP knows - see: http://stackoverflow.com/questions/6588135/mysql-error-about-updating-table – OMG Ponies Jul 05 '11 at 20:30
  • I tried removing quotes from index but, It did not work that way. It gave my mysql error. This code doesnt give a error but I cant see adifference my database, something weird?! – user552828 Jul 05 '11 at 20:31
  • Make sure the criteria filtered by the where clause actually match some of the rows. Try "SELECT * FROM soru WHERE 'index' = '$indexno'" – Joey C. Jul 05 '11 at 20:32
  • He needs the ` around index because index is a keyword. – Amir Raminfar Jul 05 '11 at 20:38
  • @Amir Raminfar solved the problem, thank you. – user552828 Jul 05 '11 at 20:40
  • Failing to update rows because your WHERE clause didn't allow anything to match is NOT an error condition, so your `if (!$link)` is not a valid way to check for success. That would only work properly if your query had a syntax error. – Marc B Jul 05 '11 at 20:42

2 Answers2

0

You need to remove the single quotes from aroud the index. You should not put single quotes around a column name while writing a query. Write your query this way -

$query = "UPDATE soru SET cevap = '$cevap', cevapdate = '$cevapdate' WHERE index = '$indexno'";
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • its not working that way, I tried that way, it gave me a mysql error. This code doesnt give a error but I cant see a change in my database. – user552828 Jul 05 '11 at 20:29
  • Actually, it'dbe helpful if you post the error you get when running this query, cos, quotes should not be used for the `index` field in the table – Balanivash Jul 05 '11 at 20:30
  • @user552828: What does the error say? Also, try printing the values of the `$_POST['indexno']` and `$_POST['cevap']`. – MD Sayem Ahmed Jul 05 '11 at 20:31
  • I printed they are correct. when I remove quotes it says You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index = '2'' at line 1 – user552828 Jul 05 '11 at 20:33
0

You have to escape your rows/table with backticks, not single-quotes.

$query = "UPDATE `soru`
  SET `cevap` = '$cevap', `cevapdate` = '$cevapdate'
  WHERE `index` = '$indexno'";

Also, you should escape your user input to prevent SQL injections.

kba
  • 19,333
  • 5
  • 62
  • 89
  • They both are still the same. – MacMac Jul 05 '11 at 20:31
  • You must be making a mistake somewhere else - perhaps you're editing the wrong file or similiar. If the code you paste above is correct, that _should_ have given you an error. Nonetheless, it should work now as well. Are you sure there are any entries in your database where `index` = '$indexno. Perhaps `$indexno` doesn't contain what you think it does. – kba Jul 05 '11 at 20:33
  • You don't _have_ to use backticks. – Nahydrin Jul 05 '11 at 20:34
  • @Dark Slipstream: Normally you don't, but `index` is a reserved keyword, so here you do. Under any circumstances, using ' as he did before won't work. – kba Jul 05 '11 at 20:35