AidanPT
AidanPT

Reputation: 185

Form isn't posting data into MySQL database?

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>";
}
?>   

Upvotes: 0

Views: 69

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

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."

Upvotes: 1

skrtbhtngr
skrtbhtngr

Reputation: 2251

I think this should work; Change the line

echo '<meta http-equiv="refresh" content="2;url=admin.php?page=3">';

to

echo "<script>window.location = 'admin.php?page=3';</script>";

Upvotes: 0

Related Questions