0

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>";
}
?>   
8
  • Isn't that how it's meant to be? Commented Mar 19, 2014 at 15:08
  • Just took that out, now it's staying on the same page but saying "update didn't work" Commented Mar 19, 2014 at 15:10
  • 2
    Replace die ("update didn't work"); with die (mysql_error()); to know the exact error why your query ain't working. Commented Mar 19, 2014 at 15:13
  • Um... what is $user_data? I think you want to change both of those to $_POST --- As it stands, it's a stray variable. @AidanPT which is most likely the reason why your code is not working and you might need to add a few hidden fields for them instead. Commented Mar 19, 2014 at 15:17
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'news_link`) VALUES ('trhrth', '', '', now(), '', 'rthrthrt', '0', 'trhrth')' at line 1 Commented Mar 19, 2014 at 15:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

It's amazing how much of a problem the little details can cause. Thanks very much!
You're welcome. Yeah, even a missing dot, comma etc. can ruin someone's day. Glad to have been able to help and found a solution for you, cheers @AidanPT amazing how much power a simple character has, eh?
:) Nah not needed mate @Fred-ii- +1 for solving the issue
It's always best and useful information @ShankarDamodaran and thank you :)
Shankar's a star, yet he's so "modest" ;-)
0

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

4 Comments

It's now staying on the same page but saying "update did not work" think there might be something wrong with the INSERT ?
As said by @Shankar, replace die ("update didn't work"); with die (mysql_error());.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'news_link`) VALUES ('trhrth', '', '', now(), '', 'rthrthrt', '0', 'trhrth')' at line 1
Replace `button with `button`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.