SQL query... enters info twice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

SQL query... enters info twice

Post by Jim »

Hey all...

I'm inserting info using PHP/MySQl and I have a problem. When I run an insert query, I get two entries in to my database.

One is all the info I wanted, and the other is totally empty. Here's the code:

Code: Select all

function select_game() {
		
		echo "<table cellpadding=0 cellspacing=0 border=0 align="center">
		
		<tr><td>";
		
		echo "<form method="post" action="index.php?f=addreview">";
		echo "<select name="game">";
		
			$sql = mysql_query("select game , id from game_index");
				while ($info = mysql_fetch_array($sql)) {
				
				$game = $info['game'];
				$id = $info['id'];
				
				echo "<option value="$id">" .$game;
				
				}
				
		echo "</select> &nbsp;&nbsp; <input type="submit" value="Select Game"";
		echo "</td></tr></table>";
		
		}
		
		
		function new_review() {
		
				
		echo "<BR><BR>";
		echo "<form method="post" action="index.php?f=infusereview">";

		
	$indexid = $_POST['game'];
	$author = $_SESSION['gp_username'];

		$sql = mysql_query("select id from reviews where indexid = '$indexid'");
                          $num = mysql_num_rows($sql);
                                if ($num != 0) { die (" A review has already been written for this game! "); }

		echo "<form method='post' action='cp_index.php?action=ireview'>";
		echo "<input type='hidden' name='indexid' value='$indexid'>";
		echo "<textarea name='introduction' rows='5' cols='25'>Introduction</textarea><BR><BR>";

		echo "<b>Gameplay Rating:</b> &nbsp;<select name='gameplay_rating'>";
		echo "<option>10";
		echo "<option>9";
		echo "<option>8";
		echo "<option>7";
		echo "<option>6";
		echo "<option>5";
		echo "<option>4";
		echo "<option>3";
		echo "<option>2";
		echo "<option>1";
		echo "<option>0";
		echo "</select><BR>";
		
		echo "<input type='text' name='gameplay_header' value='Gameplay Header'><BR>";
		echo "<textarea name='gameplay' rows='5' cols='25'>This will be Gameplay</textarea><BR><BR>";
		
		echo "<b>Graphics Rating:</b> &nbsp;<select name='graphics_rating'>";
		echo "<option>10";
		echo "<option>9";
		echo "<option>8";
		echo "<option>7";
		echo "<option>6";
		echo "<option>5";
		echo "<option>4";
		echo "<option>3";
		echo "<option>2";
		echo "<option>1";
		echo "<option>0";
		echo "</select><BR>";

		echo "<input type='text' name='graphics_header' value='Graphics Header'><BR>";
		echo "<textarea name='graphics' rows='5' cols='25'>This will be graphics</textarea><BR><BR>";

		echo "<b>Sound Rating:</b> &nbsp;<select name='sound_rating'>";
		echo "<option>10";
		echo "<option>9";
		echo "<option>8";
		echo "<option>7";
		echo "<option>6";
		echo "<option>5";
		echo "<option>4";
		echo "<option>3";
		echo "<option>2";
		echo "<option>1";
		echo "<option>0";
		echo "</select><BR>";

		echo "<input type='text' name='sound_header' value='Sound Header'><BR>";
		echo "<textarea name='sound' rows='5' cols='25'>This will be Sound</textarea><BR><BR>";

		echo "<b>Funfactor Rating:</b> &nbsp;<select name='funfactor_rating'>";
		echo "<option>10";
		echo "<option>9";
		echo "<option>8";
		echo "<option>7";
		echo "<option>6";
		echo "<option>5";
		echo "<option>4";
		echo "<option>3";
		echo "<option>2";
		echo "<option>1";
		echo "<option>0";
		echo "</select><BR>";

		echo "<input type='text' name='funfactor_header' value='funfactor Header'><BR>";
		echo "<textarea name='funfactor' rows='5' cols='25'>This will be Fun Factor</textarea><BR><BR>";

		echo "<b>Plasmafactor Rating:</b> &nbsp;<select name='plasmafactor_rating'>";
		echo "<option>10";
		echo "<option>9";
		echo "<option>8";
		echo "<option>7";
		echo "<option>6";
		echo "<option>5";
		echo "<option>4";
		echo "<option>3";
		echo "<option>2";
		echo "<option>1";
		echo "<option>0";
		echo "</select><BR><BR>";

		echo "<input type='text' name='plasmafactor_header' value='Plasmafactor Header'><BR>";
		echo "<textarea name='plasmafactor' rows='5' cols='25'>This will be Plasmafactor</textarea><BR><BR>";

		echo "<input type='text' name='conclusion_header' value='Conclusion Header'><BR>";
		echo "<textarea name='conclusion' rows='5' cols='25'> This will be the Conclusion</textarea><BR><BR>";

		echo "<input type='submit' value='Submit Review'>";
		echo "</form>";
	

		}
	
		
		function infuse_review(){
		
	$indexid = $_POST['indexid'];			
	$author = $_SESSION['gp_username'];
	
	$introduction = $_POST['introduction'];
	$conclusion = $_POST['conclusion_header'];
	$conclusion_body = $_POST['conclusion'];

	$gameplay_rating = $_POST['gameplay_rating'];
	$gameplay= $_POST['gameplay_header'];
	$gameplay_body = $_POST['gameplay'];

	$sound_rating = $_POST['sound_rating'];
	$sound = $_POST['sound_header'];
	$sound_body = $_POST['sound'];

	$funfactor_rating = $_POST['funfactor_rating'];
	$funfactor = $_POST['funfactor_header'];
	$funfactor_body = $_POST['funfactor'];

	$graphics_rating = $_POST['graphics_rating'];
	$graphics = $_POST['graphics_header'];
	$graphics_body = $_POST['graphics'];

	$plasmafactor_rating = $_POST['plasmafactor_rating'];
	$plasmafactor = $_POST['plasmafactor_header'];
	$plasmafactor_body = $_POST['plasmafactor'];

	$fscore_add = $gameplay_rating + $sound_rating + $funfactor_rating + $graphics_rating + $plasmafactor_rating;
	$finalscore_rating = $fscore_add / 5;

	$sql = "insert into reviews ( finalscore , author , introduction , conclusion , conclusion_body , gameplay_rating , gameplay_body , gameplay , sound_rating , sound_body , sound , funfactor_rating , funfactor_body , funfactor , graphics_rating , graphics_body , graphics , plasmafactor_rating , plasmafactor_body , plasmafactor , indexid ) 
		values ( '$finalscore_rating' , '$author' , '$introduction' , '$conclusion' , '$conclusion_body' , '$gameplay_rating' , '$gameplay_body' , '$gameplay' ,  '$sound_rating' , '$sound_body' , '$sound' , '$funfactor_rating' , '$funfactor_body' , '$funfactor' , '$graphics_rating' , '$graphics_body' , '$graphics' , '$plasmafactor_rating' , '$plasmafactor_body' , '$plasmafactor'  , '$indexid')";
			
	$act = mysql_query($sql);
	
	echo "<b>" .$sql. "</b>";	
		}
