Hello every one.
I am creating a form in php and trying to work with button in that. There are 3 buttons below then form.
1. Submit.(to insert data in BD)
2. Update.(to update data in table in DB)
3. Delete.(to delete data from table in DB)

When I apply the code, its not working fine. Someone direct me to use php functions or php statements. Please guide me how can i use "if" "else-if" or "switch" statement.
The code is shown below.

<h1>EDIT MEDICINE &amp; SURGICAL EQUIPMENT RECORD </h1>
          <p>Enter New Item's Data.</p>
          <form id="form" name="form" method="post" action="">

          <table width="563" border="0" bgcolor="#FFFFCC">

            <tr>
              <td width="261" height="56">Item ID:
                <input name="mid" type="text" /></td>
              <td width="286">Item's Name :
                <input type="text" name="mname" /></td>
            </tr>
          </table>
          <label><br />
          <br /></label>
          <label></label>
          <input name="Save" type="submit"/>
          <?php
          $con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql="INSERT INTO med_t (mid, mname)
VALUES
('$_POST[mid]','$_POST[mname]')";

if (!mysqli_query($con,$sql))
{
//die('Error:' . mysqli_error());
}
else
{
echo "1 record added";
}
mysqli_close($con);
?>
          | 
          <input name="Save2" type="submit" value="Update"/>
          <?php
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"UPDATE med_t SET mname=('$_POST[mname]')
WHERE mid=('$_POST[mid]')");


if (!mysqli_query($con,$sql))
{
//die('Error:' . mysqli_error());
}
else
{
echo "1 record Updated";
}
$url="medicine.php";
mysqli_close($con);

?>
          |
          <label></label>
          <label></label>
          <label>
          <input name="Save22" type="submit" value="Delete"/>
          <?php
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"DELETE FROM med_t WHERE mid=('$_POST[mid1]')");



if (!mysqli_query($con,$sql))
{
//die('Error:' . mysqli_error());
}
else
{
echo "1 record deleted";
}
$url="medicine.php";

mysqli_close($con);

?>
          </label>
          </form>

Thanks and Regards.
Aamir Karim.

This is not how PHP works. You cannot add code after a button, and expect it to execute when you click it. PHP is executed on the server, and HTML resides on the client. For every button you'll need to trigger some action on the server, either with a separate form, or with Javascript.

Thanks @ pritaes.
Its updated code.
Its working fine with insert button but cannot update and delete the data.

<form id="form" name="form" method="post" action="">

          <table width="563" border="0" bgcolor="#FFFFCC">

            <tr>
              <td width="261" height="56">Item ID:
                <input name="mid" type="text" /></td>
              <td width="286">Item's Name :
                <input type="text" name="mname" /></td>
            </tr>
          </table>
          <label><br />
          <br /></label>
          <label></label>
          <input name="Save" type="submit" value="Insert"/>


          | 
          <input name="Save2" type="submit" value="Update"/>

          |
          <label></label>
          <label></label>
          <label>
          <input name="Save3" type="submit" value="Delete"/>
          </label>

<?php
  if(isset($_POST['Save']))
 //code behind Submit Button
{
           $con=mysqli_connect("localhost","root","","bsc_db");
    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
        $sql="INSERT INTO med_t (mid, mname)
    VALUES
    ('$_POST[mid]','$_POST[mname]')";

    if (!mysqli_query($con,$sql))
    {
    //die('Error:' . mysqli_error());
    }
    else
    {
    echo "1 record added";
    }
    mysqli_close($con);



}

//Code Behind Update Button

