Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
| Search | Today's Posts | Mark Forums Read
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 28th, 2004, 09:49 AM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

  #2 (permalink)  
Old April 28th, 2004, 10:07 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

Snib

<><
  #3 (permalink)  
Old April 28th, 2004, 10:23 AM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

  #4 (permalink)  
Old April 28th, 2004, 10:25 AM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

  #5 (permalink)  
Old April 28th, 2004, 09:03 PM
Registered User
 
Join Date: Apr 2004
Location: Knoxville, TN, .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #6 (permalink)  
Old April 28th, 2004, 09:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
I solved insert query.now see this Update Query. amit_mande@yahoo.com VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Global Varibles? Sibrovvs Access VBA 4 December 18th, 2004 11:36 AM
PHP varibles in FLASH Ashleek007 Beginning PHP 1 November 30th, 2004 12:44 PM
How to update Mysql version qazi_nomi MySQL 1 September 8th, 2004 09:50 AM
How to update Mysql version qazi_nomi MySQL 1 June 14th, 2004 02:45 PM





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