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

时间:2015-01-25 21:56:28

标签: php

我有一个带有两个按钮的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");
}
?>

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

1 个答案:

答案 0 :(得分:3)

首先,非常同意使用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">