Bradley99 Posted January 26, 2011 Share Posted January 26, 2011 I'm trying to make a newspaper page - I have a simple code but when i hit submit nothing happens, just page refreshes. Help would be great! Code: <?php session_start(); include "includes/db_connect.php"; include "includes/functions.php"; logincheck(); $username=$_SESSION['username']; $query=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1"); $fetch=mysql_fetch_object($query); $above = mysql_query("SELECT * FROM users WHERE username='$username'"); $info = mysql_fetch_object($above); $admin=$info->editor ; if($admin !=1){ echo"<center> This information is limited to Editors only!"; }else{ if ($_POST['Submit']){ $edition = $_POST['edition']; $news = $_POST['news']; $title = $_POST['title']; $by = $_POST['by']; $date = gmdate('Y-m-d h:i:s'); mysql_query("INSERT INTO `paper` ( `edition` , `news` , `title` , `by` , `date` ) VALUES ( '', '$edition', '$news', '$title', '$by', '$date' )"); echo "Article has been added"; } ?> <form name="form1" method="post" action=""> <table width="450x" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" class="Main"> <tr class="Tableheading"> <td class="TableHeading">Add an Article</td></tr> <tr> <td class="TableArea" align="center">Edition: <input name="edition" type="text" id="edition" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Article: <input name="news" type="text" id="news" value="" cols="80" rows="5"> </td></tr> <tr> <td class="TableArea" align="center">Title: <input name="title" type="text" id="title" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Author: <input name="by" type="text" id="by" value="" size="30"> </td></tr> <tr><td class="TableArea" align="center">Add this article: <input name="submit" type="submit" class=button id="submit" value="Add Article"><br> </td></tr> </table> </form> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/ Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 is this ever true? echo something after it and find out. if ($_POST['Submit']){ Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165724 Share on other sites More sharing options...
tomtimms Posted January 26, 2011 Share Posted January 26, 2011 I also recommend doing. $query ="INSERT INTO `paper` ( `edition` , `news` , `title` , `by` , `date` ) VALUES ( '', '$edition', '$news', '$title', '$by', '$date' )"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165727 Share on other sites More sharing options...
Bradley99 Posted January 26, 2011 Author Share Posted January 26, 2011 It has that at the top of POST tags and echo at the bottom of them. Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165728 Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 this is never true: if ($_POST['Submit']){ i recommend checking for the post directly instead: if ($_SERVER['REQUEST_METHOD'] == "POST") { // the form was posted Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165729 Share on other sites More sharing options...
Bradley99 Posted January 26, 2011 Author Share Posted January 26, 2011 Added both of them and got nothing back? Been at this for a while now!! Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165731 Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 post updated code? also, make sure the query runs. $result = mysql_query($query) or die(mysql_error() . " IN $query"); Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165733 Share on other sites More sharing options...
dragon_sa Posted January 26, 2011 Share Posted January 26, 2011 part of the problem is you are checking for Submit when you named your button submit, secondly in you insert query you have declared the values you want to insert, then the first thing in after VALUES id '', you dont need that, I am assuming you did that because of an auto increment id in your table, that will happen automatically as you already declared what values to insert, effectively you are saying that the value for edition is '', the value for news is edition and so on. Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165734 Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 yeah, the mis-match column count would be pointed out by mysql_error() Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165735 Share on other sites More sharing options...
blew Posted January 27, 2011 Share Posted January 27, 2011 dont you forget to edit in the form (action = "").. you shall put it something like (action = "<?php echo $_SERVER['PHP_SELF']; ?>" if you dont expecify the url in the action, you PHP code never gonna work Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165747 Share on other sites More sharing options...
dragon_sa Posted January 27, 2011 Share Posted January 27, 2011 actually if you are processing on the same page the action should be action='', you do not need to specify the link like that Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165761 Share on other sites More sharing options...
blew Posted January 27, 2011 Share Posted January 27, 2011 actually if you are processing on the same page the action should be action='', you do not need to specify the link like that well, i did not know hehe my bad.. so thats not the error lol Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165765 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 Ok, Updated code below. What i don't get is that its basically the same script as the Updates page i have and that works fine. <?php session_start(); include "includes/db_connect.php"; include "includes/functions.php"; logincheck(); $username=$_SESSION['username']; $query=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1"); $fetch=mysql_fetch_object($query); $above = mysql_query("SELECT * FROM users WHERE username='$username'"); $info = mysql_fetch_object($above); $admin=$info->editor ; if($admin !=1){ echo"<center> This information is limited to Editors only!"; }else{ if ($_POST['Submit']){ $edition = $_POST['edition']; $news = $_POST['news']; $title = $_POST['title']; $by = $_POST['by']; $date = gmdate('Y-m-d h:i:s'); mysql_query("INSERT INTO `paper` ( `edition` , `news` , `title` , `by` , `date` ) VALUES ( '$edition', '$news', '$title', '$by', '$date' )"); echo "Article has been added"; } if ($_SERVER['REQUEST_METHOD'] == "POST") { // the form was posted } ?> <form name="form1" method="post" action=""> <table width="450x" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" class="Main"> <tr class="Tableheading"> <td class="TableHeading">Add an Article</td></tr> <tr> <td class="TableArea" align="center">Edition: <input name="edition" type="text" id="edition" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Article: <input name="news" type="text" id="news" value="" cols="80" rows="5"> </td></tr> <tr> <td class="TableArea" align="center">Title: <input name="title" type="text" id="title" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Author: <input name="by" type="text" id="by" value="" size="30"> </td></tr> <tr><td class="TableArea" align="center">Add this article: <input name="submit" type="Submit" class=button id="Submit" value="Add Article"><br> </td></tr> </table> </form> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165771 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 With the script below i get the 'Your article has been added' But nothing is entered into database. . . <?php session_start(); include "includes/db_connect.php"; include "includes/functions.php"; logincheck(); $username=$_SESSION['username']; $query=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1"); $fetch=mysql_fetch_object($query); $admin=$fetch->editor ; if($admin !=1){ echo"<center> This information is limited to Editors only!"; }else{ if ($_POST['Submit']){ $edition = $_POST['edition']; $news = $_POST['news']; $title = $_POST['title']; $by = $_POST['by']; $date = gmdate('Y-m-d h:i:s'); $query ="INSERT INTO `paper` ( `edition` , `news` , `title` , `by` , `date` ) VALUES ( '$edition', '$news', '$title', '$by', '$date' )"; echo "Article has been added"; } ?> <form name="form1" method="post" action=""> <table width="450x" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" class="Main"> <tr class="Tableheading"> <td class="TableHeading">Add an Article</td></tr> <tr> <td class="TableArea" align="center">Edition: <input name="edition" type="text" id="edition" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Article: <input name="news" type="text" id="news" value="" cols="80" rows="5"> </td></tr> <tr> <td class="TableArea" align="center">Title: <input name="title" type="text" id="title" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Author: <input name="by" type="text" id="by" value="" size="30"> </td></tr> <tr><td class="TableArea" align="center">Add this article: <input name="Submit" type="Submit" class=button id="Submit" value="Add Article"><br> </td></tr> </table> </form> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165778 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 Also it's fetching info from the paper table fine? Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165781 Share on other sites More sharing options...
dragon_sa Posted January 27, 2011 Share Posted January 27, 2011 try the insert like this $query ="INSERT INTO paper (edition, news, title, by, date) VALUES ('$edition', '$news', '$title', '$by', '$date')"; Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165788 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 try the insert like this $query ="INSERT INTO paper (edition, news, title, by, date) VALUES ('$edition', '$news', '$title', '$by', '$date')"; Nope still get 'Article has been added' but nothing sent to database!!! Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165791 Share on other sites More sharing options...
dragon_sa Posted January 27, 2011 Share Posted January 27, 2011 your missing this line after the insert query $result=mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165796 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 $query ="INSERT INTO paper (edition, news, title, by, date) VALUES ('$edition', '$news', '$title', '$by', '$date')"; $result=mysql_query($query); echo "Article has been added"; } ? Still doesn't post anything into the database Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165798 Share on other sites More sharing options...
dragon_sa Posted January 27, 2011 Share Posted January 27, 2011 are the columns in your database exactly the same as you have listed here, meaning they are all lower case as you have them in this query also add this onto the end of the insert $query ="INSERT INTO paper (edition, news, title, by, date) VALUES ('$edition', '$news', '$title', '$by', '$date')" or die(mysql_error()); and see if that gives any error message Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165803 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 Yes all lower case: id edition news title by date align Added that and just got 'Article added' again, but again nothing goes into the DB!! Uploaded with ImageShack.us Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165804 Share on other sites More sharing options...
dragon_sa Posted January 27, 2011 Share Posted January 27, 2011 can you post your full page again please what you have now? Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165811 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 Sure....the bottom half is the fetching test to make sure i can fetch from the table. <?php session_start(); include "includes/db_connect.php"; include "includes/functions.php"; logincheck(); $username=$_SESSION['username']; $query=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1"); $fetch=mysql_fetch_object($query); $admin=$fetch->editor ; if($admin !=1){ echo"<center> This information is limited to Editors only!"; }else{ if ($admin== "1"){ if (strip_tags($_POST['Submit'])){ $edition=strip_tags($_POST['edition']); $news=strip_tags($_POST['news']); $title=strip_tags($_POST['title']); $by=strip_tags($_POST['by']); $date = gmdate('Y-m-d h:i:s'); $query ="INSERT INTO paper (edition, news, title, by, date) VALUES ('$edition', '$news', '$title', '$by', '$date')" or die(mysql_error()); $result=mysql_query($query); echo "Article has been added"; } ?> <form name="form1" method="post" action=""> <table width="450x" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" class="Main"> <tr class="Tableheading"> <td class="TableHeading">Add an Article</td></tr> <tr> <td class="TableArea" align="center">Edition: <input name="edition" type="text" id="edition" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Article: <input name="news" type="text" id="news" value="" cols="80" rows="5"> </td></tr> <tr> <td class="TableArea" align="center">Title: <input name="title" type="text" id="title" value="" size="30"> </td></tr> <tr> <td class="TableArea" align="center">Author: <input name="by" type="text" id="by" value="" size="30"> </td></tr> <tr><td class="TableArea" align="center">Add this article: <input name="Submit" type="Submit" class=button id="Submit" value="Add Article"><br> </td></tr> </table> </form> <? } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head> <title>One-Mafia Newspaper</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="includes/in.css" rel="stylesheet" type="text/css"> <script language="javascript" type="text/javascript"> <!-- function popitup(url) { newwindow=window.open(url,'name','height=1000,width=1000'); if (window.focus) {newwindow.focus()} return false; } // --> </script> </head> <body> <td > <table width="60%" style='table-layout:fixed' align="center" cellpadding="2" cellspacing="0" colspan="4" class="Main"><br> <col width=5> <col width=15> <col width=15> <col width=25> <tr> <td class="tableheading" colspan="4">Recent Updates</td> </tr> <tr> <td class="subtableheader" width="25%">Id</td> <td class="subtableheader" width="25%">User</td> <td class="subtableheader" width="25%">Posted</td> <td class="subtableheader" width="25%">Updates</td> </tr> <? $select_paper=mysql_query("SELECT * FROM paper ORDER BY id DESC"); while($the=mysql_fetch_object($select_paper)){ ?> <tr> <td class="profilerow" width="25%"><?php echo "$the->id"; ?></td> <td class="profilerow" width="25%"><?php echo "$the->title"; ?></td> <td class="profilerow" width="25%"><?php echo "$the->by"; ?></td> <td class="profilerow" width="25%"><?php echo "$the->date"; ?></td> </tr> <? }} ?> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165814 Share on other sites More sharing options...
dragon_sa Posted January 27, 2011 Share Posted January 27, 2011 can you try this please and see if the values are echoing if ($_SERVER['REQUEST_METHOD'] == "POST") { $edition=strip_tags($_POST['edition']); $news=strip_tags($_POST['news']); $title=strip_tags($_POST['title']); $by=strip_tags($_POST['by']); $date = gmdate('Y-m-d h:i:s'); echo "edition=$edition - news=$news - title=$title - by=$by"; $query =("INSERT INTO paper (edition, news, title, by, date) VALUES ('$edition', '$news', '$title', '$by', '$date')"); $result=mysql_query($query); if (!$result) { die(mysql_error()); } else { echo "Article has been added"; } } Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165820 Share on other sites More sharing options...
Bradley99 Posted January 27, 2011 Author Share Posted January 27, 2011 Thanks, i got this back edition=1 - news=This is my article - title=Article - by=BradleyYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by, date) VALUES ('1', 'This is my article', 'Article', 'Bradley', '2011-01-27 0' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/225798-simple-post-help/#findComment-1165822 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.