I can't find anything, so I hope you can.

Note: When I have the SQL print at the end of the script, it only shows that the script was run once, not twice. Weird huh?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I don't know if you did this on purpose, but inside the "new_review" function you did not include a value property inside the <option> tags.

You have:

echo "<option>10";
echo "<option>9";
echo "<option>8";
etc..

You probably should have:

echo "<option value=10>10";
echo "<option value=9>9";
echo "<option value=8>8";
etc..

Also, in an article called "Developing Rock Solid PHP Code" it recommends you always use the single quotes when you do not need the double because it makes your code run faster, not that is that important to have this code run like a half a second faster ;)

I have no idea why it would enter the give you two entries. Sorry. But I hope I have been of some help.
[/quote]
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

The single/double quote debate has been an argument around these forums for a while.

I find it's easier to edit my HTML when I use the double quotes. :)

I'm looking around... and I still don't see why there are two entries.
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

I've spent a while looking for the code mistake, but I'm probably blind ;) . Maybe there something in the code which you did not put here.

BTW: Did you try to check if the query is ok :?: It surely is, if it insert a record, but maybe there is some problem about it, try to check if the $act is a valid mysql result.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Already have that set. I get no error. Also, when echoing the SQL, it prints out just fine.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

I think the problem may be with the index.php file that calls the function to insert the information.

