简体   繁体   中英

INSERT data from one form into a chosen MySQL table via buttons

I have an HTML form with two buttons (one adds to a main archive table and one adds to a drafts table to be edited and submitted later). I cannot figure out how to make this work with the two buttons (a button becoming the insertion into a table)

The code is now very messy and filled with other ideas/techniques but any good coder will see I have no idea what I'm at.

Here's the two buttons from the <form> :

<button type="submit" class="btn btn-primary btn-lg" name="submit" value="archive">Add to Archive</button>

<button type="submit" class="btn btn-default btn-lg" name="submit" value="drafts">Save to Drafts</button>

and here's the PHP file:

<?php

include("dbconnect.php"); //connection file

//retrieve all the data from the form

$title= ($_POST['title']);
$date= ($_POST['date']);
$series= ($_POST['series']);
$housemates= ($_POST['housemates']);
$houseLocation= ($_POST['houseLocation']);
$length= ($_POST['length']);
$airtime= ($_POST['airtime']);
$type= ($_POST['type']);
$team= ($_POST['team']);
$objective= ($_POST['objective']);
$finalOutcome= ($_POST['finalOutcome']);
$successfulness= ($_POST['successfulness']);

$submit = $_POST['submit']; //following an example from online
$action = $submit;

//send all data to database tables(s)

switch ($action){ //taken from an online example a switch statement - doesn't work
    case 'archive':
    $dbQuery="INSERT into tasks values (NULL,'$title','$date','$series','$housemates','$houseLocation','$length','$airtime','$type','$team','$objective','$finalOutcome','$successfulness')";
    $dbResult=mysql_query($dbQuery);
    break;

    case 'drafts':
    $dbQuery="INSERT into drafts values (NULL,'$title','$date','$series','$housemates','$houseLocation','$length','$airtime','$type','$team','$objective','$finalOutcome','$successfulness')";
    $dbResult=mysql_query($dbQuery);
    break;
}

mysql_close();
header("Location: index.php");
}
?>

I do have the form submitting the data to one table (the archive table).

Firstly, strongly agree with using PDO or mysqli.

As to your question, the value of a <button> doesn't get submitted, so you won't see it in $_POST['submit'] . You'll need to change it to an <input> , something like:

<input type="submit" class="btn btn-primary btn-lg" name="submit" value="Add to Archive">
<input type="submit" class="btn btn-default btn-lg" name="submit" value="Save to Drafts">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM