SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    pass
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Roll up yer sleeves, syntax problems

    For the practice, I started creating a customer profile database.
    I've been troubleshooting syntax errors for 2 days and I came here as a last resort. There is something wrong with my sql query, but I just cant see it.

    I lost hope with this, so I named the script and wrote it a character persona.
    It didn't help.


    PHP Code:

    <?php
    //this script prints to screen the client database
    //and allowes for editing


    // login to mysql database
    $dbcnx mysql_connect('localhost''jamie_mast''pass');
        if (!
    $dbcnx) {
            echo 
    mysql_error();
        }

    // connect to database    
    !mysql_select_db('jamie_clt');
        echo 
    mysql_error();
        
    // greeting
    echo '<p>Adrian: welcome back Jamie, Im ready to be accessed </p>';

    // echo data base contence
    $result mysql_query('SELECT * FROM clients');
        if (!
    $result) {
            echo 
    '<p> Im sorry jamie, I have a mysql select error </p>';
            echo 
    mysql_error();
            exit();
            }

    // take result set data and turn it into an array
    // then declare that array as $row
    while ($row mysql_fetch_array($result)) {
        echo 
    '<p>' $row['id'];
        echo 
    ' | ' $row['name'];
        echo 
    ' | ' $row['mthp'];
        echo 
    ' | ' $row['lastpm'];
        }

    //form inputs: row id, column to update, and updated data
    ?>
    <html>
    <body>
    <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <p>Amend which id?
           <input name="amdid" type="text" id="amdid">
    </p>
         <p>Amend which column?
           <input name="amdcolumn" type="text" id="amdcolumn">
    </p>
         <p>
           amend data
            <input name="newdat" type="text" id="newdat">
         </p>
         <p>
           <input type="submit" name="Submit" value="Submit">
         </p>
    </form>
    </body>
    </html>
        
    <?php

        
    //load the mysql update command if form data is available
        
    if ($formdat $_POST['newdat']) {
        
        
    // declare the variables for the form data     
        
    $newdat $_POST['newdat'];
        
    $amdcolumn $_POST['amdcolumn'];
        
    $amdid $_POST['amdid']; 
        
    count($result);

    $sql "UPDATE clients SET 
           '
    $amdcolumn'='$newdat' WHERE ID ='$amdid'";
        
            if (
    mysql_query($sql)) {
                echo 
    '<p>Adrian: I amended the indicated data, Jamie </p>';
                } else { 
                echo 
    '<p> Adrian: I have an error in processing.</p>  '
                 echo 
    mysql_error();
                }
        }
            echo 
    '<p> Adrian: I am ready for commands. . . </p>';
            
    //mem dump to check if vars are working
            
    echo "$newdat  $amdcolumn   $amdid ";
         
            
    ?>

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry jamie, I don't like my table names in quotes:
    PHP Code:
    $sql "UPDATE clients SET '$amdcolumn'='$newdat' WHERE ID ='$amdid'"
    jamie, why are you so negative?
    PHP Code:
    !mysql_select_db('jamie_clt'); 

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't like my table names in quotes:
    ....you can however put field names in backquotes (the character beside the 1).
    If you ever have a MySQL field called `order` for example, you'll need to use the back quotes to differentiate the field name from the ORDER statement of a query.

  4. #4
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Typically I would do:
    PHP Code:
     $sql "UPDATE clients SET $amdcolumn=\"$newdat\" WHERE ID =\"$amdid\""
    And the make sure double quotes are escaped in $newdat.

    If you are going to use backticks, then escape them and make sure you read the manual about what they can do in PHP.

  5. #5
    SitePoint Enthusiast SlayMan's Avatar
    Join Date
    Oct 2004
    Location
    Germany/ Hamburg
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First Suggestion by me would be:
    PHP Code:
    $result mysql_query('SELECT * FROM clients'); 
    replaced with:
    PHP Code:
    $result mysql_query('SELECT * FROM clients';); 
    see? in my statement there is a semicolon, which is very useful for querying databases
    PHP Code:
    "UPDATE clients SET 
           
    $amdcolumn=\"$newdat\" WHERE ID =\"$amdid\";"
    again that semicolon!
    SlayMan,
    Your friendly neighbourhood trainee and webmaster
    go, see for yourself @ http://www.alpupp.de

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    again that semicolon!
    No, it's not needed in this context. And in fact your first example won't parse.

  7. #7
    SitePoint Member ravikiran's Avatar
    Join Date
    Dec 2004
    Location
    hyderabad
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    make some change in update statement

    Hey,

    Try

    $sql = "UPDATE clients SET
    '$amdcolumn'='$newdat' WHERE ID ='$amdid'"


    as:

    $sql = "UPDATE clients SET
    $amdcolumn='$newdat' WHERE ID ='$amdid'"


    means u need not to enclose the field name in quotes, and need not give the semicolon at the end of query statement. Php will pass the query to database and that will be executed.
    Ravi

  8. #8
    SitePoint Member
    Join Date
    Jan 2005
    Location
    pass
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect. She's happy to be working just right now.
    Since I took the quotes off the field names except for $newdat like you all said.

    I was and probably still will be a bit frustrated with PHP.
    I finished highshool with c++ and I thought I would have a bit of an edge with PHP. No such luck.
    The two are like catholic and Luthern.


Bookmarks

Posting Permissions

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