Jay-oh
Jay-oh

Reputation: 456

Have a Insert and a Edit button in the same form using PHP and SQL

I've been going at this for a couple of hours now (searching here and google) but nothing I find helped me to get this to work.

I'm trying to make one form and have a Insert INTO and UPDATE $table SET function in the same form, using buttons.

But whatever I try the Update doesn't copy the data from the form. INSERT INTO works. But when I try to edit the form, no data is copied.

HTML:

<form id="contact-form" method="post" action="cms_data.php" role="form">
    <div class="col-sm-12">
        <h2>id</h2>
        <input name="id" type="text" placeholder="<?php echo $id;?>" value="1">
    </div>
    <div class="col-sm-12">
        <h2>Omschrijving</h2>
        <textarea name="omschrijving" type="text" style="height:220px;width:100%;resize:none;"><?php echo $omschrijving;?></textarea>
    </div>
    <div class="col-sm-12">
        <h2>Datum</h2>
        <input name="datum" type="text" value="<?php echo $datum;?>">
    </div>
    <div class="col-sm-12">
        <h2>Tijd</h2>
        <input name="tijd" type="text" value="<?php echo $tijd;?>">
    </div>
    <div class="col-sm-12">
        <h2>Locatie</h2>
        <input name="locatie" type="text" value="<?php echo $locatie;?>">
    </div>
    <div class="col-sm-12">
        <h2>Dresscode</h2>
        <input name="dresscode" type="text" value="<?php echo $dresscode;?>">
    </div>
    <div class="col-sm-12 text-right">
        <input type="submit" class="btn btn-success btn-send" value="Versturen" id="sent" <?php // echo $_SESSION['disabled']; ?>>
        <a href="update-cms.php">Update </a>
    </div>
</form>

CMS_DATA.php

<?php session_start();?>
<?php
    $servername = "localhost";
    $username = "xxx";
    $password = "xxx";
    $dbname = "xxx";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    //echo '<div style="width:100%;background:green;color:#FFF;font-size:2rem;text-align:center;">Connected to '. $dbname. '</div>';

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

$id = $_POST['id'];
$omschrijving = $_POST["omschrijving"];
$datum = $_POST["datum"];
$item = $_POST["tijd"];
$locatie = $_POST["locatie"];
$dresscode = $_POST["dresscode"];

$quote = iconv("UTF-8", "WINDOWS-1252//TRANSLIT");
$date = date('Y-m-d');
date_default_timezone_set("Europe/Amsterdam");
$time = date("h:i:sa");
$sql = "INSERT INTO $table (ID, Omschrijving, Datum, Tijd, Locatie, Dresscode )
                        VALUES ('" .$id."','" .$omschrijving."','".$datum."',' ".$item."','".$locatie."','".$dresscode."')";

if ($conn->query($sql) === TRUE) {
    echo "";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

UPDATE-CMS.php

<?php session_start();?>
<?php
    $servername = "localhost";
    $username = "xxx";
    $password = "xxx";
    $dbname = "xxx";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    //echo '<div style="width:100%;background:green;color:#FFF;font-size:2rem;text-align:center;">Connected to '. $dbname. '</div>';

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

$id = $_POST['id'];
$omschrijving = $_POST["omschrijving"];
$datum = $_POST["datum"];
$item = $_POST["tijd"];
$locatie = $_POST["locatie"];
$dresscode = $_POST["dresscode"];

$quote = iconv("UTF-8", "WINDOWS-1252//TRANSLIT");
$date = date('Y-m-d');
date_default_timezone_set("Europe/Amsterdam");
$time = date("h:i:sa");
$sql = "UPDATE $table SET
    Omschrijving = '$omschrijving', Datum = '$datum', Tijd = '$item', Locatie = '$locatie', Dresscode = '$dresscode' WHERE ID = '1'";

if ($conn->query($sql) === TRUE) {
    echo "Done";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Like I said, the INSERT INTO works fine. But no data (values) are copied when using the update. I just overrides ID 1 with empty rows... I hope someone can help me... thanks in advance.

Upvotes: 1

Views: 1367

Answers (3)

jasinth premkumar
jasinth premkumar

Reputation: 1413

anchor just links the page it will not pass data

you are trying to have submit and update button in one form

solution:

in html5 button has formaction attribute .formaction specifies page data to be transferred .so that different button can have different action page

<form id="contact-form" method="post" action="cms_data.php" role="form">
    <div class="col-sm-12">
        <h2>id</h2>
        <input name="id" type="text" placeholder="<?php echo $id;?>" value="1">
    </div>
    <div class="col-sm-12">
        <h2>Omschrijving</h2>
        <textarea name="omschrijving" type="text" style="height:220px;width:100%;resize:none;"><?php echo $omschrijving;?></textarea>
    </div>
    <div class="col-sm-12">
        <h2>Datum</h2>
        <input name="datum" type="text" value="<?php echo $datum;?>">
    </div>
    <div class="col-sm-12">
        <h2>Tijd</h2>
        <input name="tijd" type="text" value="<?php echo $tijd;?>">
    </div>
    <div class="col-sm-12">
        <h2>Locatie</h2>
        <input name="locatie" type="text" value="<?php echo $locatie;?>">
    </div>
    <div class="col-sm-12">
        <h2>Dresscode</h2>
        <input name="dresscode" type="text" value="<?php echo $dresscode;?>">
    </div>
    <div class="col-sm-12 text-right">
        <buttin formaction="CMS-DATA.php" type="submit" class="btn btn-success btn-send" value="Versturen" id="sent" <?php // echo $_SESSION['disabled']; ?>>
        <button formaction="UPDATE-CMS.php" >Update </button>
    </div>
</form>

Upvotes: 0

Denis Solakovic
Denis Solakovic

Reputation: 245

You have defined action on your form action="cms_data.php", so your button that is responsible for submitting that form works correctly, but on the other hand you've defined another button (anchor tag), that only has href (hence points to another page), so if you click on it, you won't pass any arguments with it.

My suggestion here is, as I mentioned in comment below your question, to use two buttons, both with submit property, but then handle clicking on them via JavaScript.

When you capture submitment, you can dinamically change action on your form, so your data will be passed to desired script.

Handling multiple buttons in a form: Process a Form Submit with Multiple Submit Buttons in Javascript

Manipulating form's action property: How to use JavaScript to change the form action

Another suggestion would be that you use prepared statements in your query, so you wouldn't be vulnerable to SQL injections (from the comments section, I see you'll only be using this locally, but this is a good practice).

Using Mysqli prepared statements: https://stackoverflow.com/a/24989090/5018750

Upvotes: 1

Tejas Gundecha
Tejas Gundecha

Reputation: 39

Echo only prints value on the screen in your respective textbox and does not assign that value to your actual field.

Instead what you can do is start the session in the start of your contact form and store those fields in session variable.

When user selects UPDATE option he will get redirected to UPDATE-CMS.php page. In UPDATE-CMS.php you can retrieve your stored session variables and assign them to your actual variables. In this way you can carry forward your old as well as new values.

Upvotes: 0

Related Questions