If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > sql help needed

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-09, 04:19
azeemaqsood azeemaqsood is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
sql help needed

The scenario was like I had a customer apllication form for plots. Mostly getting the customer data and it has a droopdown field showing the available plots from the plots table where plots.clients_id='NULL' and status='Null'.

Now after the customer has selected a plot i want to store the client_id from the session variable into the plots.client_id (a foreign key from clients table) where plots.plot_id=selected plot id from the form.

The query i am trying to run does not store the value of the client id into the plots table. The query is as follows:

<?php
include("connect2db.php");
$plot_id=$_POST['plot_select'];
//echo $plot_id; //the value is printed
$client_id= $_SESSION['id'];
//echo $client_id;
$query="INSERT INTO plots(client_id) VALUES ($client_id) where plots.plot_id='$plot_id'";
$result=mysql_query($query);
?>


Please help...thanks is advance...
AZEEM
Reply With Quote
  #2 (permalink)  
Old 06-13-09, 06:13
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by azeemaqsood
Please help...thanks is advance...
  • When you compare against a null value it's done like this:
    Code:
    where plots.clients_id is NULL and status is Null
    not like this:
    Code:
    where plots.clients_id='NULL' and status='Null'
  • When you run any mysql function (doing the connection, going to the database and the running the SQL) it's a good idea to test if the function succeeded or not using the or die command:
    Code:
    $result=mysql_query($query) or die( "Problem with sql: $query" );
  • It would also be a good idea to print out $query and try running the SQL in your database by hand. If it fails then you can see immediately what's wrong.
Reply With Quote
  #3 (permalink)  
Old 06-13-09, 07:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i can tell you right now what's wrong, you won't have to test your SQL (although that ~is~ good advice -- always test outside of php first)

you have:
Code:
INSERT INTO plots(client_id) VALUES ($client_id) where plots.plot_id='$plot_id'
the INSERT VALUES statement does not allow a WHERE clause

you probably want UPDATE, not INSERT

check da manual for the correct UPDATE syntax

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 06-13-09, 11:01
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Ooops - not sure how I missed that glaring mistake. Well spotted.
Reply With Quote
  #5 (permalink)  
Old 06-14-09, 04:53
azeemaqsood azeemaqsood is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
the problem is solved..

Hi guys !

Thanks a lot for your solution...
The problem is resolved...

Thanks
Azeem
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 On
Pingbacks are On
Refbacks are On