不工作。通过PHP验证和清理工作突然无法正常工作

时间:2016-04-10 05:48:27

标签: php mysql

我有一个奇怪的问题。首先,在我通过PHP进行表单验证之前,我可以插入并显示数据(我至少有30个字段)。然后在我进行验证和消毒后,我突然无法插入和显示数据。删除数据库中的一些字段和列后,我留下了一些,现在我可以插入数据和显示数据,但是如果我添加的数量超过5或6个字段,我就无法插入数据。请告诉我出了什么问题?

<?php
echo var_dump($_POST);
echo var_dump($_FILES);
print_r($_SESSION);
error_reporting(E_ALL);
ini_set("display_errors",1);
//define variables and define to null.
$adtitleError = $dcrptnError = $rmError = $advertnameError = $apE = "";
$adtitle = $dcrptn = $rm = $advertname = $ap = "";
//Retrieve the field values from registration form.
$adtitle = !empty($_POST ['adtitle']) ? trim($_POST['adtitle']) : null;
$dcrptn = !empty($_POST ['dcrptn']) ? trim($_POST['dcrptn']) : null;
$rm = !empty($_POST ['rm']) ? trim($_POST['rm']) : null;
$advertname = !empty($_POST ['advertname']) ? trim($_POST['advertname']) : null;
$ap = !empty($_POST ['adphone']) ? trim($_POST['adphone']) : null;
function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
$formValid = true;
if(isset($_POST["submit"])){
    //insert record
    if($conn->connect_error)
    {die("Connection failed:".$conn->connect_error);}
    $id=isset($_POST['id'])?$_POST['id']:"";
    //insert data
    $statement = $conn->prepare("INSERT INTO useradvert(id,image1,adtitle,dcrptn,rm,advertname,adphone)VALUES (?,?,?,?,?,?,?)");
    //bind param
    $statement->bind_param("issssss",$id,$target_file,$adtitle,$dcrptn,$rm,$advertname,$ap);
    $target_file=isset($_FILES['image'])?$_FILES['image']:"";
    $adtitle=isset($_POST['adtitle'])?$_POST['adtitle']:"";
    $dcrptn=isset($_POST['dcrptn'])?$_POST['dcrptn']:"";
    $rm=isset($_POST['rm'])?$_POST['rm']:"";
    $advertname=isset($_POST['advertname'])?$_POST['advertname']:"";
    $ap=isset($_POST['adphone'])?$_POST['adphone']:"";
    //bind the variables to be called at other places
    if (empty($adtitle)){
        $adtitleError = "Ad title is required. Select category to activate form.";
        $formValid = false;
    }else{
        $adtitle = test_input($_POST["adtitle"]);
        // check name only contains letters and whitespace
        if (!preg_match('/^[a-zA-Z\s]{3,50}+$/', $adtitle)) {
            $adtitleError = "Letters only & spaces,(min 3),e.g: a, A)";
            $formValid = false;
        }
    }
    if (empty($dcrptn)){
        $dcrptnError = "Decsription is required. Select category to activate form.";
        $formValid = false;
    }
    if (empty($rm)){
        $rmError = "A value is required, e.g: 123000 or 12,300.00. Select category to activate form.";
        $formValid = false;
    }
    else{
        $rm = test_input($_POST["rm"]);
        // check name only contains letters and whitespace
        if (!preg_match('/^[0-9]+(?:\.[0-9]{1,13})?$/',$rm)) {
            $rmError = "Invalid value.E.g: 123000.45. Select category to activate form.";
            $formValid = false;
        }
    }
    if (empty($advertname)){
        $advertnameError = "Name is required. Select category to activate form.";
        $formValid = false;
    }
    else{
        $advertname = test_input($_POST["advertname"]);
        // check name only contains letters and whitespace
        if (!preg_match('/^[a-zA-Z\s]{3,50}+$/',$advertname)) {
            $advertnameError = "Letters only & spaces,(min 3),(e.g: a, A). Select category to activate form.";
            $formValid = false;
        }
    }

    if (empty($ap)){
        $apE = "Tel number is required. Select category to activate form.";
        $formValid = false;
    }
    else{
        $ap= test_input($_POST["adphone"]);
        // check name only contains letters and whitespace
        if (!preg_match('/^\d{9,11}+$/',$ap)) {
            $apE = "Invalid tel no format.( E.g:0123456789). Select category to activate form.";
            $formValid = false;
        }
    }
    //image
    $target_dir="uploads/";
    $target_file=$target_dir.basename($_FILES["image1"]["name"]);
    $uploadOk=1;
    $imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
    //script for targetfile -image
    // Check if image or not        
    $check=getimagesize($_FILES["image1"]["tmp_name"]);
    if($check!==false){
        echo "File is an image - ".$check["mime"].".";
        $uploadOk=1;
    }else{
        echo "File is not an image.";
        $uploadOk=0;
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk=0;
    }
    // Check file size
    if ($_FILES["image1"]["size"]>500000) {
        echo "Sorry, your file is too large.";
        $uploadOk=0;
    }
    // Allow certain file formats
    if($imageFileType!="jpg"&&$imageFileType!="png"&&$imageFileType!="jpeg"
        &&$imageFileType!="gif")
    {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk=0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk==0) {
        echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
    }else{
        if (move_uploaded_file($_FILES["image1"]["tmp_name"],$target_file)) {
            echo "The file ".basename($_FILES["image1"]["name"])."has been uploaded.";
        }else{
            echo "Sorry, there was an error uploading your file.";
        }
    }
    if ($formValid){
        $statement->execute();
        header('Location: userpppp.php');
        exit;
    }
}
?>

0 个答案:

没有答案