5

I am in the midst of completing a uni assignment and ive come across the issue of being totally stumped on how to update and delete data in my database through the use of a form.

I have successfully connected the database so that when the user selects the the addressID in the previous page they wish to work on, it will take them to the updateform.php page.

My code for the form is as follows:

<form method="post">
<tr>
    <td>Firstline</td>
    <td><input type="text" name="firstline" class="form-control"/></td>
</tr>
<tr>
    <td>Secondline</td>
    <td><input type="text" name="secondline" class="form-control"/></td>
</tr>
<tr>
    <td>City</td>
    <td><input type="text" name="city" class="form-control"/></td>
</tr>
<tr>
    <td>State</td>
    <td><input type="text" name="state" class="form-control"/></td>
</tr>
    <tr>
    <td>Zip</td>
    <td><input type="text" name="zip" class="form-control"/></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
    <tr>
    <td></td>
    <td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>
</tr>

I am now completely stumped on how to connect the data they enter in the form to update the database.

I am trying to update the addresses table with the selected addressID chosen earlier if that helps.

Any push in the correct direction will be greatly appeciated.

Kind Regards

2 Answers 2

2

Change these line:

<form method="post">

to

<form method="post" action="process.php">

and

<input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/>

to

<input type="submit" name="update" value="Update" class="btn btn-success btn-lg"/>

and

<input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/>

to

<input type="delete" name="delete" value="Delete" class="btn btn-success btn-lg"/>

process.php:

if(isset($_REQUEST['update']))
{
    // update block
    // get all required value and fire update query
}

if(isset($_REQUEST['delete']))
{
    // delete block
    // get all required value and fire delete query
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Ill give it a crack.
i am completely stumped on how to complete this process. Any ideas for me to get on my way?
0

You could try something like this:

updateform.php

    <?php

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

strip tags from user input:

    $firstline= trim(strip_tags($_POST['first_line']));
    $secondline= trim(strip_tags($_POST['second_line']));

create query:

    $myquery = "INSERT INTO tablename (firstline, secondline) 
                VALUES ($firstline', '$secondline')";

execute query (confirm success/failure if/else):

    if (@mysqli_query($database, $myquery)) {
    print '<p>Entries accepted</p>';
    }

    else    {
    print '<p style="color: red;">Could not INSERT values because:<br />'
    . mysqli_error($database) . '</p>;
   }

   }

   ?>

Then have your form post values for the query variables:

<form action="updateform.php" method="post">
    <tr>
    <td>Firstline</td>
    <td><input type="text" name="firstline" size="20" value= <?php  if (isset($_POST['firstline'])) {
                                                                                print htmlspecialchars($_POST['first_line']);
                                                                                } ?> /></td>

    <tr>
    <td>Secondline</td>
    <td><input type="text" name="secondline" size="20" value= <?php if (isset($_POST['secondline'])) {
                                                                                print htmlspecialchars($_POST['second_line']);
                                                                                } ?> /></td>

    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>

3 Comments

Im getting a whole bunch of undefined index errors with the strip tags.
The first line of the php code should check request method is post:... if ($_SERVER['REQUEST_METHOD'] == 'POST') { ...and then close that bracket just before closing the ?> bracket... I will edit the code above for you.
Should solve the undefined indexes...my fault for forgetting that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.