Re: Update to DB
| From: | David Grant | Date: | Wed, 28 May 2003 12:33:33 +0000 |
| Subject: | Re: Update to DB | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-149235@lists.php.net to get a copy of this message | ||
Kalle Saarinen wrote:
Hello, I was just wondering how to update data to database from html text field. I have a form that retrieves data from dbase and put's values into text field in a web page. I want to be able to modify data and then save it dbase by hitting save button (also form object). ie. $sql="SELECT col1 FROM table WHERE id = 1"; $result=mysql_query($sql); $num = mysql_num_rows($result); $row = mysql_fetch_array($result); $var = $row["col1"]; echo "<form name=\"form1\" method=\"post\" action=\"\">"; echo "<input name=\"name\" type=\"text\" id=\"name\" value=\"$var1\" size=\"30\" maxlength=\"30\">"; echo "<input name=\"save\" type=\"submit\" id=\"save\" value=\"save\">"; Anyone know what is the proper way to do this? -KalleYou need to capture the values passed by the form in the script referenced in the form action attribute, using the relevant superglobal referenced in the form method attribute (e.g. $_GET or $_POST). Using your example, you would access the "name" form element like so: $_POST['name'] Then you should use this variable in an UPDATE or INSERT statement and send that to your database server, e.g.: mysql_query("UPDATE table SET col1 = " . $_POST['name'] . " WHERE id = " . $_POST['id'] . " LIMIT 1"); I would suggest reading the PHP manual a little more carefully, in particular those sections regarding variable and form handling, as well as the section on MySQL. Regards, David -- David Grant Web Developer david.grant@wiredmedia.co.uk http://www.wiredmedia.co.uk Tel: 0117 930 4365, Fax: 0870 169 7625 Wired Media Ltd Registered Office: 43 Royal Park, Bristol, BS8 3AN Studio: Whittakers House, 32 - 34 Hotwell Road, Bristol, BS8 4UD Company registration number: 4016744 ********************************************************************** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. **********************************************************************