doufen3786 2014-03-19 15:05
浏览 62
已采纳

表单是不是将数据发布到MySQL数据库中?

Trying to get this form to add new data into my database yet for some reason it doesn't seem to work? Instead of staying on the same page "admin.php?page=3" it redirects to just "admin.php" and no echos or signs that it is even doing anything? Could someone help me out here? Cheers!

Form:

<form>
<form name="postnewstory" action="admin.php?page=3" method="POST">
<strong>News Title: </strong><input type="text" name="news_title"><br><br />
<strong>News Story:</strong><br>
<textarea name="news_body" rows="4" cols="60"></textarea><br><br />

<input type="file" name="news_photo"><br>
<strong>Story Link: </strong><br />
<input name="button" type="radio" value="0" checked="checked">No Link<br>
<input type="radio" name="button" value="1">Link<br><br />
<strong>Link Address: </strong><input type="text" name="news_link"><br>
<strong>News Story Tags: </strong><input type="text" name="news_tags">    
<input type="submit" value="Post" name="postnewstory" class="btn btn-success"><br />
</form>

PHP: I know its very basic, just trying to get it to work before I add error checks or anything.

<?php
if (isset($_POST['postnewstory'])){
    $username = $user_data['username'];
    $email_address = $user_data['email_address'];
    $news_title = $_POST['news_title'];
    $news_photo = $_POST['news_photo'];
    $button = $_POST['button'];
    $news_link = $_POST['news_link'];
    $news_tags = $_POST['news_tags'];

$exists = mysql_query ("SELECT * FROM users WHERE username='$username'") or die ("not found");
    if (mysql_num_rows($exists) != 0){
        //update the info in database
        mysql_query ("INSERT INTO news (`news_id` ,`news_title` ,`news_body` ,`news_photo` ,`news_date` ,`username` ,`news_tags` ,`button ,`news_link`)
        VALUES (NULL , '$news_title', '$news_body', '$news_photo', now(), '$username', '$news_tags', '$button', '$news_link');") or die ("update didn't work");

        echo "<div class='alert alert-success'><strong>This post was sent!</strong></span></div> ";
        echo '<meta http-equiv="refresh" content="2;url=admin.php?page=3">';
} else echo "<strong><font color=red>Update did not work, please try again.</font></strong>";
}
?>   
  • 写回答

2条回答 默认 最新

  • douwen3362 2014-03-19 15:22
    关注

    You have a missing backtick in:

    ,`button 
    

    change to:

    ,`button`
    

    Rewrite:

    ("INSERT INTO news (`news_id` ,`news_title` ,`news_body` ,`news_photo` ,`news_date` ,`username` ,`news_tags` ,`button`,`news_link`)
    

    Footnotes:

    I quote Shankar:

    "Replace die ("update didn't work"); with die (mysql_error()); to know the exact error why your query ain't working."

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?