简体   繁体   中英

reason why sqlstate[hy093] error is happening? when all conditions are met

i'm trying to figure out whats wrong with this code i got from this forum basically i was trying to modify it and see if i could solve this error above which has also come with an undefined index error.my question is why the errors because all seems to be fine and the binding is ok if i replace the POST with the GET in the if(isset($_POST['btn-update'])) the error is gone but nothing is happening.im trying to understand why the above sqlstate[hy093] error and the undefined index error on $id = $_POST['edit_id']; which is defined in the crud function

public function getID($id)
    {
        $stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE id=:id");
        $stmt->execute(array(":id"=>$id));
        $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
        return $editRow;
    }

the binding in the crud class seems to be ok

<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

class crud
{
    private $db;

    function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function create($fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity)
    {
        try
        {
            $stmt = $this->db->prepare("INSERT INTO tbl_users(first_name,last_name,employee_nrc, phone_no, business_type ,business_site ,business_activity) VALUES(:fname, :lname, :employee_nrc, :Phone, :Businesstype, :Businesssite, :Businessactivity)");
            $stmt->bindparam(":fname",$fname);
            $stmt->bindparam(":lname",$lname);
            $stmt->bindparam(":employee_nrc",$employee_nrc);
            $stmt->bindparam(":Phone",$Phone);
            $stmt->bindparam(":Businesstype",$Businesstype);
            $stmt->bindparam(":Businesssite",$Businesssite);
            $stmt->bindparam(":Businessactivity",$Businessactivity);
            $stmt->execute();
            return true;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }

    }

    public function getID($id)
    {
        $stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE id=:id");
        $stmt->execute(array(":id"=>$id));
        $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
        return $editRow;
    }

    public function update($id,$fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivityt)
    {
        try
        {
            $stmt=$this->db->prepare("UPDATE tbl_users SET first_name=:fname, 
                                                       last_name=:lname, 
                                                       employee_nrc=:employee_nrc,
                                                       phone_no=:Phone,
                                                       business_type=:Businesstype,
                                                       business_site=:Businesssite,
                                                       business_activity=:Businessactivity
                                                    WHERE id=:id ");
            $stmt->bindparam(":fname",$fname);
            $stmt->bindparam(":lname",$lname);
            $stmt->bindparam(":employee_nrc",$employee_nrc);
            $stmt->bindparam(":Businesstype",$Phone);
            $stmt->bindparam(":Businesstype",$Businesstype);
            $stmt->bindparam(":Businesssite",$Businesssite);
            $stmt->bindparam(":Businessactivity",$Businessactivity);
            $stmt->bindparam(":id",$id);
            $stmt->execute();

            return true;    
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }
    }

    public function delete($id)
    {
        $stmt = $this->db->prepare("DELETE FROM tbl_users WHERE id=:id");
        $stmt->bindparam(":id",$id);
        $stmt->execute();
        return true;
    }

    /* paging */

    public function dataview($query)
    {
        $stmt = $this->db->prepare($query);
        $stmt->execute();

        if($stmt->rowCount()>0)
        {
            while($row=$stmt->fetch(PDO::FETCH_ASSOC))
            {
                ?>
                <tr>
                <td><?php print($row['id']); ?></td>
                <td><?php print($row['first_name']); ?></td>
                <td><?php print($row['last_name']); ?></td>
                <td><?php print($row['employee_nrc']); ?></td>
                <td><?php print($row['phone_no']); ?></td>
                <td><?php print($row['business_type']); ?></td>
                <td><?php print($row['business_site']); ?></td>
                <td><?php print($row['business_activity']); ?></td>

                <td align="center">
                <a href="edit-data.php?edit_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
                </td>
                <td align="center">
                <a href="delete.php?delete_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-remove-circle"></i></a>
                </td>
                </tr>
                <?php
            }
        }
        else
        {
            ?>
            <tr>
            <td>Nothing here...</td>
            </tr>
            <?php
        }

    }

    public function paging($query,$records_per_page)
    {
        $starting_position=0;
        if(isset($_GET["page_no"]))
        {
            $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
        $query2=$query." limit $starting_position,$records_per_page";
        return $query2;
    }

    public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><ul class="pagination"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
                $previous =$current_page-1;
                echo "<li><a href='".$self."?page_no=1'>First</a></li>";
                echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
                }
                else
                {
                    echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
                }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
                echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
            }
            ?></ul><?php
        }
    }

    /* paging */

}

