0

I am trying UPDATE to update my data with the following code. There might be problem in UPDATE query, but I checked syntax, modified it, but still its not working. Please Help.

FORM.php

<?php
require_once 'conn.php';
$var = $_GET['q'];
$varmod = 'tid="'.$var.'"';
$query = "SELECT * FROM temptable WHERE $varmod";
$result = mysql_query($query, $db) or die(mysql_error($db));
while ($row = mysql_fetch_assoc($result)) {
    $head = $row['thead'];
    $text = $row['ttext'];
    echo "<div id='main'>";
    echo "<form action='show.php?q=".$row['tid']."' method='POST'>";
    echo "<textarea name='thead' id='thead'>$head</textarea><br>";
    echo "<textarea name='ttext' >$text</textarea><br>";
    echo "<input type='submit' value='Update' /></form></div>";
}
?>

show.php

<?php
$title = $_POST['thead']; 
$text = $_POST['ttext'];
$date = date("Y-m-d");  
require_once 'conn.php';

if(isset($title)){  
    if (isset($_GET['q'])) {
        $temp = $_GET['q'];
        $query = "UPDATE temptable SET thead=\"$title\" AND ttext=\"$text\" WHERE tid=\"$temp\"";
    }
    else{
    $query= "INSERT INTO temptable
    (thead, ttext, tdate) 
    VALUES (\"$title\", \"$text\", \"$date\")";
    }
    $result = mysql_query($query, $db) or die(mysql_error($db));
}

Well INSERT query is working well.

peeyushsrj
  • 280
  • 4
  • 10
  • are you getting any error codes? – Joseph Jun 04 '14 at 23:07
  • 3
    Your query should be like: `UPDATE temptable SET thead=\"$title\", ttext=\"$text\" WHERE tid=\"$temp\""` so.. replace "and" with comma.. – Hardy Jun 04 '14 at 23:08
  • 1
    -Phoenix Wright voice- [INJECTION!](http://xkcd.com/327/) – Niet the Dark Absol Jun 04 '14 at 23:08
  • Nope i m not gerring any errors. – peeyushsrj Jun 04 '14 at 23:08
  • 1
    That's not how you use UPDATE. Use Hardy's suggestion. – Funk Forty Niner Jun 04 '14 at 23:09
  • 1
    Of course you're not seeing errors, you're not checking for them. – O. Jones Jun 04 '14 at 23:10
  • 1
    Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` – Funk Forty Niner Jun 04 '14 at 23:10
  • You need to read up on [proper SQL escaping](http://bobby-tables.com/php) so you don’t create any more severe [SQL injection bugs](http://bobby-tables.com/) like the one you have here. Also, `mysql_query` should not be used in new applications. It's a deprecated interface that's being removed from future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and is a safer way to compose queries. `$_GET` data **never** goes directly in a query. – tadman Jun 04 '14 at 23:10
  • Thanks, Hardy. It's working now :D – peeyushsrj Jun 04 '14 at 23:10
  • 1
    Your present code is open to [**SQL injection**](http://stackoverflow.com/q/60174/). Use [**`mysqli_*` with prepared statements**](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php), or [**PDO**](http://php.net/pdo) with [**prepared statements**](http://php.net/pdo.prepared-statements). – Funk Forty Niner Jun 04 '14 at 23:11
  • Thanks to all. I will keep these points in mind :) – peeyushsrj Jun 04 '14 at 23:16
  • @Hardy You should have made that as an answer. You have my blessing ;-) – Funk Forty Niner Jun 04 '14 at 23:17

2 Answers2

1

Mysql Update queries must be like this ;

"UPDATE targettable SET column='$var1', column2='$var2' WHERE targetcolumn='$target'";

Try this it will help you..

  • 1
    [`It's already been answered/solved`](https://stackoverflow.com/questions/24049043/update-query-not-working-phpmysql#comment37078900_24049043) this is points mongering. – Funk Forty Niner Jun 04 '14 at 23:16
  • Sory, i didnt see any answer on this question marked as **correct answer**.. That is why i answered it... –  Jun 04 '14 at 23:18
  • Well, since Hardy doesn't seem to want to make it as an answer, it's all yours ;-) – Funk Forty Niner Jun 04 '14 at 23:28
0

use ,(comma) instead of AND in your update query then it will work fine.

"UPDATE temptable SET thead='".$title."', ttext='".$text."' WHERE tid='".$temp."' ";

Vivek Singh
  • 2,435
  • 1
  • 12
  • 24