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
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(); }
?>

 

 

Link to comment
Share on other sites

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


Link to comment
Share on other sites

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(); }
?>

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.