Insert data from custom PHP function into MySQL database
I'm having problems inserting a particular line of code into my MySQL database. It inserts three rows just fine, but the "html_href" row isn't going in for whatever reason. Here is my code:
function html_path() {
$title = strtolower($_POST['title']); // convert title to lower case
$filename = str_replace(" ", "-", $title); // replace spaces with dashes
$html_href = $fil开发者_运维知识库ename . ".html"; // add the extension
}
And my MySQL query code:
$query = "INSERT INTO work (title, logline, html_href, synopsis) VALUES";
$query .= "('".mysql_real_escape_string($_POST['title'])."',";
$query .= "'".mysql_real_escape_string($_POST['logline'])."',";
$query .= "'".html_path()."',";
$query .= "'".mysql_real_escape_string($_POST['synopsis'])."')";
$result = mysql_query($query);
The title, logline, and synopsis values go in just fine, but the html_href()
function inserts a blank row.
It looks like your html_path()
function isn't returning anything.
Try:
function html_path() {
$title = strtolower($_POST['title']); // convert title to lower case
$filename = str_replace(" ", "-", $title); // replace spaces with dashes
$html_href = $filename . ".html"; // add the extension
return $html_href;
}
Your html_path() does not return the $html_href variable. Add
return $html_href;
before you close it, and it should work perfectly.
精彩评论