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 > Data Access, Manipulation & Batch Languages > PHP > Updating DB using PHP form

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-10, 10:27
zeroge zeroge is offline
Registered User
 
Join Date: Dec 2009
Location: Now in China and since the past 7 years in China
Posts: 51
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 ..
Quote:
<?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 ...
Quote:
$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"
Reply With Quote
  #2 (permalink)  
Old 01-04-10, 10:21
zeroge zeroge is offline
Registered User
 
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"
Reply With Quote
  #3 (permalink)  
Old 01-04-10, 15:25
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
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.
Reply With Quote
  #4 (permalink)  
Old 01-04-10, 16:00
zeroge zeroge is offline
Registered User
 
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"
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