ztimer Posted January 14, 2015 Share Posted January 14, 2015 (edited) Hi all, Its been a long time since last help request from real professional from here but I'm again in trouble with a much more spectacular plan I'm working on.For those who are interested in the plan then here it is:My Idea was to make a new build starting from scratch and make it as dynamical as possible. So my goal is not to make almost anything fixed in the code. I have made a decision to make a one supper large table for multiple different entries so no more joining and no more views for me.! In this help request I'm having trouble with Posting values to a page processing page lets call it record_changer.php The sole purpose of this file is to get form posts and decide what to do. Either update, delete, or insert.record_changer.php <?php include '../../config/config.inc.php'; if(is_ajax()){ # Checks if action value exists if(isset($_POST["action"]) && !empty($_POST["action"])){ $action = $_POST["action"]; # Switch case for value of action switch($action){ case "insert": datatable_insert_function(); break; case "update": datatable_update_function(); break; case "delete": datatable_delete_function(); break; } } } # Function to check if the request is an AJAX request function is_ajax(){ return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; } function datatable_insert_function(){ } function datatable_update_function(){ } function datatable_delete_function(){ } ?> The problem.The problem is that the $insert places two entries to the DB. I cant seem to understand why.? # Test _POST values $_POST['UserID'] = '2'; $_POST['WorkID'] = '22'; $_POST['Status'] = '1'; $_POST['Code'] = '1'; $_POST['Title'] = '1'; $columns = array(); foreach(array_keys($_POST) as $name){ # Exclude Action and ID if($name == 'Action' || $name == 'ID' || $name == 'submit' ){ continue; } $columns[] = $name; } print_r($columns); echo "<br>"; $data = array_fill_keys($columns, 'NULL'); print_r($data); foreach($data as $key => $value){ $data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_real_escape_string($_POST[$key])."'"; } echo "<br>"; print_r($data); $insert = mysql_query('INSERT INTO datatable (ID, '.implode(', ',$columns).')VALUES (null, '.implode(',',$data).')') or die(mysql_error()); No errors no nothing. Just two entries of correct data. PS. Sorry for a lot of prints in the code it is work and idea in the progress. The posts at the moment are fixed in the code so it is easier to refresh and debug.Please help if you spot the problem. Im really out of ideas. Some fresh eyes might make a difference.And Please for those who want to say it is a bad idea and why and why and so on.. Move a long.!!! Im not interested in whinging i have a great use for this and just having trouble with the two entries.Thanks. Edited January 14, 2015 by ztimer Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/ Share on other sites More sharing options...
mac_gyver Posted January 14, 2015 Share Posted January 14, 2015 your browser is probably requesting the php page twice, though there are server-side reasons pages can get requested/run twice. have you checked your web server access log to look at the requests being made to see if there are two requests together? Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502940 Share on other sites More sharing options...
ztimer Posted January 14, 2015 Author Share Posted January 14, 2015 I don't believe this. This has never happened in my history of code before. But no. I have not checked this .. It is not probable. Sorry Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502941 Share on other sites More sharing options...
ztimer Posted January 14, 2015 Author Share Posted January 14, 2015 your browser is probably requesting the php page twice, though there are server-side reasons pages can get requested/run twice. have you checked your web server access log to look at the requests being made to see if there are two requests together? Ok so this made me suspicious and I am desperate so I went to the logs and deleted them all. Made one try to load the page and one request is loged. Sorry .. This is not the case. Ps. Did try just in case with multiple browsers also. Same thing. Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502944 Share on other sites More sharing options...
mac_gyver Posted January 14, 2015 Share Posted January 14, 2015 then that narrows down the problem to something on the server-side. does the code where your insert query is at only exist once on the page? is it actually being included, and could be included/required twice? is the code where your insert query at, actually inside the datatable_insert_function() function being called by the switch/case statement or is it inline with all the main code and that's actually the same page as your main code, so that it gets requested once when you browse to the page and a second time by the ajax request? 1 Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502948 Share on other sites More sharing options...
ztimer Posted January 14, 2015 Author Share Posted January 14, 2015 then that narrows down the problem to something on the server-side. does the code where your insert query is at only exist once on the page? is it actually being included, and could be included/required twice? is the code where your insert query at, actually inside the datatable_insert_function() function being called by the switch/case statement or is it inline with all the main code and that's actually the same page as your main code, so that it gets requested once when you browse to the page and a second time by the ajax request? Noooooooo..... Ok its too late to debug this today but yes. I just made a test.php to the root of the web dir and yes. it does insert one entry. Thanks for this .. Seems it is indeed in the coding but now lets talks some more. I have the index page buidup like if(__FILE__ == $_SERVER['SCRIPT_FILENAME']) exit('Restricted!'); include 'config/config.inc.php'; session_start(); if(!isset($_SESSION['Username'])){ header("Location: login.php"); } include 'modules/header.php'; ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); if(isset($_GET['app'])){ if(!@include('modules/'.$_GET['app']."/main.php")){ include 'modules/error.php'; } }else{ include 'modules/main.php'; } include 'modules/footer.php'; and the page that was tested is in modules and main.php requested as http://address?app=test how to prevent this of happening. If you are still willing.. Well im happy to get at least some lead for tomorrow so thanks for this @mac_gyver but if you have some fix for me i would love to read and continue from there tomorrow.. Thanks again for pointing to the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502952 Share on other sites More sharing options...
mac_gyver Posted January 14, 2015 Share Posted January 14, 2015 the only thing in the posted code that could cause an 'unexpected' occurrence of your code to be ran is this - if(!isset($_SESSION['Username'])){ header("Location: login.php"); } since you don't have an exit;/die; statement after the header() redirect, when you are not logged in and visit that page, the rest of the code you have shown still runs, which would cause an insert query to be run. then if you then login and repeating the process, a second insert query would run. i also hope you plan on validating the $_GET['app'] value before blindly putting it into an include statement, as an un-validated input will allow directory traversal and let anyone include anything they want, which would typically be used for privilege elevation (a regular user can cause the code to include administrative level files.) 1 Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502956 Share on other sites More sharing options...
ztimer Posted January 14, 2015 Author Share Posted January 14, 2015 (edited) the only thing in the posted code that could cause an 'unexpected' occurrence of your code to be ran is this - if(!isset($_SESSION['Username'])){ header("Location: login.php"); } since you don't have an exit;/die; statement after the header() redirect, when you are not logged in and visit that page, the rest of the code you have shown still runs, which would cause an insert query to be run. then if you then login and repeating the process, a second insert query would run. Ok. Sorry I lost you there can you be more specific. Code example or something . Thanks. Im clearly not at the level you are on yet. Still learning new tricks. How could i fix this. I would really love to leave my index base layout like it is. It seems promising to me.. And I really appreciate your help. Edited January 14, 2015 by ztimer Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502957 Share on other sites More sharing options...
CroNiX Posted January 14, 2015 Share Posted January 14, 2015 if(!isset($_SESSION['Username'])){ header("Location: login.php"); return; //or exit(), or die() } Make sure code execution stops when redirecting. Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502958 Share on other sites More sharing options...
ztimer Posted January 14, 2015 Author Share Posted January 14, 2015 if(!isset($_SESSION['Username'])){ header("Location: login.php"); return; //or exit(), or die() } Make sure code execution stops when redirecting. Ok so i set my index to if(__FILE__ == $_SERVER['SCRIPT_FILENAME']) exit('Restricted!'); include 'config/config.inc.php'; session_start(); if(!isset($_SESSION['Username'])){ header("Location: login.php"); return; //or exit(), or die() } include 'modules/header.php'; ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); if(isset($_GET['app'])){ if(!@include('modules/'.$_GET['app']."/main.php")){ include 'modules/error.php'; } }else{ include 'modules/main.php'; } include 'modules/footer.php'; Now i have 3 same entries .. not two. any ideas. Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502961 Share on other sites More sharing options...
Solution mac_gyver Posted January 15, 2015 Solution Share Posted January 15, 2015 based on this direct access check - if(__FILE__ == $_SERVER['SCRIPT_FILENAME']) exit('Restricted!');, the code you are showing us is in an included file. return'ing from an included file returns to the main program that included the code and the main code still continues to run, and who knows what it is doing, redirecting/including... you need to use an actual exit; statement to insure that code stops running. it's also possible that something your login.php page is doing is causing the problem. this code is include/redirect happy, making it hard, without having all the code, to determine everything it is actually doing. some general debugging tips - i would put the ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); as the first thing after your first opening <?php tag in your main code (or ever better, as settings in your php.ini) so that any errors in the include/session_start/header() redirect will also be reported. make sure that output_buffering is set to false in your php.ini and that you are not using any output buffing in your code. having buffering on hides both php error messages and output that your code sends and will also allow header()/session_start() statements to work in instances where the code on your page is laid out incorrectly. Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1502969 Share on other sites More sharing options...
ztimer Posted January 15, 2015 Author Share Posted January 15, 2015 I got it working. Did all the things mentioned here and still multiple entries. So I went crazy and started chopping out code "includes" like header sidebar and footer and still multiple entries.Then I went to the actual main.php file and on top there was include 'modules/topbar.php';Took that away and all was well.It had only the code below inside. As Im using a template to build it on i have not yet managed to get to that part but What is making insert to make two or three entris in this part of the code. At the moment I removed Search , tasks and messages and now the code works and does not loop or request twice. <!-- START X-NAVIGATION VERTICAL --> <ul class="x-navigation x-navigation-horizontal x-navigation-panel"> <!-- TOGGLE NAVIGATION --> <li class="xn-icon-button"> <a href="#" class="x-navigation-minimize"><span class="fa fa-dedent"></span></a> </li> <!-- END TOGGLE NAVIGATION --> <!-- SEARCH --> <li class="xn-search"> <form role="form"> <input type="text" name="search" placeholder="Search..."/> </form> </li> <!-- END SEARCH --> <!-- SIGN OUT --> <li class="xn-icon-button pull-right"> <a href="#" class="mb-control" data-box="#mb-signout"><span class="fa fa-sign-out"></span></a> </li> <!-- END SIGN OUT --> <!-- MESSAGES --> <li class="xn-icon-button pull-right"> <a href="#"><span class="fa fa-comments"></span></a> <div class="informer informer-danger">4</div> <div class="panel panel-primary animated zoomIn xn-drop-left xn-panel-dragging"> <div class="panel-heading"> <h3 class="panel-title"><span class="fa fa-comments"></span> Messages</h3> <div class="pull-right"> <span class="label label-danger">4 new</span> </div> </div> <div class="panel-body list-group list-group-contacts scroll" style="height: 200px;"> <a href="#" class="list-group-item"> <div class="list-group-status status-online"></div> <img src="#" class="pull-left" alt="John Doe"/> <span class="contacts-title">John Doe</span> <p>Praesent placerat tellus id augue condimentum</p> </a> <a href="#" class="list-group-item"> <div class="list-group-status status-away"></div> <img src="#" class="pull-left" alt="Dmitry Ivaniuk"/> <span class="contacts-title">Dmitry Ivaniuk</span> <p>Donec risus sapien, sagittis et magna quis</p> </a> <a href="#" class="list-group-item"> <div class="list-group-status status-away"></div> <img src="#" class="pull-left" alt="Nadia Ali"/> <span class="contacts-title">Nadia Ali</span> <p>Mauris vel eros ut nunc rhoncus cursus sed</p> </a> <a href="#" class="list-group-item"> <div class="list-group-status status-offline"></div> <img src="#" class="pull-left" alt="Darth Vader"/> <span class="contacts-title">Darth Vader</span> <p>I want my money back!</p> </a> </div> <div class="panel-footer text-center"> <a href="pages-messages.html">Show all messages</a> </div> </div> </li> <!-- END MESSAGES --> <!-- TASKS --> <li class="xn-icon-button pull-right"> <a href="#"><span class="fa fa-tasks"></span></a> <div class="informer informer-warning">3</div> <div class="panel panel-primary animated zoomIn xn-drop-left xn-panel-dragging"> <div class="panel-heading"> <h3 class="panel-title"><span class="fa fa-tasks"></span> Tasks</h3> <div class="pull-right"> <span class="label label-warning">3 active</span> </div> </div> <div class="panel-body list-group scroll" style="height: 200px;"> <a class="list-group-item" href="#"> <strong>Phasellus augue arcu, elementum</strong> <div class="progress progress-small progress-striped active"> <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width: 50%;">50%</div> </div> <small class="text-muted">John Doe, 25 Sep 2014 / 50%</small> </a> <a class="list-group-item" href="#"> <strong>Aenean ac cursus</strong> <div class="progress progress-small progress-striped active"> <div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 80%;">80%</div> </div> <small class="text-muted">Dmitry Ivaniuk, 24 Sep 2014 / 80%</small> </a> <a class="list-group-item" href="#"> <strong>Lorem ipsum dolor</strong> <div class="progress progress-small progress-striped active"> <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="95" aria-valuemin="0" aria-valuemax="100" style="width: 95%;">95%</div> </div> <small class="text-muted">John Doe, 23 Sep 2014 / 95%</small> </a> <a class="list-group-item" href="#"> <strong>Cras suscipit ac quam at tincidunt.</strong> <div class="progress progress-small"> <div class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%;">100%</div> </div> <small class="text-muted">John Doe, 21 Sep 2014 /</small><small class="text-success"> Done</small> </a> </div> <div class="panel-footer text-center"> <a href="pages-tasks.html">Show all tasks</a> </div> </div> </li> <!-- END TASKS --> </ul> <!-- END X-NAVIGATION VERTICAL --> Quote Link to comment https://forums.phpfreaks.com/topic/293923-dynamic-form-post-using-implode-inserts-duplicate-entries/#findComment-1503011 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.