简体   繁体   English

想要使用HTML表单字段将页面URL存储到mysql

[英]Want to store page url to mysql using html form field

I have two php files. 我有两个php文件。 index.php and insert.php . index.phpinsert.php I am inserting some data by using form but I also want to store the index.php url into mysql by using html form. 我通过使用表单插入一些数据,但我也想通过使用HTML表单将index.php url存储到mysql Because I have many pages like index.php which are calling insert.php . 因为我有许多诸如index.php页面都在调用insert.php Actually when some page call insert.php it should take the url of that page by using form so that i can store that url into database. 实际上,当某些页面调用insert.php它应该使用表单来获取该页面的url ,以便我可以将该url存储到数据库中。 Code is given below. 代码如下。

<?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 的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();?>" />

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

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