【问题标题】:i need to retrieve data from database and use it in php if statement我需要从数据库中检索数据并在 php if 语句中使用它
【发布时间】:2019-07-06 18:13:39
【问题描述】:

我有一个课程的系统注册,但我遇到了这个问题让它再次可用

我试图解决的方法是在管理页面中有一个单选按钮,它在数据库(使用 ajax)中传递值 0 或 1 我想要以某种方式检索该值,将其存储到 php 变量并使用例如在 if 语句中

html+ajax

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>OnClick Insert Radio Button value into Database using PDO in 
Jquery Ajax PHP | SoftAOX Tutorial</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script>
</head>
<body>
<h1> On Click Insert Radio Button Value into Database</h1>
<input type="radio" name="status" value= "1" >on<br/><br/>
<input type="radio" name="status" value= "0">off<br/><br/>

<h3 id="result"></h3>
<br/>
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var status = $(this).val();
$.ajax({
url:"insert.php"
method:"POST",
data:{status:status},
success: function(data){
$('#result').html(data);
}
});
});
});
</script>
  </body>
</html>

插入.php

<?php

//Insert Data
$hostname = "localhost";
$username = "root";
$password = "";
$databasename = "onlinecourse";
try {
    $conn = new PDO("mysql:host=$hostname;dbname=$databasename", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if (isset($_POST["status"])) {
        $query = "UPDATE CRS SET status =  (:status)";
        $statement = $conn->prepare($query);
        $statement->execute(
            array(
                'status' => $_POST["status"]
            )
        );
        $count = $statement->rowCount();
        if ($count > 0) {
            echo "Data Inserted Successfully..!";
        } else {
            echo "Data Insertion Failed";
        }
    }
} catch (PDOException $error) {
    echo $error->getMessage();
}

我想要重定向 if 语句的文件

<?php

session_start();
include('includes/config.php');

error_reporting(0);


if(strlen($_SESSION['login'])==0)
{   
header('location:index.php');
}
else{
date_default_timezone_set('Asia/Kolkata');// change according timezone
$currentTime = date( 'd-m-Y h:i:s A', time () );
if(isset($_POST['submit']))
{
$sql=mysqli_query($con,"SELECT * FROM  students where 
pincode='".trim($_POST['pincode'])."' && 
StudentRegno='".$_SESSION['login']."'");
$num=mysqli_fetch_array($sql);

if($num>0)
{
 //statement for button

$_SESSION['pcode']=$_POST['pincode'];


if($status == "1") {
header("location:enroll.php"); //when system is on 
}
else {
    header("location:close.php"); //when system is off
}
}
else
{
$_SESSION['msg']="Error :Wrong Pincode. Please Enter a Valid Pincode !!";
}
}
?>

【问题讨论】:

  • 好的,谢谢你知道有什么方法可以检索我想要的值并在 if 语句中使用它
  • 第二个文件有语法错误。缺少}

标签: php html mysql ajax


【解决方案1】:

单选按钮在被点击时会向 PHP 脚本发送一个字符串,其中包含其属性 value 的内容。

如何从收音机中获取数据

<input type="radio" name="status" value= "1" >on<br/><br/>
<input type="radio" name="status" value= "0">off<br/><br/>

radiocheckbox 类似,在未选择任何选项时不会向 PHP 脚本发送任何内容。

你可以这样做:

$status = $_POST['status'] ?? "0"; 
if($status = "1") {
    header("location:enroll.php"); //when system is on 
    exit;
} else {
    header("location:close.php"); //when system is off
    exit;
}

如果您不熟悉我在代码中使用的 ??,请查看null coalescing operator。如果您的 PHP 版本 ternary operator 代替。

【讨论】:

  • 是的,但这是检查它是否为空?我希望能够在管理员喜欢时将其关闭并在我弄错时纠正我但不应该使用 == 而不是 = ?
  • @daxternator 我正在使用 null 合并操作符 - ?? 检查它是否为空。它有助于节省一些代码行。 $status = $_POST['status'] ?? "0"; 行的意思是:如果$_POST['status'] 存在且不为空,则将其值分配给$status,否则,将"0" 分配给$status。你可以了解更多关于??我在答案中输入的链接中的运算符。
  • 是的,我理解,但因为我总是有一个价值,所以如果声明为真,它总是会有这个
  • 如果您在该字段中始终具有价值,则不需要该验证。你可以直接检查它的值:if($_POST['status'] == "1") {....
【解决方案2】:

我对@9​​87654321@ 不太熟悉,但你可以这样做。试想在 signup_confirm_table 中有一个名为statues_of_registration_form 的字段,并且该字段默认在0(已禁用)中。

然后在管理页面你可以让管理员像这样改变状态。

admin.php

<form action="action.php" method="post">
 <label> Change the visibility of the signup page</label>
   <select name="change_visibility">
    <option value="1">Show</option>
    <option value="0">Hide</option>
   </select>
  <button type="submit" name="submit">Confirm</button>     
</form>

<?php
if(isset($_POST['submit']){

  $sql = "UPDATE signup_confirm_table SET statues_of_registration_form = '".$_POST['change_visibility']."';
  $result= $dbconnection->query($sql);

   if(isset($result)){

    echo "<script>
    alert('Form status updated');
    window.location='admin.php';
    </script>";
}else{
    if(mysqli_errno($con)){
        echo "Error:".mysqli_error($con);
    }  
 }

 }
?>

现在管理员可以将statues_of_registration_form 字段设置为01。接下来在您的signup.php 中检查页面加载前的状态,如下所示。

注册.php

<html>
<head></head>
<body>
<?php
 $sql = "SELECT statues_of_registration_form FROM signup_confirm_table";
 $result= $dbconnection->query($sql);
 $row = mysql_fetch_array($result);

 if( $row['statues_of_registration_form'] == 1 ) { ?>
 //if status of the form = 1 put your form here
  <form>
   <input type="text"/>
  </from>
 <?php 
}else{
  echo "<script>
        alert('Registration form disabled by the admin.');
        window.location='signup.php';
        </script>";
?>
</body>
</html>

请注意,这只是一个示例。强烈建议您在sql 中使用prepared statements。希望您能从中得到一些东西。谢谢

【讨论】:

  • @Dharman 我在答案中提到了它。他应该知道怎么用ph​​p
  • 是的,但是你为什么要建议一个有缺陷的代码,并且只在最后加上一个小脚注如何正确地做到这一点。没有意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 2016-08-03
  • 2015-12-13
  • 1970-01-01
  • 2014-10-18
相关资源
最近更新 更多