我的代码根据用户提供的名称和日期在数据库中创建一个表(想象所有的值都没有重复),它将只为第一个"用户&#创建一个数据库34; (您可以从下拉框中选择名字),但没有其他名称?可以发布其他sql
代码,但不要认为这是问题所在。
switch ($Name) {
case "Ben":
ct($Name, $Date);
break;
case "Tom":
ct($Name, $Date);
break;
case "Tom":
ct($Name, $Date);
break;
case "Ben":
ct($Name, $Date);
break;
case "Tom":
ct($Name, $Date);
break;
case "Ben":
ct($Name, $Date);
break;
case "Tom":
ct($Name, $Date);
break;
case "Ben":
ct($Name, $Date);
}
}
?>
<!-- Allows User to who they are -->
<p>You are: </p><form action="" method="post">
<select name="name" id="name">
<option value="RoryRedfearn">Rory Redfearn</option>
<option value="ChrisParner">Chris Parner</option>
<option value="JamesBarnes">James Barnes</option>
</select>
功能:
function ct($Name, $Date) {
//Takes inputed data and stores it in variables
$Car = isset($_POST['car']) ? trim($_POST['car']) :'';
$Night = isset($_POST['yes']) ? trim($_POST['yes']) :'';
$Siteid = isset($_POST['siteid']) ? trim($_POST['siteid']) :'';
$LHA = isset($_POST['stepExample1']) ? trim($_POST['stepExample1']) :'';
$AAS = isset($_POST['stepExample2']) ? trim($_POST['stepExample2']) :'';
$LS = isset($_POST['stepExample3']) ? trim($_POST['stepExample3']) :'';
//Database information
$dbhost = "localhost";
$dbname = "people";
$dbusername = "root";
$dbpassword = "";
//connects to our database
$link = new PDO("mysql:host=$dbhost;dbname=$dbname","$dbusername","$dbpassword");
$table = "" .$Name. " ". $Date . "";
$sql = "CREATE TABLE IF NOT EXISTS `".$table."` (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Car VARCHAR(20) NOT NULL,
Night VARCHAR(20) NOT NULL,
Date VARCHAR(20) NOT NULL,
Siteid VARCHAR(20) NOT NULL,
LHA VARCHAR(20) NOT NULL,
AAS VARCHAR(20) NOT NULL,
LS VARCHAR(20) NOT NULL)";
$sq = $link->query($sql);
//Prepares inputed data for insertion
$statement = $link->prepare("INSERT INTO $table(Name, Car, Night, Date, Siteid, LHA, AAS, LS)
VALUES(:Name, :Car, :Night, :Date, :Siteid, :LHA, :AAS, :LS)");
try {
$statement->execute(array(
"Name" => $Name,
"Car" => $Car,
"Night" => $Night,
"Date" => $Date,
"Siteid" => $Siteid,
"LHA" => $LHA,
"AAS" => $AAS,
"LS" => $LS,
));
} catch(PDOException $e) {
echo "Exception caught: $e";
}
}
答案 0 :(得分:2)
如果仍然重复相同的代码,为什么要使用switch
?
if (in_array($Name, ['Ben', 'Tom']) {
ct($Name, $Date);
}
建议的数据库结构(像所有其他人一样):
[user]
id | Name | Surname
1 Ben Afflec
2 Tom Gandolfini
[data]
id | UserID | Car | Night | Date
1 1 Yes Yes No
2 2 No Yes No
然后查询将更容易维护
SELECT Car FROM data WHERE userID IN (1, 2);