Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Location
    Now in China and since the past 7 years in China
    Posts
    51

    Unanswered: Updating DB using PHP form

    Hi

    I have a form that would allow me to manually insert/populate our database with relevant data from our clients. This form works great. here is an extract ..
    <?php

    include '../../../anme/config.php';
    include '../../anme/opendb.php';

    $first_name=$_POST['first_name'];
    $last_name=$_POST['last_name'];
    $nickname=$_POST['nickname'];

    $query = "INSERT INTO clients (first_name,last_name,nickname) VALUES ('$first_name','$last_name','$nickname')";
    mysql_query($query);

    mysql_close();
    ?>
    The thing is, I want to use the same structure now to UPDATE and previous entered data.

    For this, I have changed the actual form to having 2 (TWO) input fileds for every data. meaning - in text field "1" I'd put in the current data and in text field "2" of the form, I put in the data as it should appear after the update.

    Hence, I changed the SQL "insert" command to "update" and extended the code accordingly but I seem to have some php syntax errors (I am not a php wizzard neither)

    Here is the bit of "query" code I changed from the above code to possibly achieve an update ...
    $query = "UPDATE clients (first_name,last_name,nickname) WHERE ('$first_name','$last_name','$nickname') SET (first_name,last_name,nickname) WHERE ('$first_name2','$last_name2','$nickname2')";
    Logically id wouldn't work, since '$first_name2','$last_name2','$nickname2' are not included in the above $Post array. But this is what I don't know how to add or would I need some "echo" statement instead. (as I said, I am still learning and with your help I will get there, for sure!)

    Thanks a lot for your help
    "Defying critics is a disagreement to growth through improvement"

  2. #2
    Join Date
    Dec 2009
    Location
    Now in China and since the past 7 years in China
    Posts
    51
    OOPS ... when I said "solved" it my other post it didn't include this one ...

    Would you like to see a copy of the form?

    I'd also like to have a warning popping up to ask
    "ARE YOU SURE YOU WANT TO DELETE/UPDATE THIS ENTRY?"
    before it actually goes ahead and messes me up because I was too fast with my idea ...

    ANYONE CAN HELP? ... P L E A S E
    "Defying critics is a disagreement to growth through improvement"

  3. #3
    Join Date
    May 2008
    Posts
    277
    The only error I see is your SQL statement. You really should review the documentation:

    Code:
    UPDATE clients 
    SET first_name = '$first_name2',
        last_name = '$last_name2',
        nickname = '$nickname2'
    WHERE
        first_name = '$first_name'
        AND last_name = '$last_name'
        AND nickname = '$nickname'
    From the php documentation, it appears you really should be using the mysqli interface (as opposed to the older mysql extension).

    I also hope your actual application is validating and escaping all user input before using it in SQL statements, otherwise you are leaving yourself wide open to SQL injection.

  4. #4
    Join Date
    Dec 2009
    Location
    Now in China and since the past 7 years in China
    Posts
    51
    Thank you for your reply!
    ...OOPS ... misunderstanding

    The sample of the page you see is the actual for I use for inserting data. This page works smoothly.

    However, I'd like to keep the format and turn it into a form that would first allow me to type any "Search" into a text filed and - depending on the search return (within the table) the form would be populated with the corresponding "row data"

    I's then choose the fields which would need to be updated, type in the update value, press submit and then the row would be updated.

    And yes, I use exactly the SQL string as you pointed out. What I am missing is how to actually echo the search result into the form fields so I can view/check the current data and update the once I want/need.

    I am very close , but I guess my PHP is far too week. I played around, added the search text-field, I get the connection to MySQL but when I press the "GET"button nothing happens ... this experimental page is here

    Can I post my code here?

    Thanks
    "Defying critics is a disagreement to growth through improvement"

Posting Permissions

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