elseif(isset($_post['Save2']))
{
$con=mysqli_connect("localhost","root","","bsc_db");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

mysqli_query($con,"UPDATE med_t SET mname=('$_POST[mname]')
WHERE mid=('$_POST[mid]')");


    if (!mysqli_query($con,$sql))
    {
    //die('Error:' . mysqli_error());
    }
    else
    {
    echo "1 record Updated";
    }

    mysqli_close($con);

}


//code behind delete button
elseif (isset($_post['Save3']))
{
$con=mysqli_connect("localhost","root","","bsc_db");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

mysqli_query($con,"DELETE FROM med_t WHERE mid=('$_POST[mid]')");



    if (!mysqli_query($con,$sql))
    {
    //die('Error:' . mysqli_error());
    }
    else
    {
    echo "1 record deleted";
    }


mysqli_close($con);
}  

?>
          </form>

I am not getting the point that what is preventing "Update" and "Delete" buttons from functioning.
Thanks and Regards.
Aamir Karim.

Enable lines 73 and 101. I think your queries are generating an error. I think the parenthesis are causing the problem.

when i enable these lines then it generate warning and the whole code below these lines can not execute. means in design view, no tables or forms show of they are below these lines.
Check this code. Here insert query is working, Update is working but when i press delete key, so it deletes the doctor name but doctor id is still shown in the physician's id filed. Also, when i check the database, the same situation is happening in mysql.

<?php
do
            {

            $con=mysqli_connect("localhost","root","","bsc_db");
        // Check connection
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $sql="INSERT INTO med_t (mid, mname)
        VALUES
        ('$_POST[mid]','$_POST[mname]')";

        if (!mysqli_query($con,$sql))
        {
        //die('Error:' . mysqli_error());
        }
        else
        {
        echo "1 record added";
        }
        mysqli_close($con);



            }

while (isset($_post['Save']));
?>

<?php
//NEXT CODE FOR UPDATE
do
    {
    $con=mysqli_connect("localhost","root","","bsc_db");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    mysqli_query($con,"UPDATE med_t SET mname=('$_POST[mname]')
    WHERE mid=('$_POST[mid]')");


    if (!mysqli_query($con,$sql))
    {
    //die('Error:' . mysqli_error());
    }
    else
    {
    echo "1 record Updated";
    }

    mysqli_close($con);

    }
while (isset($_post['Save2']));
?>
<?php


//CODE AFTER DELETE

do
{
    $con=mysqli_connect("localhost","root","","bsc_db");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    mysqli_query($con,"DELETE FROM med_t WHERE mid=('$_POST[mid]')");



    if (!mysqli_query($con,$sql))
    {
    //die('Error:' . mysqli_error());
    }
    else
    {
    echo "1 record deleted";
    }

mysqli_close($con);

}   
while (isset($_post['Save3']));

?>

Any idea, where i am making a mistake that 2 buttons are working and the third one isn't?
I ahev tried it with if, else if, do while statements. but same result.

Thanks & Regards.

Did you rename 'Save22' to 'Save3' ?

Yes. I have total 3 buttons and i have named the Save, Save2 and Save 3 respectively.
Tried with if, elseif but not working. I thing I have noticed is that if i take 2 buttons then, 2nd one doesn't works, if 3 then third one is out of order. I think i m making such mistake at the very end or at the very bigining.

Make sure that the HTML names you use exactly match the PHP code:

<input name="Save" type="submit"/>
<input name="Save2" type="submit"/>
<input name="Save3" type="submit"/>

$_POST['Save'];
$_POST['Save2'];
$_POST['Save3'];

I have done the same. I think i have to recode the whole scene, that might solve the issue. if yes, then i'll match the working and non-working codes and will find the mistake.

Is there any body who can give me an example that how to run php code when a button is clicked in html form?
(OR)
How to control html form's button through php code?

Thanks and Regards.
Aamir Karim.

OR IS THERE ANY OTHER WAY THEN JAVA? THROUGH WHICH I CAN CONTROL THESE BUTTONS AND THEN RUN PHP CODE AFTER PRESSING ANY BUTTON RESPECTIVLY.

Here is the page print view.
When I enter the medicine id and name, it works. When I press update button, the query updated. But when i press Delete button, The medicine name deleted but the medicine id remains when page refreshes. It means the when page is refreshing, The insert button code is also executing.[Click Here]for image of that page which shows the problem itself.(null) How can i prevent the re-execution of auto execution of insert query?

Is there any body who can give me an example that how to run php code when a button is clicked in html form?

Search the Javascript forum, there are a lot of AJAX examples/questions about this.

I think it will direct me to java coding?
While I am searching a way to do the work with buttons or hyperlinks(if possible).

I assume you have this:

<form method="post" action="yourphpscript.php">
    <input name="Save" type="submit"/>
    <input name="Save2" type="submit"/>
    <input name="Save3" type="submit"/>
</form>

Is this not working for you? If not, try to explain the problem a little more clearly.

i have a lot in the form. Not only buttons but textfileds also.
Let me paste fresh code of the form.

          <form id="form" name="form" method="post" action="medicine.php">

          <table width="563" border="0" bgcolor="#FFFFCC">

            <tr>
              <td width="261" height="56">Item ID:
                <input name="mid" type="text" /></td>
              <td width="286">Item's Name :
                <input type="text" name="mname" /></td>
            </tr>
          </table>
          <label><br />
          <br />
          </label>
          <label></label>
          <input name="Save" type="submit" value="Insert"/>
<?php
if(isset($_POST['Save']))
//code behind Submit Button
{
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO med_t (mid, mname)
VALUES
('$_POST[mid]','$_POST[mname]')";
if (!mysqli_query($con,$sql))
{
//die('Error:' . mysqli_error());
}
//else

{
echo "";
}
mysqli_close($con);
}

?>


          | 
          <input name="Save2" type="submit" value="Update"/>


<?php
//Code Behind Update Button
if(isset($_post['Save2']))
{
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE med_t SET mname=('$_POST[mname]')
WHERE mid=('$_POST[mid]')");
//if (!mysqli_query($con,$sql))
{
//die('Error:' . mysqli_error());
}
//else

echo "";

mysqli_close($con);
}
?>
          | 
          <label>
          <input type="submit" name="save3" value="Delete" />

<?php
//code behind delete button
if (isset($_post['Save3']))
{
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM med_t WHERE mid=('$_POST[mid]')");
if (!mysqli_query($con,$sql))
{
//die('Error:' . mysqli_error());
}
//else
{
echo "";
}
mysqli_close($con);

}

?>

          </label>
          </form>

I have tried too much ways but not getting the point. When I press delete button, It delete the madicine name whos id is taken from a textfield named "mid", but on the contrary it perform insert query. And it inserts only the medicine id. I am searching how to take control of the buttons under mine. Means the selected php code "INSERT","UPDATE"and"DELETE" should execute only when i need it to run and click a button but the php code is executing on its own because i don't know how to control this code with button or something else.

And yes. you are right. I have the problem which you have described in you last reply.

I have got the point.

All three buttons "INSERT","DELETE" and "UPDATE" do the same actions.
Means I have comment the code of INSERT and UPDATE and leave the code of DELETE query active. Then I execute the page so all the three buttons were deleting the records.

Please guide me how can I control the buttons?
I mean only exact code should be executed what I need.

Regards.

Ok. Its doen. Got separate forms for Insert, Delete and Update with diffirent text field names.
Its working now. Thanks for all your support.
Regards and Thanking you again. I appreciate your time.
Here is the whole code.

<form id="form" name="form" method="post" action="">

          <table width="563" border="0" bgcolor="#FFFFCC">

            <tr>
              <td width="261" height="56">Item ID:
                <input name="mid" type="text" /></td>
              <td width="286">Item's Name :
                <input type="text" name="mname" /></td>
            </tr>
          </table>
          <label><br />
          <br />
          </label>
          <label></label>

<?php
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO med_t (mid,mname)
VALUES
('$_POST[mid]','$_POST[mname]')";

if (!mysqli_query($con,$sql))
  {
  //die('Error: ' . mysqli_error());
  }
echo "";

mysqli_close($con);
?> 

<input name="Save" type="submit" value="Insert"/>
          </form>


          <label></label>
          <p>&nbsp;</p>
          <p class="style1">Enter Medicine/ Surgical Equipment ID number to delete its record.</p>
          <form id="form1" name="form1" method="post" action="medicine.php">
            <table width="563" border="0" bgcolor="#FFFFCC">
              <tr>
                <td height="56"><div align="center">Item ID:
                  <input name="mid2" type="text" />
                </div></td>
                </tr>
            </table>
            <input type="submit" name="save3" value="Delete" />
            <?php
//code behind delete button
//if (isset($_post['Save3']))
//{
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"DELETE FROM med_t WHERE mid=('$_POST[mid2]')");

mysqli_close($con);

//}

?>
</form>
          <p>&nbsp;</p>
          <p> 

            <br>
            </p>
          <label></label>
          <label> </label>
          <label>List of Medicine and Surgical Equipments you have in your Data Base.<br />
            </label>
            <label><br />
            </label>
            <table width="557" height="62" border="1" bgcolor="#FFFFCC">
              <tr>
                <td width="255" height="34"><div align="left"><strong>Item's ID :                    &nbsp;</strong></div></td>
                <td width="278"><label> <strong>Item's Name : </strong> </label></td>
              </tr>
              <tr>
                <td><?php
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM med_t");

while($row = mysqli_fetch_array($result))
  {
  echo $row['mid'];
  echo "<br />";
  }

mysqli_close($con);
?>
                  &nbsp;</td>
                <td><?php
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM med_t");

while($row = mysqli_fetch_array($result))
  {
  echo $row['mname'];
  echo "<br />";
  }

mysqli_close($con);
?>                  &nbsp;</td>
              </tr>
            </table>
            <label><br />
            </label>
          </form>

Here Fields that are inserting data in form.
Then field with different text filed name in form1 for delete.
Form3 has data that is fetching data from the SQL Table.

GOD bless you...

commented: Thanks for sharing. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.