p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   MySQL (http://p2p.wrox.com/forumdisplay.php?f=99)
-   -   How to do a mysql update using varibles in a query (http://p2p.wrox.com/showthread.php?t=12371)

Alex_forever April 28th, 2004 09:49 AM

How to do a mysql update using varibles in a query
 
every time I use this:

$query = "UPDATE AddTable2 SET Code='$c', Description='$d', Name='$n' WHERE Id=$id";
$result = @mysql_query($query) or die (mysql_error());
//new screen
if ($result)// it ran
{
    echo '<p>You have updated your information</p>';
    include ('WSfooter.inc'); // Include the HTML footer.
    exit(); // Quit the script.
}
else
{
    $message = '<p>Something is wrong</p><p>'. mysql_error() . '</p>';
}

mysql_close(); // Close the database connection.
}

I get this:
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 '' at line 1

Someone please help


Snib April 28th, 2004 10:07 AM

Is the value of $id the name of a field?

Snib

<><

Alex_forever April 28th, 2004 10:23 AM

it is now working however, when it goes through the data that I updated stays the same.


Alex_forever April 28th, 2004 10:25 AM

here is the code:
if ($c && $d && $n)
    {//do database again
         require_once ('mysql_connect.php');

         $query = "UPDATE AddTable2 SET Code='$c', Description='$d', Name='$n' WHERE Id='$Id'";
        $result = @mysql_query($query) or die (mysql_error());
        //new screen
            if ($result)// it ran
            {
                echo '<p>You have updated your information</p>';
                include ('WSfooter.inc'); // Include the HTML footer.
                exit(); // Quit the script.
            }
            else
            {
                $message = '<p>Something is wrong</p><p>'. mysql_error() . '</p>';
            }

            mysql_close(); // Close the database connection.
    }
}

And no $id is not a field it is a row that I want php to search


James Davies April 28th, 2004 09:03 PM

Just a guess, since your code never mentions it, but perhaps $Id has not been set, or is not set to anything that it matches in the table. That's the only reason I can see why it wouldn't update the results. An update won't fail just because it doesn't update anything. Just a possibility.


happygv April 28th, 2004 09:34 PM

Alex, (Sorry I was off for the psat couple of days, So couldn't replay any of the posts here)

You would curse yourself, seeing the solution. You have missed the concatenation operator inbetween;)

$query = "UPDATE AddTable2 SET Code='" . $c . "', Description='" . $d . "', Name='". $n . "' WHERE Id='" . $Id . "'";

NOTE: Something to remember always, Do not use Single Quotes around the VARIABLE, if its DATATYPE is of NUMERIC set in the DATABASE.

May be you can alter the above said query, as I am not sure about the Datatypes of each column there.

PS: I will be online for next 5 days (Early morning IST).

Cheers!

-Vijay G


All times are GMT -4. The time now is 12:19 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.