Code: Select all

<?php $admin = "1"; ?>
<html>
<head>
<title>gameplasma.com :: Control Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="/include/default.css" rel="stylesheet" type="text/css">
</head>

<body bgcolor="#CCCCCC" text="#000000" link="#D58000" vlink="#D58000" alink="#FFCC00" leftmargin="0" topmargin="0">

<table width="760" height="0" align="center" cellpadding="0" cellspacing="0" class="mtable">
  <tr background="/images/slice_7.jpg"> 
  <td>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
		<tr>

      	<tr>
      	<td width="51%" height="73"><img src="/images/top_logo.jpg" width="388" height="73"><img src="/images/top_right.jpg" width="372" height="73"></td>
	  	</tr>
		</table>
		
	</td>
	</tr>
	</table>
	
	
	<table cellpadding=0 cellspacing=0 border=0 align="center" width="761">
	<tr>
	<td>
	
	<table width="123" border="0" cellpadding="0" cellspacing="0">
          <tr> 
            <td width="124" height="33" valign="top" background="/images/slice_16.jpg"> 
              <table width="124" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr> 
                  <td><img src="/images/slice_21.jpg" width="123" height="33"></td>
                </tr>
                <tr> 
                  <td class="menu">
				  <b>News Admin</b><br>
				  <a href="/cp/index.php?f=addnews">Add News</a><br>
				  <a href="/cp/index.php?f=selectnews">Edit News</a><br><br>
				  
				  <b>Reviews Admin</b><br>
				  <a href="/cp/index.php?f=selectreview">Add Review</a><br>
				  <a href="/cp/index.php?f=editreview">Edit Review</a><br><br>
				  
				  <b>Previews Admin</b><br>
				  <a href="/cp/index.php?f=addpreview">Add Preview</a><br>
				  <a href="/cp/index.php?f=editpreview">Edit Preview</a><br><BR>
				  
				  <b>Screens Admin</b><br>
				  <a href="/cp/index.php?f=addss">Add Screenshots</a><br>
				  <a href="/cp/index.php?f=selectss">Edit Screenshots</a><br><br>
				  
				  <?php if ($admin == '1') { ?>
				  
				  <b>Game Index</b><br>
				  <a href="/cp/index.php?f=addgame">Add New Game</a><br>
				  <a href="/cp/index.php?f=selectgame">Edit Game</a><br>
				  
				  <?php } ?>
                  </td>
                </tr>
				</table>
	
	
	
	
	
	</td>
	
	
	<td>
	
	<table cellpadding=0 cellspacing=0 border=0 width="638" valign="top" bgcolor="#F7F7F7" height="100%">
		<tr>
			<td>
			
			<table cellpadding=6 cellspacing=0 border=0 align="center" width="450" background="#D6D6D6" style="border:dashed width:thin" bordercolor="#D6D6D6" class="body">
			<tr>
			<td>
			
			<table cellpadding=0 cellspacing=0 border=0 class="body" width="100%">
			<tr>
			<td><b> Welcome <?=$_SESSION['gp_username']?></td>
			<td>
			
			<div align="right">
			<select name="cpsite">
			<option value="http://www.rongalaxy.com/panel">Rise of Nations
			<option> Other sites
			<option selected> - No Site -
			</select>
			</div>
			
			</td>
			</tr>
			</table>
			
			<table cellpadding=0 cellspacing=0 border=0 class="body" width="100%">
			<tr>
			<td>
			<?php
			
			require "class/news.php";
			require "class/preview.php";
			require "class/screenshot.php";
			require "class/review.php";
			require "class/gameindex.php";
			
			
			
				$f = $_GET['f'];
				
					switch ($f) {
					
					// Edit News
					
					/*
					case 'addnews':
					$news = new News;
					$news -> add_news();
					break;
					
					case 'infusenews':
					$news = new News;
					$news -> infuse_news();
					break;
					
					case 'selectnews':
					$news = new News;
					$news -> pull_news();
					break;
					
					case 'editnews':
					$news = new News;
					$news -> edit_news();
					break;
					
					case 'reinfusenews':
					$news = new News;
					$news -> reinfuse_news();
					break;
					
					case 'deletenews':
					$news = new News;
					$news -> delete_news();
					break; 
					
					*/
					
					// Edit Game Index
					
					case 'addgame':
					$index = new Gameindex;
					$index -> new_game();
					break;
					
					case 'dogame':
					$index = new Gameindex;
					$index -> infuse_game();
					break;
					
					case 'selectgame':
					$index = new Gameindex;
					$index -> pull_game();
					break;
					
					case 'editgame':
					$index = new Gameindex;
					$index -> edit_game();
					break;
					
					case 'reinfusegame':
					$index = new Gameindex;
					$index -> reinfuse_game();
					break;
					
					case 'deletegame':
					$index = new Gameindex;
					$index -> delete_game();
					break;
					
					
					
					/*// Edit Articles
					
					// Edit Previews
					case 'addpreview':
					$prev = new Preview;
					$prev -> add_preview();
					break;
					
					case 'infusepreview':
					$prev = new Preview;
					$prev -> infuse_preview();
					break;
					
					case 'editpreview':
					$prev = new Preview;
					$prev -> pull_preview();
					break;
					
					case 'selectpreview':
					$prev = new Preview;
					$prev -> edit_preview();
					break;
					
					case 'reinfusepreview':
					$prev = new Preview;
					$prev -> reinfuse_article();
					break;
					
					// Edit Reviews
					
					case 'selectreview':
					$rev = new Review;
					$rev -> select_game();
					break;
					
					case 'addreview':
					$rev = new Review;
					$rev -> new_review();
					break;
					
					case 'infusereview':
					$rev = new Review;
					$rev -> do_review();
					break;
					
					case 'editreview':
					$rev = new Review;
					$rev -> pull_review();
					break;
					
					// Edit Users
					
					// Edit Screenshots
					
					case 'addss':
					$screen = new Screenshots;
					$screen -> new_screenshots();
					break;
					
					case 'infusess':
					$screen = new Screenshots;
					$screen -> infuse_screenshots();
					break; 
					*/
					
					//DEFAULT
					
					DEFAULT:
					
						$connect = mysql_connect ( "localhost" , "aaaaa" , "aaaaa" );
						$select = mysql_select_db ( "aaaaa" , $connect );
							if (!$select) { echo mysql_error(); }
						
							$sql = mysql_query("select * , UNIX_TIMESTAMP(date) as datestamp from gameplasma order by id limit 5 ");
							if (!$sql) { echo mysql_error(); }
								while ($info = mysql_fetch_array($sql)) {
								
								$headline = $info['headline'];
								$author = $info['author'];
								$datestamp = $info['datestamp'];
								$date = date( "F d Y" , $datestamp);
								$body = $info['body'];
								
								echo "<BR>";
								echo "<b>" .$headline. "</b><BR>";
								echo "<font size="-2"><i>by \n" .$author. "\n on \n" .$date. "</i></font><BR><BR>";
								echo $body;
								
								}
					
					}					
			?>
									
			</td>
			</tr>
			</table>
			
			
			
			
			
			
			
			
			
			</td>
			</tr>
			</table>
			
			</td>
		</tr>
	</table>
	
	</td>
	</tr>
	</table>
Post Reply