2015-01-03 102 views
1

我的mysql插入語句會在我手動插入時通過,但是表單不會發布。任何幫助表示讚賞。php mysql插入表單發佈問題

表單

<form method="post" action=""> 
    <h4>Title</h4> 
    <input type="text" id="post_title" /> 
    <h4>Location</h4> 
    <input type="text" id="post_location" /> 
    <h4>My Tale</h4> 
    <textarea id="post_content" ></textarea> 
    <input type="submit" value="Share"/><br /> 
</form> 

表單處理器

<pre> 
if (isset($_GET['success']) === true) { 
    echo 'Your journal entry has been added'; 
} else { 
    if (empty($_POST) === false && empty($errors) === true) { 
     $post_data = array(
     'post_title' => $_POST['post_title'], 
     'post_location' => $_POST['post_location'], 
     'post_content' => $_POST['post_content'] 
    ); 
    add_post($user_id, $post_data); 
    header('Location: settings.php?success'); 
    exit(); 
} else if (empty($errors) === false) { 
    echo output_errors($errors); 
} 
?> 
</pre> 

,處理後處理

<pre> 
function add_post($blog_author, $post_data) { 
    $post = array(); 
    array_walk($post_data, 'array_sanitize'); 
    foreach($post_data as $field=>$data) { 
     $post[] = '`' . $field . '` = \'' . $data . '\''; 
    } 
    mysql_query("INSERT INTO user_blog (`post_author`,`post_title`,`post_location`,`post_content`) VALUES ('$blog_author','post_title','post_location','post_content')"); 
} 
</pre> 

有沒有錯誤,只是檢查空白表單域的作用。帖子和功能之間似乎存在一個問題,但我不能確定這個問題。

+1

**危險**:您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)並應使用[現代替換](http:// php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin

回答

0

add_post()$post準備但在最後的呼叫沒有用於mysql_query

您準備$post用於INSERT INTO user_blog SET聲明中,但您正在使用INSERT INTO user_blog() VALUES()進行最終調用。

其中值只有`$ blog_author將被插入,其餘是字符串文字。

0

在textarea的沒有名字的屬性因此不會獲取內容