php - 想要使用 html 表单字段将页面 url 存储到 mysql

标签 php html mysql

我有两个 php 文件。 index.phpinsert.php。我正在使用表单插入一些数据,但我还想使用 html 表单将 index.php url 存储到 mysql 中。因为我有很多像 index.php 这样的页面正在调用 insert.php。实际上,当某个页面调用 insert.php 时,它应该使用表单获取该页面的 url ,以便我可以将该 url 存储到数据库中。代码如下。

<?php

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

// Everything is working fine but not taking the url
$sql="INSERT INTO comment (id, name, email,comment,url)
VALUES
(null, '$_POST[name]','$_POST[email]','$_POST[comment]', $_POST[url])";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);

?>

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Comment Box</title>
</head>

<body>

<?php

//getURL function to get URL

function getURL()

 {

    /* First check if page is http or https */

    //$whichprotocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';

    /* Combine different pieces of $_SERVER variable to return current URL */

    //return $whichprotocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    return '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

 }

 /*Calling getURL function to display current page URL*/


?>



<form action="insert.php" method="post">

  <p>Name:
  <input name="name" type="text" />
  </p>
  <p>Email:
  <input name="email" type="text" />
  </p>
  <p>Comment:</p>
  <p>
    <textarea name="comment" cols="50" rows="10"></textarea>
  </p>
  <p>
    <--! getURL(); gives me error. It takes url but don't send it insert query -->
    <input type="hidden" name="url" id="url" value="<?php getURL();?>" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>
<br />
<?php
    echo getURL();
?>
</body>
</html>

最佳答案

您需要回显函数中的值 -

value="<?php echo getURL();?>" />

关于php - 想要使用 html 表单字段将页面 url 存储到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26939223/

相关文章:

javascript - 滚动到文档顶部,jQuery 无法正常工作

javascript - 将引导工具提示添加到动态创建的按钮

php - 如何从 MySQL 时间戳转换为 PHP Y-m-d?

php - 在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

php - 从 PHP、mysql(页面中的多个查询)导出到 excel,代码无法正常运行?

php - 即使我添加了缓存清除代码,缓存也没有清除

javascript - 如何在一个人停止输入后调用一个函数?

MySQL - 存储过程

php - 从数据库加载的表不会通过 POST 发送正确的数据

用于阅读电子邮件的 PHP 库