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 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?