It's for a user to "save" a story he's read. I got a snippet of code from SO a few days ago but, I can't seem to get it to work. I want to snatch the url from the page开发者_开发知识库 a user is currently viewing, insert the url in a MySQL DB and output the url back to client under a favorite section on the website. Any ideas?
When I try the code below, I get a T String error.
Index.php
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER ["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI";
}
return $pageURL;
}
?>
<html>
<body>
<form action="storeBookmark.php?url=<?php echo curPageURL();" method="GET"> <input type="submit" value="Submit" />
</form>
</body>
</html>
<?php
$url = $_GET['url'];
$dbc = mysqli_connect('xxxx', 'xxxx', 'xxxx', 'xxxx')
or die('Error connecting to MySQL server');
$query = "INSERT INTO xxxx (url)". "VALUES('$url')";
$result = mysqli_query($dbc, $query) or die('Error.');
mysqli_close($dbc);
?>
<form action="storeBookmark.php?url=<?php echo curPageURL();?>" method="GET"> <input type="submit" value="Submit" />
Added in closing php tag for curPageURL() function call.
You should also not store unsanitized user data in your database. Since you're using mysqli you should use a prepared statement.
<?php
$link = mysqli_connect('xxxx', 'xxxx', 'xxxx', 'xxxx') or die('Error connecting to MySQL server');
$url = $_GET['url'];
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, 'INSERT INTO table1 (url) VALUES(?)')) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $url);
/* execute query */
mysqli_stmt_execute($stmt);
/* close statement */
mysqli_stmt_close($stmt);
}
精彩评论