我需要根据已选择的单选按钮从数据库中选择一个表。我认为这将起作用的方式是,1)通过确定在if语句中选择了什么单选按钮并为其分配变量; 2)将该变量分配给PDO中用于连接的$ dbname(表的变量)。我的尝试没有成功,所以我需要知道如何从if语句中获取变量并在PDO中使用它(或者被告知更好的解决方案)。
示例html(有三个单选按钮):
<div id="radio_buttons">
<label for="p_book">Book</label>
<input type="radio" name="p_radio" id="p_book" value="p_book">
<label for="p_article">Article</label>
<input type="radio" name="p_radio" id="p_article" value="p_article">
<label for="journal">Journal</label>
<input type="radio" name="p_radio" id="p_journal" value="p_journal">
</div> <!-- end radiobuttons -->
我如何定位PHP中的单选按钮:
if (isset($_POST['edify_button'])) {
if(isset($_POST['p_radio'])) {
$radio_values = $_POST['p_radio'];
if ($radio_values == 'p_book') {
$dbname = 'books'; //this is the name of the table
return $dbname;
}elseif ($radio_values == 'p_article') {
$dbname = 'articles';
return $dbname;
}elseif ($radio_values == 'p_journal') {
$dbname = 'journals';
return $dbname;
}
}else{
echo 'unchecked';
}
}
然后我想以某种方式让变量在PDO中计算:
function connect($dbname, $servername) {
//$dbname is supposed to be retrieved from the if-statement
$servername = "localhost";
$username = "admin";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
我还尝试将if语句放在connect()中,以防connect()无法访问if语句中的变量:不成功。
我在线搜索疯子的信息,但没有运气。如果您知道涵盖这些内容的教程,请发布它。
答案 0 :(得分:0)
我解决问题的方式(它不是OO,它仍然有点原始)是通过实施3个步骤:
1)使用if-statements创建一个函数,该函数检查单选按钮值并为与表名对应的变量赋值;
2)创建函数的变量并在PDO中调用它(见下文);
3)在PDO函数中添加一个简单的if语句,以检查是否没有单击单选按钮。
第一个功能:
self.sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.xxxxxxxx"];
然后是一些变量:
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[request setHTTPMethod:@"GET"];
[self sendURLRequestWithURLString:request];
-(void)sendURLRequestWithURLString:(NSMutableURLRequest *)request
{
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if (!connectionError)
{
if (data)
{
id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if ([jsonObj isKindOfClass:[NSDictionary class]])
{
NSDictionary *jsonDict = (NSDictionary*)jsonObj;
NSDictionary *featuredEvent = [jsonDict valueForKey:@"featured_event"];
self.completionHandler([featuredEvent valueForKey:@"events"],[[featuredEvent valueForKey:@"total"] integerValue],[featuredEvent valueForKey:@"error_msg"]);
}
else
{
// self.completionHandler(false);
}
}
else
{
//self.completionHandler(false);
}
}
else
{
[self sendURLRequestWithURLString:request];
//self.completionHandler(false);
}
}];
}
最后是PDO功能:
function table_name() {
if (isset($_POST['edify_button'])) {
if(isset($_POST['p_radio'])) {
$radio_values = $_POST['p_radio'];
if ($radio_values == 'p_book') {
$table_name = 'books';
return $table_name;
}elseif ($radio_values == 'p_article') {
$table_name = 'articles';
return $table_name;
}elseif ($radio_values == 'p_journal') {
$table_name = 'journals';
return $table_name;
}
}else{
$table_name = false;
}
}
}