繁体   English   中英

通过按钮将数据从一种形式插入到选定的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">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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