通过按钮将数据从一个表单插入到选定的MySQL表中


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

我有一个带有两个按钮的HTML表单(一个添加到主存档表,另一个添加到草稿表以供以后编辑和提交)。我无法弄清楚如何使用两个按钮(一个按钮插入到表格中)来使其工作

代码现在非常混乱,充满了其他想法/技术,但任何好的编码人员都会看到我不知道我在做什么。

这是<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>

这是 PHP 文件:

<?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");
}
?>

我确实有将数据提交到一个表(存档表)的表单。

首先,强烈同意使用 PDO 或 mysqli。

至于你的问题,<button>的价值没有提交,所以你不会在$_POST['submit']中看到它。 您需要将其更改为 <input> ,如下所示:

<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">