如何通过php验证表单数据,使其包含内容?

如何通过php验证表单数据,使其包含内容?,php,html,validation,Php,Html,Validation,因此,我创建了一个html表单,它将信息提供给保存html表单的文档,我需要一个php检查,以确保特定字段中包含信息,我的代码如下所示 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $errors = array(); if ( ! isset($_POST['dbname'])) { array_push($errors,'dbname'); } if ( ! isset($_P

因此,我创建了一个html表单,它将信息提供给保存html表单的文档,我需要一个php检查,以确保特定字段中包含信息,我的代码如下所示

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors = array();
    if ( ! isset($_POST['dbname'])) {
        array_push($errors,'dbname');
    }
        if ( ! isset($_POST['dbpass'])) {
        array_push($errors, 'dbpass');
        }
            if ( ! isset($_POST['dbhost'])) {
        array_push($errors, 'dbhost');
        }
            if ( ! isset($_POST['prefix'])) {
        array_push($errors, 'prefix');
        }
            if (isset($_POST['dbname']) && trim($_POST['dbname']) != '' {
            echo "something";
            }
            else
            {
            echo "nothing";
            }

    if (count($errors) > 0) {
        $error_msg = implode('<br>', $errors);
        show_form($error_msg);
    }
    else {
        do_install();
        echo 'Install Done';
    }
}
else {
    // show the form
    show_form();
}
基本上,我认为这一行基本上会说“如果设置了字段dbname,并且在修剪时它的值等于某个值,那么就这样做,如果不是,那么就这样做”,但是当我在整个过程中添加这一行时,它就断开了,不再在浏览器中显示任何内容

如果你需要的话,我文件的其他部分看起来像这样

function show_form($msg = '') {
?>
<html>
    <body>
        <?php if (isset($msg) && trim($msg) != '') { echo $msg; } ?>
        <P> HI </P>
        <form action="hi.php" method="POST">
            <p>Your Database name and user: <input type="text" name="dbname" /><br />
            Your database password <input type="text" name="dbpass" /> <br/>
            Your DB_HOST (ip address) <input type ="text" name = "dbhost" /> <br/> 
            Your desired table prefix <input type ="text" name = "prefix" /> <br />
            </p>

            <p><input type="submit" value="Send it!"></p>
        </form>
<?php
        }
        ?>
<?php
function recurseRmdir($di) {
  $files = array_diff(scandir($di), array('.','..'));
  foreach ($files as $file) {
    (is_dir("$di/$file")) ? recurseRmdir("$di/$file") : unlink("$di/$file");
  }
  return rmdir($di);
}
?>
<?php
function do_install() { 
    chdir(__DIR__);
    $dir = getcwd(); /** stores the directory that the php file is in as a variable */
    file_put_contents($dir . '/wordpress.zip', file_get_contents('http://wordpress.org/latest.zip'));
    $zip = new ZipArchive;
    $zip->open('wordpress.zip');
    $zip->extractTo($dir);
    $zip->close();
    unlink($dir . '/wordpress/wp-content/plugins/hello.php');
    recurseRmdir($dir . '/wordpress/wp-content/plugins/akismet');
    recurseRmdir($dir . '/wordpress/wp-content/themes/twentyfourteen');
    recurseRmdir($dir . '/wordpress/wp-content/themes/twentythirteen');
    recurseRmdir($dir . '/wordpress/wp-content/themes/twentytwelve');
    file_put_contents ($dir . '/wpbfboilerplate.zip', file_get_contents ('/home/wpbfboilerplate.zip'));
    $zip = new ZipArchive;
    $zip->open('wpbfboilerplate.zip');
    $zip->extractTo($dir);
    $zip->close();
    unlink($dir . '/wordpress.zip');
    unlink($dir . '/wpbfboilerplate.zip');
    rename($dir . '/wordpress/wp-config-sample.php', $dir . '/wordpress/wp-config.php');
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('database_name_here',$dbname, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('username_here', $dbname, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('localhost', $dbhost, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('password_here', $dbpass, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace("\$table_prefix  = 'wp_';", "\$table_prefix  =" . "'" . $prefix . "'", $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    }
?>
函数显示形式($msg=''){
?>

您的数据库名称和用户:
您的数据库密码
您的DB_主机(ip地址)
您所需的表格前缀

应该是if(isset($\u POST['dbname'])和trim($\u POST['dbname'])!=''{我只是忘记了括号!xD

你可以试试这个

if(!$_POST['dbname']){
    $data['Error']='Please enter DB Name.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['dbpass']){
    $data['Error']='Please enter DB password.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['dbhost']){
    $data['Error']='Please enter DB Host.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['prefix']){
    $data['Error']='Please enter DB Prefix.';
    show_form($data['Error']); // show the form
}else{
        do_install();
        echo 'Install Done';
}

做这样的事情,我希望上面能有所帮助。

if(isset($\u POST['dbname'))和trim($\u POST['dbname')!='')应该可以工作。
错误的原因一定是其他原因。哈哈哈,解决了它,简单愚蠢的清晨错误!应该是if(isset($\u POST['dbname'))和trim($\u POST['dbname')!=''){我只是忘记了括号!xd打开错误报告,它将为您节省大量时间。(可以关闭通知和警告)我已经解决了这个问题,不管怎么说,上面的内容都没有帮助,您的代码只检查字段数据是否已发布,但因为在PHP中,没有任何内容仍然被标识为已发布,因此它不会验证用户是否已将数据放入。无论如何,谢谢:)
if(!$_POST['dbname']){
    $data['Error']='Please enter DB Name.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['dbpass']){
    $data['Error']='Please enter DB password.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['dbhost']){
    $data['Error']='Please enter DB Host.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['prefix']){
    $data['Error']='Please enter DB Prefix.';
    show_form($data['Error']); // show the form
}else{
        do_install();
        echo 'Install Done';
}