Languages :: PHP :: update table, connect to DB |
|||
| By: FiatLink |
Date: 12/09/2003 00:00:00 |
Points: 300 | Status: Answered Quality : Excellent |
|
<?php #how can I update data from here to a MySQL Database # # if (!$_POST) die("This Zone can only be accessed from a form"); ob_start(); // turn on output buffering so we can redirect later $foo = $_POST['foo']; // do something with $foo here, how can I insert data into or post or update to a specific // database, to a specific record. // now redirect the user to the appropriate page switch($_REQUEST['response']) { case 1: header("Location: insert_ad1.php"); break; case 2: header("Location: index.php"); break; case 3: header("Location: insert_ad3.php"); break; case 4: header("Location: insert_ad4.php"); break; default: header("Location: default_response.php"); } ?> |
|||
| By: VGR | Date: 12/09/2003 16:24:00 | Type : Answer |
|
| $foo=... // work with $foo and produce new values for DB, let's say $var1, $var2... // now update the DB $linkID=mysql_connect("$dbHost","$dbLogin","$dbPassword") or die ("bad connect".mysql_error()); //or persistent connections? $linkID=mysql_pconnect("$dbHost","$dbLogin","$dbPassword") or die ("bad connect".mysql_error()); mysql_select_db($dbName,$linkID) or die ("bad select DB. ".mysql_error()); $query="UPDATE yourtable SET field1='$var1', field2=$var2 WHERE somecondition;"; $result=mysql_query($query,$linkID) or die ("bad query '$query' : ".mysql_error()); // it's done // PS I suggest to redirect AFTER the DB update and to perform the switch(action_to_take) HERE, like this : //case of action required switch($_POST['response']) { case 1: // insert AD1 //perform code for insertion, like the UPDATE above, but with INSERT INTO // redirect to "error.php" in case of error (in stead of die()-ing) break; case 2: // ? //perform code for ? // redirect to "error.php" in case of error (in stead of die()-ing) break; case 3: // insert AD3 //perform code for insertion, like the UPDATE above, but with INSERT INTO // redirect to "error.php" in case of error (in stead of die()-ing) break; case 4: // insert AD4 //perform code for insertion, like the UPDATE above, but with INSERT INTO // redirect to "error.php" in case of error (in stead of die()-ing) break; default: // default response //perform code for default response // redirect to "error.php" in case of error (in stead of die()-ing) } // case of header("Location: thankyou.php"); // page for positive action taken, itself redirecting to the main page after a while |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








