Jump to content

[SOLVED] query happening twice


Stooney

Recommended Posts

The following function is running the query twice.  I've have double checked and the function is only called once.  One query inputs all the information into the database and uploads the image, then I find another query with all the fields empty aside from the 'dateadded' field.  Makes no sense to me, here's the function.

 

<?php
function add_item(){
$name=mysql_real_escape_string($_POST['name']);
$desc=mysql_real_escape_string($_POST['desc']);
$barcode=mysql_real_escape_string($_POST['barcode']);
$quantity=mysql_real_escape_string($_POST['quantity']);
$received=mysql_real_escape_string($_POST['received']);

$uploaddir="items/img/";
$_FILES['image']['name']=ereg_replace(" ", "_", $_FILES['image']['name']);
$uploadfile=$uploaddir.basename($_FILES['image']['name']);
if(file_exists($uploadfile)){
	$tmpvar=1;
	while(file_exists(basename($uploaddir.$tmpvar.'-'.$_FILES['image']['name']))){
		$tmpvar++; 
	}
	$uploadfile=$uploaddir.$tmpvar.'-'.$_FILES['image']['name'];
}
if(move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)){
	$ipath=$uploadfile; 
}
        //This is the problem query
$item=mysql_query("INSERT INTO `stauctioneer_items` (`name`, `desc`, `ipath`, `dateadded`, `barcode`, `quantity`, `received`) VALUES ('$name', '$desc', '$ipath', NOW(), '$barcode', '$quantity', '$received')"); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/82167-solved-query-happening-twice/
Share on other sites

The function is inside of func.php.  Here is the file that calls the function. 

 

I also noticed that the blank record is received it's id before the real record.  So basically the problem is that for every time add_item() is called i get both a blank row and the actual filled row of data.

 

<?php
include("dbc.php");
include("func.php");
if(!isset($_GET['sp'])){ $_GET['sp']="li"; }
if($_GET['sp']=="af"){ add_item_form(); }
else if($_GET['sp']=="ai"){ add_item(); 
	add_item_form(); }
else if($_GET['sp']=="li"){ list_items(); }
else if($_GET['sp']=="id"){ item_details(); }
else if($_GET['sp']=="ui"){ update_item();
	list_items(); }
else if($_GET['sp']=="sf"){ search_item_form(); }
else if($_GET['sp']=="si"){ search_item(); }
else if($_GET['sp']=="di"){ delete_item(); 
	list_items(); }
?>

 

 

excuse my sloppy and noobish coding ways....

 

in the action part of the form, al just tells index.php which file to include, in this case it will include the file i posted which calls the functions.

 

function add_item_form(){
echo'<form enctype="multipart/form-data" action="index.php?al=mi&sp=ai" method="post">
	<table border="0" cellspacing="0" cellpadding="2">
	<tr>
		<td>Item Name</td>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<td>Item Description:</td>
		<td><textarea rows="2" cols="75" name="desc"></textarea></td>
	</tr>
	<tr>
		<td>Barcode:</td>
		<td><input type="text" name="barcode"></td>
	</tr>
	<tr>
		<td>Quantity:</td>
		<td><input type="text" name="quantity"></td>
	</tr>
	<tr>
		<td>Date received:</td>
		<td> <script>DateInput(\'received\', true)</script></td>
	</tr>
	<tr>
		<td>Upload Image:</td>
		<td><input name="image" type="file"></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" value="Create Item"></td>
	</tr>
	</table>

	</form>'; }


i posted this one already, its manageitems.php included in index.php if al=mi in the url

 

<?php
include("dbc.php");
include("func.php");
if(!isset($_GET['sp'])){ $_GET['sp']="li"; }
if($_GET['sp']=="af"){ add_item_form(); }
else if($_GET['sp']=="ai"){ add_item(); 
	add_item_form(); }
else if($_GET['sp']=="li"){ list_items(); }
else if($_GET['sp']=="id"){ item_details(); }
else if($_GET['sp']=="ui"){ update_item();
	list_items(); }
else if($_GET['sp']=="sf"){ search_item_form(); }
else if($_GET['sp']=="si"){ search_item(); }
else if($_GET['sp']=="di"){ delete_item(); 
	list_items(); }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.