未捕获的PDOException

时间:2017-10-01 16:19:04

标签: php mysql pdo

当我运行以下代码时,我收到此错误:

  

致命错误:未捕获PDOException:SQLSTATE [HY093]:参数号无效:绑定变量数与C:\ xampp \ htdocs \ pizda \ aids \ admin \ addnew.php中的标记数不匹配:73堆栈跟踪: #0   第73行

<?php

error_reporting( ~E_NOTICE ); // avoid notice

require_once 'dbconfig.php';

if(isset($_POST['btnsave']))
{
    $NaslovSrpski = $_POST['NaslovSrpski'];
    $NaslovEngleski = $_POST['NaslovEngleski'];
    $TekstSrpski = $_POST['TekstSrpski'];
    $TekstEngleski = $_POST['TekstEngleski'];


    $imgFile = $_FILES['user_image']['name'];
    $tmp_dir = $_FILES['user_image']['tmp_name'];
    $imgSize = $_FILES['user_image']['size'];


    if(empty($NaslovSrpski)){
        $errMSG = "Please Enter Username.";
    }
    else if(empty($NaslovEngleski)){
        $errMSG = "Please Enter Your Job Work.";
    }
    else if(empty($TekstSrpski)){
        $errMSG = "Please Enter Your Job Work.";
    }
    else if(empty($TekstEngleski)){
        $errMSG = "Please Enter Your Job Work.";
    }
    else if(empty($imgFile)){
        $errMSG = "Please Select Image File.";
    }
    else
    {
        $upload_dir = '../uploads/'; // upload directory

        $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

        // valid image extensions
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

        // rename uploading image
        $userpic = rand(1000,1000000).".".$imgExt;

        // allow valid image file formats
        if(in_array($imgExt, $valid_extensions)){           
            // Check file size '5MB'
            if($imgSize < 5000000)              {
                move_uploaded_file($tmp_dir,$upload_dir.$userpic);
            }
            else{
                $errMSG = "Sorry, your file is too large.";
            }
        }
        else{
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
        }
    }


    // if no error occured, continue ....
    if(!isset($errMSG))
    {
        $stmt = $DB_con->prepare('INSERT INTO Obavestenja(NaslovSrpski,NaslovEngleski,TekstSrpski,TekstEngleski, Slika) VALUES(:naslovs, :naslove, :teksts, tekste, :upic)');
        $stmt->bindParam(':naslovs',$NaslovSrpski);
        $stmt->bindParam(':naslove',$NaslovEngleski);
        $stmt->bindParam(':teksts',$TekstSrpski);
        $stmt->bindParam(':tekste',$TekstEngleski);
        $stmt->bindParam(':upic',$userpic);

        if($stmt->execute())
        {
            $successMSG = "new record succesfully inserted ...";
            header("refresh:5;index.php"); // redirects image view page after 5 seconds.
        }
        else
        {
            $errMSG = "error while inserting....";
        }
    }
}
include 'header.php';
?>


<div class="container">


    <div class="page-header">
        <h1 class="h2">add news. <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-eye-open"></span> &nbsp; view all </a></h1>
    </div>


    <?php
    if(isset($errMSG)){
            ?>
            <div class="alert alert-danger">
                <span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
            </div>
            <?php
    }
    else if(isset($successMSG)){
        ?>
        <div class="alert alert-success">
              <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
        </div>
        <?php
    }
    ?>   

<form method="post" enctype="multipart/form-data" class="form-horizontal">

    <table class="table table-bordered table-responsive">

    <tr>
        <td><label class="control-label">NaslovSrpski</label></td>
        <td><input class="form-control" type="text" name="NaslovSrpski" placeholder="NaslovSrpski" value="<?php echo $NaslovSrpski; ?>" /></td>
    </tr>

    <tr>
        <td><label class="control-label">NaslovEngleski</label></td>
        <td><input class="form-control" type="text" name="NaslovEngleski" placeholder="NaslovEngleski" value="<?php echo $NaslovEngleski; ?>" /></td>
    </tr>
     <tr>
        <td><label class="control-label">TekstSrpski</label></td>
        <td><textarea class="form-control" type="text" name="TekstSrpski" placeholder="TekstSrpski" value="<?php echo $TekstSrpski; ?>" /></textarea></td>
    </tr>
     <tr>
        <td><label class="control-label">TekstEngleski</label></td>
        <td><textarea class="form-control" type="text" name="TekstEngleski" placeholder="TekstEngleski" value="<?php echo $TekstEngleski; ?>" /></textarea></td>
    </tr>

    <tr>
        <td><label class="control-label">Profile Img.</label></td>
        <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
    </tr>

    <tr>
        <td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
        <span class="glyphicon glyphicon-save"></span> &nbsp; save
        </button>
        </td>
    </tr>

    </table>

</form>

1 个答案:

答案 0 :(得分:2)

你在tekste的占位符中遗漏了一个“:”:

$stmt = $DB_con->prepare
        ('INSERT INTO Obavestenja' .
         ' (NaslovSrpski,NaslovEngleski,TekstSrpski,TekstEngleski, Slika)' .
         ' VALUES (:naslovs, :naslove, :teksts, :tekste, :upic)');
         # ":" was missing here ----------------^