1

Created a form that successfully inserts a record from a form into a SQLite database. Trying to also add a delete button but i am unsure on how to execute the query properly. Here is my code:

index.php

<input id="submit" type="submit" name="input" value="Input">
<input id="submit" type="submit" name="delete" value="Delete">

post.php

if (!empty($_POST['input'])) {
  header("location:index.php");
  $stmt = $conn->prepare("INSERT INTO stock (name, gender, age) VALUES   (:name, :gender, :age)");
  $stmt->execute(array(':name' => $_POST['name'],
                                       ':gender' => $_POST['gender'],
                                       ':age' => $_POST['age']));
}

elseif (!empty($_POST['delete'])) {
  header("location:index.php");
  $stmt = $conn->prepare("DELETE FROM stock WHERE name = ':name' AND gender = ':gender' AND age = 'age'");

}
16
  • 1
    Start by removing the quotes around your binds. Commented Feb 7, 2015 at 17:28
  • 1
    Same way you did in your insert. Commented Feb 7, 2015 at 17:39
  • 1
    Plus, use isset() rather than !empty() and add exit; after each header. Commented Feb 7, 2015 at 17:42
  • 1
    You're welcome. When a certain button/input is "set", it will execute what's been called ;-) Commented Feb 7, 2015 at 17:43
  • 1
    Place/move your headers after each query and add exit;. Once one of them is successfully executed, it will then redirect to the specified URL and stop any further execution of the rest of the script. Commented Feb 7, 2015 at 17:47

1 Answer 1

1

Use isset() rather than !empty() When a certain button/input is "set", it will execute what's been called and add exit; after each header.

Using "exit;" will avoid further code execution.

Since you're getting a redirection loop, use echo "Success"; exit; instead, or use another file to redirect to.

You should remove the quotes around your binds also:

WHERE name = :name AND gender = :gender
Sign up to request clarification or add additional context in comments.

Comments

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.