dqkx69935 2014-01-04 11:24 采纳率: 100%
浏览 56
已采纳

PHP elseif语句用于表单验证和插入到db

I am trying to make my form validation and db insert script work but am facing problems again. I am using an elseif statement to do:

  1. Check if the form was actually submitted by the user clicking the submit button and all data fields completed, if not warn and redirect back to the form page (this is the only part that seems to be working)

  2. If the form has been completed fully and submit button clicked then connect to mysql server and select db or die and display error message

  3. If there is a connection to the database then insert the form data to the table

I can prevent the entry of empty fields but that is all,everything else seems to break. I cannot seem to figure out why. tail -f /var/log/apache2/error.log displays nothing. Perhaps I have over complicated things. I have been using this site as a reference and http://www.w3schools.com/php/php_if_else.asp for elseif syntax, newbie still screwing things up.

Here is the code:

<?php
//Form fields passed to variables
$manu = mysql_real_escape_string($_POST['inputManu']);
$model = mysql_real_escape_string($_POST['inputModel']);
$desc = mysql_real_escape_string($_POST['inputDesc']);

//Connect to database using $conn
include ('connection.php');

//Insert record into table 
$sql = "INSERT INTO gear (`id`,`manu`,`model`,`desc`)
      VALUES (NULL,'$manu','$model','$desc')";

//Check for empty fields
if ($_POST['submit']) 
{   
   foreach($_POST as $val) 
{
      if(trim($val) == '' || empty($val))
        {
        die();
      echo "Please complete all form fields!";
      echo "<meta http-equiv='Refresh' content='3; URL=../add.php'>"; 
      //header("Location: ../add.php?error=empty_fields");
        }
    }
}
elseif (!mysqli_query($conn,$sql))
{
  die('Error: ' . mysqli_error($conn));
}
else
{
  //echo "1 record added";
  echo "Success, You added the ".$manu." ".$model."";
  echo "<meta http-equiv='Refresh' content='3; URL=../index.php'>";
}   
mysqli_close($conn);
?>
  • 写回答

2条回答 默认 最新

  • doudeng3008 2014-01-04 11:34
    关注

    you need to move the query execution part inside the first if.your full code should look like this:

    <?php
     //Connect to database using $conn
    include ('connection.php');
    //Form fields passed to variables
    $manu = mysqli_real_escape_string($_POST['inputManu']);
    $model = mysqli_real_escape_string($_POST['inputModel']);
    $desc = mysqli_real_escape_string($_POST['inputDesc']);
    
    
    
    //Insert record into table 
    $sql = "INSERT INTO gear (`id`,`manu`,`model`,`desc`)
          VALUES (NULL,'$manu','$model','$desc')";
    
    //Check for empty fields
    if (isset($_POST['submit'])) 
    {   
       foreach($_POST as $val) 
        {
          if(trim($val) == '' || empty($val))
            {
            die();
          echo "Please complete all form fields!";
          echo "<meta http-equiv='Refresh' content='3; URL=../add.php'>"; 
          //header("Location: ../add.php?error=empty_fields");
            }
        }
    
         if (!mysqli_query($conn,$sql))
         {
         die('Error: ' . mysqli_error($conn));
         }
         else
         {
           //echo "1 record added";
             echo "Success, You added the ".$manu." ".$model."";
             echo "<meta http-equiv='Refresh' content='3; URL=../index.php'>";
         }   
    }
    else
    {
    echo "some error";
    }
    
    mysqli_close($conn);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度