简体   繁体   中英

query for or change column name in php?

I need to figure out a way to prepare an insert statement in PHP for a SQL db and I'm having trouble comprehending the logic. The insert statement will be from data I'm receiving in JSON . The JSON contains questions and answers. The table I'm inserting them into is the problem. On the front end (website) the table has column names that represent the questions and it look like this.

Date | Address | City | Zip

Now the problem is in the database these names don't carry over. The column names are just column1 column 2 etc. So it looks like this

Column1 | Column2 | Column3 | Column4

Another thing - when each table like this is created it will have different values so I can't hard code the relationship. Also these tables are created from importing an excel spreadsheet from the front end (website). So I came up with a plan to duplicate row 1 & 2 of the spreadsheet so that now when you import them the database looks like this

id | Column1 | Column2 | Column3 | Column4
 1 |   Date  | Address |   City  |   Zip

So now I have some reference to the values that will need to be inserted. Now the next steps involved will be.

  1. Query for column name based on the data in row 1 OR change the column name to the data in row 1.
  2. Insert data into the correct columns based on the query

So in summary I cannot insert the answers into the correct column representing the question without the proper information. I personally think changing the column names in PHP would be the better option. However I'm pretty new to PHP and SQL so I don't even know if this is possible. Any help is appreciated

EDIT

to give a slightly better understanding of the problem I am posting the PHP I currently have.

$json = safe($_POST['assessment']);
/*
$json = 
'{
  "info":
  [
    "Date: 6-22-13",
    "Phone #: (555) 555-5555",
    "Address: 304 N. SCOTTSDALE RD.",
    "City: SCOTTSDALE",
    "Zip: 85251",
    "State: AZ"        
   ]
}';
*/
$data = json_decode($json,true);
$collection = array();

foreach($data['info'] as $piece) {
   $info = explode(':', $piece);
   $collection[] = array('question' => $info[0], 'answer' => $info[1]); 
}
echo '<pre>'; print_r($collection); echo '</pre>'

The following should create an associative array that maps the names of columns to the column numbers in the database schema:

$stmt = $db->prepare('select Column1, Column2, Column3, ... from table where id = 1');
$result = $stmt->execute();
$data = $result->fetch(PDO::FETCH_ASSOC);
$col_map = array_flip($data);

You can then use this when preparing future queries of the database.

If you don't know how many columns you could have, you could do this:

$stmt = $db->prepare('select * from table where id = 1');
$result = $stmt->execute();
$data = $result->fetch(PDO::FETCH_ASSOC);
unset($data['id']); // Remove the `id` column, which doesn't conform to the ColumnN pattern
$col_map = array_flip($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