the edit page where the undefined index error is occurring is $id = $_POST['edit_id'];

<?php
include_once 'dbconfig.php';
$first_name= null;
$last_name=null;
$employee_nrc=null;
$phone_no=null;
$business_type=null;
$business_site=null;
$business_activity=null;

if(isset($_POST['btn-update']))
{
    $id = $_POST['edit_id'];
    $fname = $_POST['first_name'];
    $lname = $_POST['last_name'];
    $employee_nrc = $_POST['employee_nrc'];
    $Phone = $_POST['phone_no'];
    $Businesstype = $_POST['business_type'];
    $Businesssite = $_POST['business_site'];
    $Businessactivity = $_POST['business_activity'];


    if($crud->update($id,$fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity))
    {
        $msg = "<div class='alert alert-info'>
                <strong>WOW!</strong> Record was updated successfully <a href='index.php'>HOME</a>!
                </div>";
    }
    else
    {
        $msg = "<div class='alert alert-warning'>
                <strong>SORRY!</strong> ERROR while updating record !
                </div>";
    }
}

if(isset($_GET['edit_id']))
{
    $id = $_GET['edit_id'];
    extract($crud->getID($id)); 
}

?>
<?php include_once 'header.php'; ?>

<div class="clearfix"></div>

<div class="container">
<?php
if(isset($msg))
{
    echo $msg;
}
?>
</div>

<div class="clearfix"></div><br />

<div class="container">

     <form class="form-horizontal" method='post'>
  <fieldset>
    <legend>Registration System</legend>
    <div class="form-group">
      <label for="inputFirstName" class="col-lg-2 control-label">First Name</label>
      <div class="col-lg-10">
        <input type="text" name="first_name" class="form-control" value="<?php echo $first_name; ?>" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputLastName" class="col-lg-2 control-label">Last Name</label>
      <div class="col-lg-10">

        <input type="text" name="last_name" class="form-control" value="<?php echo $last_name; ?>" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmployeeNRC" class="col-lg-2 control-label">Employee NRC</label>
      <div class="col-lg-10">
        <input type="text" name="employee_nrc" class="form-control" value="<?php echo $employee_nrc; ?>" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Employee Phone Number</label>
      <div class="col-lg-10">
        <input type="text" name="phone_no" class='form-control' value="<?php echo $phone_no; ?>" required>
      </div> 
    </div>

    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Type</label>
      <div class="col-lg-10">
        <input type="text" name="business_type" class='form-control' value="<?php echo $business_type; ?>" required>
      </div> 
    </div>

    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Location</label>
      <div class="col-lg-10">
        <input type="text" name="business_site" class='form-control' value="<?php echo $business_site; ?>" required>
      </div> 
    </div>

    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Activities</label>
      <div class="col-lg-10">
        <input type="text" name="business_activity" class='form-control' value="<?php echo $business_activity; ?>" required>
      </div> 
    </div>


    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="submit" class="btn btn-primary" name="btn-update">
                <span class="glyphicon glyphicon-edit"></span>  Update this Record
                </button>
                <a href="index.php" class="btn btn-large btn-success"><i class="glyphicon glyphicon-backward"></i> &nbsp; CANCEL</a>
      </div>
    </div>
  </fieldset>
</form>


</div>

<?php include_once 'footer.php'; ?>

help identify the mistake and advice doing this for learning purposes and i would appreciate ideas and solutions

there is a type which was later corrected on this code

 $stmt->bindparam(":Businesstype",$Phone);

it was supposed to be

$stmt->bindparam(":Phone",$Phone);

thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM