Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自验证后如何连接到sql_Php_Sql_Forms - Fatal编程技术网

Php 自验证后如何连接到sql

Php 自验证后如何连接到sql,php,sql,forms,Php,Sql,Forms,我的表单操作是php_self,因此它可以验证表单… 我想做的是在提交表单后,将数据连接并发送到sql…. 我已经导入了我的sql表,表中记录了一些数据…. 那么如何连接到sql?? 还有我应该在哪里写我的连接sql代码 这是我的php表单代码…… <?php // Start the session session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <script&g

我的表单操作是php_self,因此它可以验证表单…
我想做的是在提交表单后,将数据连接并发送到sql….
我已经导入了我的sql表,表中记录了一些数据….
那么如何连接到sql??
还有我应该在哪里写我的连接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"]);

    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";
        }


     //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{
         //user did not submit form!
    }

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

?>


<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</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>

函数disableSubmit(){
document.getElementById(“提交”).disabled=true;
}
功能激活按钮(元素){
如果(元素已选中){
document.getElementById(“提交”).disabled=false;
}
否则{
document.getElementById(“提交”).disabled=true;
}
}
页面标题在这里
页面标题在这里
欢迎来到乔勒顿茶室!

您应该在$valid内编写SQL代码

让我举例如下:

注意:我使用了默认凭据:Hostname=localhost,username=root,password='',database name=my_database

您可以参考以下内容:

if($valid){
echo“Valid data
”;//调试代码 回声'; 打印(邮政美元); 出口 /*SQL代码启动*/ $con=mysqli_connect(“本地主机”、“根”、“我的数据库”); if(mysqli\u connect\u errno()){ echo“未能连接到MySQL:”.mysqli_connect_error(); } $sql=“插入到…”;//插入查询 $query=mysqli_query($con,$sql)或die(mysqli_error($con)); /*SQL代码结束*/ if($query){//添加此条件。仅当SQL查询成功时才应写入会话 $\会话['forename']=$forename; $\会话['姓氏]=$姓氏; .......... $\会话['sendMethod']=$sendMethod; 标题('Location:userdetail.php'); 退出(); }否则{ 回显“无法插入”; } }否则{ echo“无效数据
”;//调试代码 回声'; 打印(邮政美元); 出口 }

希望这有帮助。

我需要在表单php页面或userdetail.php页面中编写sql代码?您可以在表单页面本身中编写它。您应该在if($valid)内尝试在此处调试。让我编辑我的答案。请告诉我你得到了什么。
if($valid){

    echo "Valid data<br/>";     // Debugging code
    echo '</pre>';
    print_r($_POST);
    exit;

    /* SQL code starts */

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

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

    $sql   = "INSERT INTO...."; // Your insert query
    $query = mysqli_query($con,$sql) or die(mysqli_error($con));

    /* SQL code ends */

    if ($query) {                // Add this condition. Session should be written only when SQL query is successful

       $_SESSION['forename'] = $forename;
       $_SESSION['surname'] = $surname;
       ..........
       $_SESSION['sendMethod'] = $sendMethod;
       header('Location: userdetail.php');
       exit();
    } else {
        echo "Unable to insert";
    }

}   else{
     echo "Invalid data<br/>";          // Debugging code
     echo '</pre>';
     print_r($_POST);
     exit;
}