php - 如何在我的管理页面中显示数据库

标签 php mysql forms mysqli

提交表单后,它可以将所有数据发送到sql数据库。
现在,我想做的是在管理页面中显示所有数据库数据

这是我的 php 表单代码

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>

<script>
 function disableSubmit() {
  document.getElementById("submit").disabled = true;
 }

  function activateButton(element) {

      if(element.checked) {
        document.getElementById("submit").disabled = false;
       }
       else  {
        document.getElementById("submit").disabled = true;
      }

  }
</script>
        <title>Page Title Goes Here</title>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="form1.css"/>

</head>

<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
<body onload="disableSubmit()">

<?php 

//define variable and set to empty value

$forenameErr = $surnameErr = $emailErr = $postalAddressErr = $landLineTelNoErr = $mobileTelNoErr = $sendMethodErr = $checkErr ="";
$valid = true;

// if forename is null , make it null , else test_input()
$forename = empty($_POST["forename"]) ? NULL : test_input($_POST["forename"]);

// if surname is null , make it null , else test_input()
$surname =  empty($_POST["surname"]) ? NULL : test_input($_POST["surname"]);

// if postalAddress is null , make it null , else test_input()
$postalAddress = empty($_POST["postalAddress"]) ? NULL : test_input($_POST["postalAddress"]);

// if landLineTelNo is null , make it null , else test_input()
$landLineTelNo = empty($_POST["landLineTelNo"]) ? NULL : test_input($_POST["landLineTelNo"]);

// if mobileTelNo is null , make it null , else test_input()
$mobileTelNo = empty($_POST["mobileTelNo"]) ? NULL : test_input($_POST["mobileTelNo"]);

//email
$email = empty($_POST["email"]) ? NULL : test_input($_POST["email"]);

// if sendMethod is null , make it null , else test_input()
$sendMethod = empty($_POST["sendMethod"]) ? NULL : test_input($_POST["sendMethod"]);

//check
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

    if  (isset($_POST["submit"])){


        //check forename
        if($forename === NULL)  {
            //forename is empty
            $forenameErr = "*Forename is required";
            $valid = false;

        } else {
            //check characters
             if (!preg_match("/^[a-zA-Z ]*$/",$forename)) {
                 $forenameErr = "Only letters and white space allowed";
                 $valid = false;
             }
        }

        //check surname
        if($surname === NULL){
            //surname is empty
            $surnameErr = "*Surname is required";
             $valid = false; //false

        } else {
            //check charaters
             if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
             $surnameErr = "*Only letters and white space allowed";
             $valid = false;
            }
        }   

         //check address
         if (!preg_match("/^[a-zA-Z0-9\-\\,. ]*$/", $postalAddress)) {
                     // check characters
                     $postalAddressErr = "*Invalid Postal Address";
                     $valid = false;//false
        }


            // check if invalid telephone number added
            if (!preg_match("/^$|^[0-9]{12}$/",$landLineTelNo)) {
                //check number
                     $landLineTelNoErr = "*Only 12 digit number can be entered";
                     $valid = false;//false
            }


            //check valid mobiel tel no
            if (!preg_match("/^$|^[0-9]{11}$/",$mobileTelNo)) {
                //check number
                     $mobileTelNoErr = "*Only 11 digit number can be entered";
                     $valid = false;//false
            }


        //check valid email
            if (isset($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) 
            { $emailErr = "*Invalid email format"; 
                 $valid = false;//false
             }


        //check sendMethod
        if($sendMethod === NULL){
             //send method is empty
             $sendMethodErr = "*Contact method is required";
             $valid = false; //false
        } else {
            $sendMethod = test_input($_POST["sendMethod"]);
        }

        //sendmethod link to information filled
        if (isset($sendMethod) && $sendMethod=="email" && $email ==NULL){
            $emailErr ="*Email is required ";
            $valid = false;
        }

        if (isset($sendMethod) && $sendMethod=="post" && $postalAddress ==NULL){
            $postalAddressErr ="*Postal Address is required ";
            $valid = false;
        }

        if (isset($sendMethod) && $sendMethod=="SMS" && $mobileTelNo ==NULL){
            $mobileTelNoErr ="*Mobile number is required ";
            $valid = false;
        }

        if(empty($_POST['agree']) || $_POST['agree'] != 'agree') {
        $checkErr ="Please indicate that you have read and agree to the Terms and Conditions and Privacy Policy";
        }


    //Sever side script
    if($valid){

    /* SQL code starts */

    $con = mysqli_connect("localhost", "root", "", "chollerton");

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    if (is_null($_POST["email"]) || $_POST["email"]=="") {
         $email = 'NULL';
         } else {
             $email = ($_POST["email"]);
             }

    if (is_null($_POST["postalAddress"]) || $_POST["postalAddress"]=="") {
         $postalAddress = 'NULL';
         } else {
             $postalAddress = ($_POST["postalAddress"]);
             }

    if (is_null($_POST["landLineTelNo"]) || $_POST["landLineTelNo"]=="") {
         $landLineTelNo = 'NULL';
         } else {
             $landLineTelNo = ($_POST["landLineTelNo"]);
             }  

    if (is_null($_POST["mobileTelNo"]) || $_POST["mobileTelNo"]=="") {
         $mobileTelNo = 'NULL';
         } else {
             $mobileTelNo = ($_POST["mobileTelNo"]);
             }  


    $sql   = $sql = "INSERT INTO ct_expressedinterest (forename, surname, email, postalAddress, landLineTelNo, mobileTelNo ,sendMethod) VALUES ('$forename', '$surname', '$email', '$postalAddress', '$landLineTelNo', '$mobileTelNo' ,'$sendMethod')";
    $query = mysqli_query($con,$sql) or die(mysqli_error($con));


     //if valid then redirect
    if($valid){

         $_SESSION['forename'] = $forename;
         $_SESSION['surname'] = $surname;
         $_SESSION['email'] = $email;
         $_SESSION['postalAddress'] = $postalAddress;
         $_SESSION['landLineTelNo'] = $landLineTelNo;
         $_SESSION['mobileTelNo'] = $mobileTelNo;
         $_SESSION['sendMethod'] = $sendMethod;

         header('Location: userdetail.php');
         exit();
        }else    {echo "Unable to insert";
        }
    }   else{
         //user did not submit form!
    }


    }
?>


<div id="wrapper">

<h1>Welcome to Chollerton Tearoom! </h1>

<nav> 
    <ul>
         <li><a href="index.html">Home</a></li>
         <li><a href="findoutmore.html">Find out more</a></li>
         <li><a href="offer.html">Offer</a></li>
         <li><a href="credit.html">Credit</a></li>
         <li><a href="admin.php">Admin</a></li>
         <li><a href="wireframe.html">WireFrame</a></li>
    </ul>
</nav>

<form id ="userdetail" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

    <fieldset id="aboutyou">
    <legend id="legendauto">user information</legend>

        <p>
        <label for="forename">Forename: </label>
        <input type="text" name="forename" id="forename" value="<?php echo $forename;?>">
        <span class="error"> <?php echo $forenameErr;?></span>
        </p>

        <p>
        <label for="surname">Surname:</label>
        <input type="text" name="surname" id="surname" value="<?php echo $surname;?>">
        <span class="error"> <?php echo $surnameErr;?></span>
        </p>

        <p>
        <label for="postalAddress">Postal Address:</label>
        <input type="text" name="postalAddress" id="postalAddress" value="<?php echo $postalAddress;?>">
        <span class="error"> <?php echo $postalAddressErr;?></span>
        </p>

        <p>
        <label for="landLineTelNo">Landline Telephone Number:</label>
        <input type="text" name="landLineTelNo" id="landLineTelNo" value="<?php echo $landLineTelNo;?>" >
        <span class="error">  <?php echo $landLineTelNoErr;?></span>
        </p>

        <p>
        <label for="mobileTelNo">Moblie:</label>
        <input type="text" name="mobileTelNo" id="mobileTelNo" value="<?php echo $mobileTelNo;?>" >
        <span class="error">  <?php echo $mobileTelNoErr;?></span>
        </p>

        <p>
        <label for="email">E-mail:</label>
        <input type="text" name="email" id="email" value="<?php echo $email;?>">
        <span class="error"> </span> <?php echo $emailErr;?> </span>
        </p>

        <fieldset id="future">
        <legend>Lastest news</legend>

        <p>
        Choose the method you recommanded to recevive the lastest information
        </p>
        <br>
        <input type="radio" name="sendMethod"  <?php if (isset($sendMethod) && $sendMethod=="email") echo "checked";?>  value="email">
        Email
        <input type="radio" name="sendMethod"  <?php if (isset($sendMethod) && $sendMethod=="post") echo "checked";?>  value="post">
        Post
        <input type="radio" name="sendMethod"  <?php if (isset($sendMethod) && $sendMethod=="SMS") echo "checked";?>  value="SMS">
        SMS
        <span class="error"> <?php echo $sendMethodErr;?></span>
        </fieldset>

       <p><span class="error">* required field.</span></p>

         <input type="checkbox" name="terms" id="terms" onchange="activateButton(this)">  
         I Agree Terms & Coditions
         <br><br>
         <input type="submit" name="submit" id="submit">


     </fieldset>
</form>      

</div>

</body>
</html>

这是我的 admin.php 代码

    <?php
    include 'database_conn.php'; //make db conncection

    $sql = "SELECT expressInterestID, forename, surname, email, postalAddress, landLineTelNo, mobileTelNo ,sendMethod FROM ct_expressedinterest";

    $queryresult = mysqli_query($conn,$$sql) or die(mysql_error($conn));

    while ($row = mysqli_fetch_assoc($queryresult)) {
         $expressInterestID = $row['expressInterestID'];
         $forename = $row['forename'];
         $surname = $row['surname'];
         $email = $row['email'];
         $postalAddress = $row['postalAddress'];
         $landLineTelNo = $row['landLineTelNo'];
         $mobileTelNo  = $row['mobileTelNo'];
         $sendMethod = $row['sendMethod'];

         echo"<div> $expressInterestID , $forename, $surname, $email,$postalAddress, $landLineTelNo, $mobileTelNo, $sendMethod </div>";
    }

    mysqli_free_result($queryresult);
    mysqli_close($conn);
?>

包含此 admin.php 以连接到 chollerton 数据库。 这是连接到表的 database_conn

<?php 
     $con = mysqli_connect("localhost", "root", "", "chollerton");

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else {echo "conect success";}
?>

谁能告诉我我的代码有什么问题吗?
管理页面显示数据库中的数据。
请帮助我并为我提供解决方案代码...
非常感谢!!

最佳答案

您的 admin.php 代码有问题。您使用 mysqli_query($conn,$$sql) 而不是 mysqli_query($conn,$sql)

试试这段代码:

<?php
include 'database_conn.php'; //make db conncection

$sql = "SELECT expressInterestID, forename, surname, email, postalAddress, landLineTelNo, mobileTelNo ,sendMethod FROM ct_expressedinterest";

$queryresult = mysqli_query($conn, $sql) or die(mysql_error($conn));

while ($row = mysqli_fetch_assoc($queryresult)) {
    $expressInterestID = $row['expressInterestID'];
    $forename = $row['forename'];
    $surname = $row['surname'];
    $email = $row['email'];
    $postalAddress = $row['postalAddress'];
    $landLineTelNo = $row['landLineTelNo'];
    $mobileTelNo  = $row['mobileTelNo'];
    $sendMethod = $row['sendMethod'];

    echo"<div> $expressInterestID , $forename, $surname, $email,$postalAddress, $landLineTelNo, $mobileTelNo, $sendMethod </div>";
}

mysqli_free_result($queryresult);
mysqli_close($conn);
?>

关于php - 如何在我的管理页面中显示数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568871/

相关文章:

php - 使用sbin/start-dfs.sh启动Hadoop集群

php - laravel 5.2 文件上传错误

mysql - 如何找到一个确切的单词而不是单词的一部分?

mysql - vb.net 按 X 轴日期排序图表

c# - Hook 键盘复制输入(c#/xna)

javascript - 如何在 &lt;input&gt; 为空时发出 CSS 警告(最好没有 JS)

php - 通过另外一个 MySQL/PHP 连接 2 个表

php - 如何在 onFlush 事件监听器中更新实体的 manyToMany 集合?

mysql - 如何检查一条记录是否存在然后在MySQL中返回1否则返回0

php - 将 PHP session 变量传递到 HTML 表单