Jump to content

Simple POST Help


Bradley99

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.