简体   繁体   中英

How can I define which db to pull from via post

$value = $_POST['directions'];
$value2 = $_POST['inventory'];
$value3 = $_POST['item'];
$value4 = $_POST['vendor'];
$sql = "INSERT INTO a1 (directions,inventory,item,vendor) VALUE ('$value','$value2','$value3','$value4')";

Right now I'm having a mysql db get updated via what I type in a textbook from a separate page. However where it says "INSERT INTO a1" id like to make "a1" be a variable like "$table" I define from the same page I type all the other values in

The question need to be more clear, but from my understanding, you can use something like this code :

// should be defined like this inside each page.    
$table = mysql_real_escape_string($_POST['tablename']); 

$params = array();
foreach($_POST as $key => $val){
    if($key!='tablename'){ 
        $params[]= "$key='".mysql_real_escape_string($val)."'"; 
    }
}

if(count($params) > 0) {
   $params = implode(",",$params);
   $sql = "INSERT INTO $table SET $params";
} else {
   die "Error submitting the data!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM