下記のようにしたら上手く動作しました。
functions.php
1<?php
2function db_access($db){
3 $dsn = 'mysql:dbname=example;host=localhost';
4 $user = 'root';
5 $password = 'shapshap';
6 $dbh = new PDO($dsn, $user, $password);
7 $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
8 switch($db){
9 case 'search':
10 $query = $dbh->query('select C_nm from example');
11 while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
12 echo '<option value="'.$row['C_nm'].'">'.$row['C_nm'].'</option>';
13 }
14 return $row;
15 break;
16 default:
17 break;
18 }
19}
20?>
<?php
try{
$db='search';
echo '<select name="C_num">';
require_once('C:\xampp\htdocs\customer\functions.php');
db_access($db);
echo '</select>';
}
catch (PDOException $e){
print('Error:'.$e->getMessage());
die();
}
$dbh = null;
?>
<?php
function db_access($db){
try{
$dsn = 'mysql:dbname=example;host=localhost';
$user = 'root';
$password = 'shapshap';
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
switch($db){
case 'search':
echo '<select name="C_num">';
$query = $dbh->query('select C_nm from example');
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['C_nm'].'">'.$row['C_nm'].'</option>';
}
echo '</select>';
$result = $row;
break;
case 'search_ans';
if (isset($_POST['C_num'])){
$get_C_nam[] = $_POST['C_name'];
$get_C_nm[] = $_POST['C_num'];
$get_C_phn[] = $_POST['C_phon'];
$get_C_add[] = $_POSt['C_add'];
}
else
{
$C_nm = $_GET['C_num'];
$sql = 'select * from example where C_nm= '.$C_nm.'';
foreach ($dbh->query($sql) as $row) {
$get_C_nam[] = $row['C_nam'];
$get_C_nm[] = $row['C_nm'];
$get_C_phn[] = $row['C_phn'];
$get_C_add[] = $row['C_add'];
}
}
$result = [$get_C_nam, $get_C_nm, $get_C_phn, $get_C_add];
break;
case 'search_fix_a';
$stmt = $dbh->prepare('UPDATE example SET C_nam = :C_nam, C_nm = :C_nm, C_phn = :C_phn, C_add = :C_add WHERE example.C_nm = :C_nm');
$stmt->execute(array(':C_nam' => $_POST['C_name'], ':C_nm' => $_POST['C_num'], ':C_phn' => $_POST['C_phon'], ':C_add' => $_POST['C_sa']));
break;
default:
break;
}
}
catch (PDOException $e){
print('Error:'.$e->getMessage());
die();
}
if(isset($result)==true){
return $result;
}
$dbh = null;
}
?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。