我有两个 php
文件。 index.php
和 insert.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/