Stooney Posted December 18, 2007 Share Posted December 18, 2007 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')"); } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 Look ok.. are you sure its only called once ? can you post some code on where its called Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 18, 2007 Author Share Posted December 18, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted December 18, 2007 Share Posted December 18, 2007 What is in the add_item_form() function? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 if its a form you must not be checking for the form is posted and calling the add_item therefore the first time the form is invoked it will be a blank record and second time when it is submitted it will be proper data.. Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 18, 2007 Author Share Posted December 18, 2007 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>'; } Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 18, 2007 Author Share Posted December 18, 2007 You have a point. I'll have add_item() validate form data first. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 can you tell me where add_item function is called ? Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 18, 2007 Author Share Posted December 18, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.