visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
316 experts, 1194 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: update table, connect to DB


By: FiatLink Belgium  Date: 12/09/2003 00:00:00  English  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 English  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

EContact
browser fav
page generated in 253.122090 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page