Results 1 to 5 of 5

Thread: sql help needed

  1. #1
    Join Date
    Jun 2009
    Posts
    2

    Unanswered: 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

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Ooops - not sure how I missed that glaring mistake. Well spotted.

  5. #5
    Join Date
    Jun 2009
    Posts
    2

    the problem is solved..

    Hi guys !

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

    Thanks
    Azeem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •