duanmao1975 2014-06-23 13:40
浏览 24

用户单击链接时插入MySQL数据库

I am creating a website that has users log in and select a pdf document that they want to download. When they open up the document to view and possibly download, I want data to be logged into a database at the same time.

The code to send the data to the database works (Except for: Undefined index: learningMaterial). But when I want to have the pdf document open and at the same time log the user and other data, all that happens is the document opens up.

Any advice would be appreciated, even for overall better methods of going about what I'm trying to achieve here. Still inexperienced with PHP.

See code below.

HTML

<form name="myform" method='post' action="../includes/writeStats.php">  

<input type='hidden' name='learningMaterial' id='learningMaterial' value='learningMaterial'>

<a href='../documents/test.pdf' id='mylink' class='courses' name='Driver Training'> Driver Training </a>

</form>

JS - In header

<script type="text/javascript">

function submitform(){
document.myform.submit(); }


var form = document.getElementById("myform");

document.getElementById("mylink").addEventListener("click", function () {
      submitform();
});

</script>

PHP

<?php
$con=mysqli_connect("localhost","root","password","qmptest");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//Get latest log nr
$result = mysqli_query($con,"SELECT * FROM logbook ORDER BY log DESC LIMIT 1");

while($row = mysqli_fetch_array($result)) {
$log =  $row['log'] + 1;
//If statement to check if log is 0(first entry) to go here
}


$date = date("Y/m/d");

session_start(); // Start a new session

$person = $_SESSION['currentUser'];

//Not sure if this is correct along with my HTML input
$material = mysqli_real_escape_string($con, $_POST['learningMaterial']);


//Insert into database 

$sql="INSERT INTO logbook (log, date, person, learningMaterial)
VALUES ('$log', '$date', '$person', '$material')";

if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}


mysqli_close($con);
?>
  • 写回答

2条回答 默认 最新

  • duanjiani6826 2014-06-23 13:44
    关注

    Your way, clicking the link will override the form being submitted. This leads to the file opening and the form never going through.

    Instead, you could try either opening the file in a new window by adding target="_blank" to the tag, or send the files URL through to the PHP, executing the database code then adding to the end:

    header("Location: http://yourdomain.com/yourfile.pdf");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离