Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    pdo error handling

    Hi guys im having some problems with a pdo insert statement and i was hoping soeone might be able to help me figure out whats wrong maybe with the use of some error handling techniques. all the code is contained in one page excluding the db connect which is in a seperate file. heres the db connect file

    PHP Code:
    <?php

    $DSN 
    "mysql:host = localHost; dbname=cms";
    $connect = new PDO($DSN'root''');
    ?>
    heres the first bit of php from the main page

    PHP Code:
    <?php require_once("includes/db.php");?>
    <?php 
    require_once("includes/functions.php");?>
    <?php 
    require_once("includes/sessions.php");?>
    <?php
    ini_set
    ('display_errors'true); // set to false in production
    error_reporting(E_ALL);
    ?>

    <?php


      
    if(isset($_POST["submit"])){
        
    $postTitle $_POST["postTitle"];
        
    $category  $_POST["category"];
        
    $image     $_FILES["image"]["name"];
        
    $target    "uploads/".basename($_FILES["image"]["name"]);
        
    $postDescription $_POST["postDescription"];
        
    $admin "cole";
        
    date_default_timezone_set("Europe/Dublin");
        
    $currenttime time();
        
    $datetimestrftime("%B-%d-%Y %H:%M:%S"$currenttime);

        if(empty(
    $postTitle)){
          
    $_SESSION["error"] = "please add a post title";
          
    redirect("addnewpost.php");
        }elseif(
    strlen($postTitle)<5){
          
    $_SESSION["error"] = "post title must be longer than 5 charachters";
          
    redirect("addnewpost.php");
        }elseif(
    strlen($postDescription)>999){
          
    $_SESSION["error"] = "post must be less than 1000 charachters";
          
    redirect("addnewpost.php");
        }else {
          
    $sql "INSERT INTO posts(datetime,title,category,author,image,post) VALUES(:entrytime,:postTitle,:postCategory,:adminName,:pic,:postText)";
          
    $stmt $connect->prepare($sql);
          
    $stmt->bindValue(':entrytime',$datetime);
          
    $stmt->bindValue(':postTitle',$postTitle);
          
    $stmt->bindValue(':postCategory',$category);
          
    $stmt->bindValue(':admiNname',$admin);
          
    $stmt->bindValue(':pic',$image);
          
    $stmt->bindValue(':postText',$postDescription);
          
    $Execute $stmt->execute();
        


          if(
    $Execute){
            
    $_SESSION["success"] = "data added successfully";
            
    redirect("addnewpost.php");
          }else {
        
    print_r($connect->errorInfo());
            
    $_SESSION["error"] = "something went wrong. data not added to table";
            
    redirect("addnewpost.php");
          }
        }
      }




    ?>
    heres the first part of the html

    PHP Code:
    <!doctype html>
    <html>

      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-compatible" content="ie=edge">
        <title>Add new post</title>
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <link rel="stylesheet" href="css/styles.css">
      </head>

      <body>

        <!-- NAVBAR START -->
        <div style="height:10px; background-color:rgb(100,100,100);"></div>
          <nav class="navbar navbar-expand-lg bg-dark navbar-dark">
            <div class="container">
              <a href="#" class="navbar-brand">jazebakram.com</a>
              <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarcollapsecms"><span class="navbar-toggler-icon"></span></button>
              <div class="collapse navbar-collapse" id="navbarcollapsecms">
              <ul class="navbar-nav mr-auto">
                <li class="nav-item"><a href="myprofile.php" class="nav-link"> <i class="fas fa-user"></i> Profile</a></li>
                <li class="nav-item"><a href="dashboard.php" class="nav-link">Dashboard</a></li>
                <li class="nav-item"><a href="posts.hp" class="nav-link">Posts</a></li>
                <li class="nav-item"><a href="catagories.php" class="nav-link">Catagories</a></li>
                <li class="nav-item"><a href="admins.php" class="nav-link">Manage admins</a></li>
                <li class="nav-item"><a href="comments.php" class="nav-link">Comments</a></li>
                <li class="nav-item"><a href="blog.php?page=1" class="nav-link">Live blog</a></li>
              </ul>
              <ul class="navbar-nav ml-auto">
                <li class="nav-item"><a href="logout.php" class="nav-link"><i class="fas fa-user-times"></i> Log out</a></li>
              </ul>
              </div>
            </div>
          </nav>
        <div style="height:10px; background-color:rgb(100,100,100);"></div>
        <!-- NAVBAR END -->

        <!-- HEADER START -->
        <header class="bg-dark text-white">
          <div class="container">
            <div class="row">
              <h1> <i class="fas fa-edit style="color: #27aae1;"></i> Manage posts</h1>
            </div>
          </div>
        </header>
        <!-- HEADER END -->

        <!-- MAIN AREA -->
        <section class="container py-2 mb-4">
          <div class="row">
            <div class="offset-lg-1 col-lg-10" style="min-height: 600px;">
              <?php
              
    echo errormessage();
              echo 
    successmessage();
              
    ?>
              <form action="addnewpost.php" method="post" enctype="multipart/form-data">
                <div class="card bg-secondary text-light ">

                </div>
                <div class="card-body bg-dark" >
                  <div class="form-group">
                    <label for="postTitle"><span class="fieldinfo">post title:</span></label>
                    <input class="form-control"  type="text" name="postTitle" id="postTitle">
                  </div>
                  <div class="form-group">
                    <label for="category title"><span class="fieldinfo">choose Category:</span></label>
                    <select class="form-control" id="categoryTitle" name="category">
    here i used php to populate the select element (which works fine
    PHP Code:
    <?php

                        
    global $connect;

                        
    $sqlT "SELECT * FROM category";
                        
    $stmtT $connect->query($sqlT);
                        while (
    $daterows $stmtT->fetch()){
                            
    $categoryname $daterows["title"];
                            echo 
    "<option> $categoryname </option>";
                          }
                            
    ?>
    and heres the rest of the html

    PHP Code:
    </select>
                  </
    div>
                  <
    div class="form-group">
                      <
    div class="custom-file">
                        <
    input  type="file" name="image" id="imageselect" value="">
                        <
    label for="imageselect"class="custom-file-label">Select image</label>
                      </
    div>
                    </
    div>
                    <
    div class="form-group">
                      <
    label for="post"><span class="fieldinfo">Post:</span></label>
                      <
    textarea class="form-control" id="post" name="postDescription" rows="8" cols="80"></textarea>
                    </
    div>
                  <
    div class="row">
                    <
    div class="col-lg-6 mb-2">
                      <
    a href="dashboard.php" class="btn btn-warning btn-lg btn-block"><class="fas fa-arrow-left">Back to dashboard</i></a>
                    </
    div>
                    <
    div class="col-lg-6 mb-2">
                      <
    button type="submit" name="submit" class="btn btn-success btn-lg btn-block"><class="fas fa-check">Publish</i></button>
                    </
    div>
                  </
    div>
                </
    div>
              </
    form>
            </
    div>
          </
    div>
        </
    section>


        <!-- 
    FOOTER START -->
        <
    footer class="bg-dark text-white">
          <
    div class-"container">
            <
    div class="row">
              <
    div class="col">
                <
    class="lead text-center">Theme by Cole Pratt</p>
              </
    div>
            </
    div>
          </
    div>
        </
    footer>

        <
    script src="https://code.jquery.com/jquery-3.3.1.slim.min.jsintegrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
      </body>



    </html> 
    any help greatly appreciated

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    @busso ...

    I've seen you do this before, and you really have to be careful that you double-check everything.
    You make typos and there could possibly be more than what I found.

    Code:
          $sql = "INSERT INTO posts(datetime,title,category,author,image,post) VALUES(:entrytime,:postTitle,:postCategory,:adminName,:pic,:postText)"; 
          $stmt = $connect->prepare($sql); 
          $stmt->bindValue(':entrytime',$datetime); 
          $stmt->bindValue(':postTitle',$postTitle); 
          $stmt->bindValue(':postCategory',$category); 
          $stmt->bindValue(':admiNname',$admin); 
          $stmt->bindValue(':pic',$image); 
          $stmt->bindValue(':postText',$postDescription); 
          $Execute = $stmt->execute();
    Maybe you can find another person who can look through your scripts and spots errors like these.
    If your webhost is set up to create "error log" files, you can find the description of the error in those logs.



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:07 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com