Jump to content

Problem In Insert Opreration


hazem
 Share

Recommended Posts

If I manage to Insert Infomation to medicine table .....the Information go without insert into DateBase Addmedicine.php

<?phpinclude_once("../Include/connectionstring.php");include_once("../Repositories/Medicine.repo.php");if(isset($_POST['btnsubmit'],        $_POST['Id_Medicine'],        $_POST['Medicine_name'],        $_POST['Medicine_Level']))        {    $Idmedicine=intval($_POST['Id_Medicine']);    $MedicineName=strval($_POST['Medicine_name']);    $MedicineLevel=intval($_POST['Medicine_Level']);        $AddMedicine= new MedicineRepo();    $affectedRows=$AddMedicine->AddMedicine($MedicineName,$MedicineLevel);    $isDone = $affectedRows == 1;    if($isDone==TRUE)    $message="Operation Succeeded!";    else    $message="Operation Failed";}else$Message="ALL FIELD ARE REQUIRED";echo $Message;?>
Medicine.repo.php
<?phpinclude_once("../include/connectionstring.php");class MedicineRepo{function GetMedicine(){    $dbh=DBClass::GetConnectionString();    $sql="select * from medicine";    $stmt=$dbh->prepare($sql);    $stmt->setFetchMode(PDO::Fetchclass,'Medicine');    $stmt->execute();    return $stmt->fetchAll();    }            function AddMedicine($MedicineName,$MedicineLevel)    {    $dbh=DBClass::GetConnectionString();    $sql="insert into medicine(Medicine_name,Medicine_Level)values(:MedicineName,:MedicineLevel)";    $stmt=$dbh->prepare($sql);    $stmt->bindparm(':MedicineName',$MedicineName,PDO::PARAM_STR);    $stmt->bindparm(':MedicineLevel',$MedicineLevel,PDO::PARAM_INT);    $stmt->execute();    return $stmt->rowcount();    }          function DeleteMedicine($IdMedicine){     $dbh=DBClass::GetConnectionString();     $sql="delete from Medicine where Id_Medicine=:IdMedicine";     $stmt=$dbh->prepare($sql);     $stmt->bindparm(':IdMedicine',$IdMedicine,PDO::PARM_INT);     $stmt->execute();}    }?>

AddMedicine.php

Medicine.repo.php

Edited by justsomeguy
Link to comment
Share on other sites

You're saying it doesn't update the database? What happens when you submit the form?

 

When I submit form ,information is sent but it doesn't insert into DB and doesn't show any message or error......just reload form without data

Edited by hazem
Link to comment
Share on other sites

Are you submitting to the right place? The PHP code you showed doesn't show the form again, it just prints a message. PHP variable names are case-sensitive though, $message is not the same as $Message. If it is refreshing the form then you probably aren't submitting to that PHP page.

  • Like 1
Link to comment
Share on other sites

Are you submitting to the right place? The PHP code you showed doesn't show the form again, it just prints a message. PHP variable names are case-sensitive though, $message is not the same as $Message. If it is refreshing the form then you probably aren't submitting to that PHP page.

Is There anything wrong in HTML tags?

 

<html> <body> <fieldset><legend>Add Medicune</legend> <form method="POST" action="" enctype="multipart/form-data"> Medicine name: <input type="text" name="Medicine_name"/><br /> Medicine Level: <input type="text" name="Medicine_level"/><br /> <input type="submit" name="btnsubmit" value="Add "/> <input type="reset" name="reset"/> </form> </fieldset> </body></html>

Edited by hazem
Link to comment
Share on other sites

There is no Id_Medicine, the form has no action so it's going to submit to the same page, and Medicine_level is not the same as Medicine_Level. So, the isset function in your PHP code will not return true. You should also remove the enctype unless you are uploading a file.

  • Like 1
Link to comment
Share on other sites

There is no Id_Medicine, the form has no action so it's going to submit to the same page, and Medicine_level is not the same as Medicine_Level. So, the isset function in your PHP code will not return true. You should also remove the enctype unless you are uploading a file.

 

Thank u very much for your reply ....

 

Id_medicine in DB is auto increment so I want to show Id without ability to insert to DB from User...Can you help me in this matter ?

 

I Will remove enctype from form tag but I don't know which page will be redirect in action property,,,,,,,

Link to comment
Share on other sites

Id_medicine in DB is auto increment so I want to show Id without ability to insert to DB from User...Can you help me in this matter ?

It doesn't matter how the database is structured, look at this code:
if(isset($_POST['btnsubmit'],        $_POST['Id_Medicine'],        $_POST['Medicine_name'],        $_POST['Medicine_Level']))        {    $Idmedicine=intval($_POST['Id_Medicine']);    $MedicineName=strval($_POST['Medicine_name']);    $MedicineLevel=intval($_POST['Medicine_Level']);
And look at the form:
        <form  method="POST" action="" enctype="multipart/form-data">            Medicine name:            <input  type="text" name="Medicine_name"/><br />            Medicine Level:            <input type="text" name="Medicine_level"/><br />        <input type="submit" name="btnsubmit" value="Add "/>        <input type="reset" name="reset"/>        </form>
The PHP is checking for $_POST['Id_Medicine'], but the form doesn't have a field called "Id_Medicine". You're also checking for $_POST['Medicine_Level'], but the form doesn't have that name either. So, that isset statement will always be false, because you're checking for things that are not part of the form.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...