I have an included filed called dbLogin.php, with the following function:
PHP Code:
function dbLogin($dbDatabase){
global $dbHost, $dbUser, $dbPass, $db;
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
}
Then, I simply call dbLogin() when I need to connect to a database. It works with multiple connections on other pages, and I use mysql_close() before each instance just to be safe.
Anyway, the problem: I'm using a foreach statement to grab $_POST variables submitted to the page, and splitting them up into two parts in order to be added to two different databases. Here's the code:
PHP Code:
$name = isset($_POST['name']) ? trim(stripslashes($_POST['name'])) : FALSE;
$sn = isset($_POST['sn']) ? trim(stripslashes($_POST['sn'])) : FALSE;
$db2 = isset($_POST['db']) ? $_POST['db'] : FALSE;
$dbtable = isset($_POST['dbtable']) ? $_POST['dbtable'] : FALSE;
unset($_POST['name']);
unset($_POST['sn']);
unset($_POST['db']);
unset($_POST['dbtable']);
unset($_POST['submit']);
unset($_POST['submit3']);
$dbpath = $db2."-".$dbtable;
$addArr = array(
"name" => $name,
"sn" => $sn,
"dbpath" => $dbpath);
$addArrKeys = array_keys($addArr);
$standFields = array_keys($standFields);
$query_standFields_cols = "$addArrKeys[0],$addArrKeys[1],$addArrKeys[2]";
$query_standFields_vals = "'$addArr[name]','$addArr[sn]','$addArr[dbpath]'";
$query_custFields_cols = "sn";
$query_custFields_vals = "'$sn'";
include('dbLogin.php');
dbLogin($db2);
$result = mysql_query("SELECT * FROM $dbtable");
$result = mysql_fetch_assoc($result,MYSQL_ASSOC);
$result = array_keys($result);
foreach($_POST as $key => $val){
if(in_array($key,$standFields)){
$query_standFields_cols .= ",$key";
$query_standFields_vals .= ",'$val'";
}
else if(in_array($key,$result)){
$query_custFields_cols .= ",$key";
$query_custFields_vals .= ",'$val'";
}
else{
die('<h1>This is not working so well.</h1>');
}
}
$query_custFields = "INSERT INTO $dbtable ($query_custFields_cols) VALUES ($query_custFields_vals)";
$query2_custFields = mysql_query($query_custFields) or die('WEIRD! query1custFields');
mysql_close();
dbLogin('standardFields') or die('Cant login to standardFields!');
$query_standFields = "INSERT INTO standFields ($query_standFields_cols) VALUES ($query_standFields_vals)";
$query2_standFields = mysql_query($query_standFields) or die('WEIRD! query2standFields');
mysql_close();
header('Location:dEdit.php?viewSN='.$sn);
The custom fields are added to that specific database, but then the code dies at dbLogin('standardFields'). The database standardFields exists and I'm able to login to it in other places. Why won't it login to standardFields here? See anything I'm missing? Thanks.
Bookmarks