Form adding a second INSERT to database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kelisia
Forum Newbie
Posts: 2
Joined: Thu Mar 08, 2007 5:15 am

Form adding a second INSERT to database

Post by kelisia »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, my first question for you guys..

I have a simple form submission script that works propperly except for the fact that after it inserts the info into the database it then adds a second row into the database with only the ID field filled in ( it is an auto_increment field)

Right now the code is in it's infant form so I understand it will need cleaning etc.. the bit I am concerned with is the "INSERT" statement and why it is adding it twice..

here is the database structure

[syntax="sql"]
CREATE TABLE `gard_content` (
  `id` mediumint(9) NOT NULL auto_increment,
  `page_num` mediumint(9) NOT NULL default '1',
  `cat_id` mediumint(9) NOT NULL default '0',
  `Page_name` varchar(50) NOT NULL default 'New Page',
  `text` blob,
  `is_active` smallint(2) default '1',
  `is_home` smallint(5) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;
and the code in question[/syntax]

Code: Select all

<?php

if ($action == "conadd") {

                    
           echo "<center><font class=\"title\"><b>Add Some Content</b></font></center><br><br>
    	         <form method=\"post\" action=\"grimoire_edit.php?action=addcon\">
    	         Page Number: <input type=\"text\" name=\"page_num\" size=\"4\" maxlength=\"100\">&nbsp;&nbsp;
                 Category: <select name=\"cat_id\">";
  //get categories      
    	         $get_cats = "SELECT * FROM ".$pre."content_cat WHERE link_type = 1 ORDER BY cat_id";
                 $get_cats_res = mysql_query($get_cats)
                  or die(mysql_error());         
                             while ($row = mysql_fetch_array($get_cats_res)) {
                                              $cat_id = $row['cat_id'];
                                              $cat_title = $row['cat_name'];                    
                 echo "<option value=\"$cat_id\">$cat_title</option>";
                 }                                                           
                 echo "</select><br><br>
                 Page Title: <input type=\"text\" name=\"page_name\" size=\"50\" maxlength=\"100\"><br><br>
                 Page Content<br><textarea name=\"text\" cols=\"85\" rows=\"25\"></textarea><br><br><br>
                 Activate:
                 <input type=\"radio\" name=\"is_active\" value=\"1\" checked>Yes
                 <input type=\"radio\" name=\"is_active\" value=\"0\">No
    	         <input type=\"submit\" value=\"Add Page\"><br><br>
    	         </form>";
    	       }
  
//
//Add the page to the database
//
    	         
if ($action == "addcon") {

 //add the page

            
 //place query below
$sql="SELECT page_num FROM ".$pre."content WHERE cat_id ='".mysql_real_escape_string($_POST["cat_id"])."'";
//query database to get the page numbers
//that already exist in the current
//category store the result of the query
//in a variable named '$result' such as:
$result = mysql_query($sql);


//create an array to hold the page numbers
$page_numbers = array();

//loop through results and store page number
while ($row = mysql_fetch_assoc($result)) {
    //add the page number to $page_numbers array
    //this asssumes the column in the db is named
    //'page_num'
    $page_numbers[] = $row['page_num'];
}

//check that the form has been submitted
//you probably already do this but...
if (isset($_POST['page_num'])) {
   
    //check that the value supplied by the user is
    //a numeric, and has value
    if (strlen(trim($_POST['page_num'])) == 0 ||
        !is_numeric($_POST['page_num'])) {
        //data has failed at least on
        echo "Page number submitted by is not numeric or has no value";
        exit;
    }
   
    //check that the value supplied by user does
    //not already exist in db
    if (in_array($_POST['page_num'], $page_numbers)) {
        //now we know that the page number
        //supplied by the user already exists
        //in db and we cannot accept the input
        echo "Page number already exists in this category";
        exit;
    }
}
            
 $sql = "INSERT INTO ".$pre."content (id, page_num, cat_id, page_name, text, is_active, is_home)  VALUES ('', '".mysql_real_escape_string($_POST["page_num"])."', '".mysql_real_escape_string($_POST["cat_id"])."', '".mysql_real_escape_string($_POST["page_name"])."', '".mysql_real_escape_string($_POST["text"])."', '".mysql_real_escape_string($_POST["is_active"])."', '0')";

			if ($result = $db->sql_query($sql)) {
         echo "<center><b>The page has been added!</b><br><a href=\"grimoire_edit.php?action=home\">Content Home</a><br><a href=\"grimoire_edit.php?action=conadd\">Add another page</a></center>";      
  }else{
            echo "<center> Something went wrong </center>";
            }  
}
?>
that is a piece of a much larger file. Each insert statement in the file does the same thing.. am I missing a break somewhere? Thanks in advance for any help 8)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A page request with $action being "conadd" or "addcon" will insert unless your exit calls are encountered.

Technically, the page will default to inserting as well.
Post Reply