Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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

Reply With Quote
  #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

<><
Reply With Quote
  #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.

Reply With Quote
  #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

Reply With Quote
  #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.

Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:58 AM.


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