ed01 Posted November 10, 2011 Share Posted November 10, 2011 I'm trying to create a INSERT query statement that makes use of the content of 'include' files. When I call up the include it simply echos it to the screen but I can't seem to capture the text string and actually make it function as part of the query. I've tried setting the include as a variable but again it only outputs to the screen. Help please! Here's a portion of the code... if (($_POST['sched_templates']) == "sched_TestingOnly.htm") { $query = include 'sched_TestingOnlyQuery1.htm'; } if (@mysql_query ($query)) { mysql_close(); // End adding Registration to the database header ('Location: redirectfile.htm'); exit(); } else { print "<p><b>Sorry, your entry could not be entered. } Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/ Share on other sites More sharing options...
trq Posted November 10, 2011 Share Posted November 10, 2011 Firstly, sched_TestingOnlyQuery1.htm would need to be a php file. That file would then need to return it's data. eg; sched_TestingOnlyQuery1.php <?php return "SELECT bar FROM foo"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1286964 Share on other sites More sharing options...
ed01 Posted November 10, 2011 Author Share Posted November 10, 2011 Thank you for the suggestion!! OK, I tried that ... if (($_POST['sched_templates']) == "sched_TestingOnly.htm") { $query = include 'sched_TestingOnlyQuery1.php'; return $query; } however the script ends with an error and displays "Query was emptyThe query was ." Below is the content of the new php file and if I test with just that code it posts perfectly to my table, so all other parts are working OK, it's just capturing and applying into the query statement that it doesn't like to do. Syntax problems? $query = "INSERT INTO sched_boys (game_id, division, level, hometeam, awayteam, field, weekdate, day, time, comment, referee) VALUES ('NULL', '{$_POST['division'][0]}', '{$_POST['level'][0]}', '{$_POST['hometeam'][0]}', '{$_POST['awayteam'][0]}', '{$_POST['field'][0]}', '{$_POST['weekdate'][0]}', '{$_POST['day'][0]}', '{$_POST['time'][0]}', '{$_POST['comment'][0]}', '{$_POST['referee'][0]}' ), ('NULL', '{$_POST['division'][1]}', '{$_POST['level'][1]}', '{$_POST['hometeam'][1]}', '{$_POST['awayteam'][1]}', '{$_POST['field'][1]}', '{$_POST['weekdate'][1]}', '{$_POST['day'][1]}', '{$_POST['time'][1]}', '{$_POST['comment'][1]}', '{$_POST['referee'][1]}' )"; Again - thanks so much for any help! Ed Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287074 Share on other sites More sharing options...
trq Posted November 10, 2011 Share Posted November 10, 2011 Read my previous reply again. Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287197 Share on other sites More sharing options...
ed01 Posted November 11, 2011 Author Share Posted November 11, 2011 Can't get it working - looks impossible .... :'( Help anyone?... :'( Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287259 Share on other sites More sharing options...
trq Posted November 11, 2011 Share Posted November 11, 2011 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287270 Share on other sites More sharing options...
ed01 Posted November 12, 2011 Author Share Posted November 12, 2011 Sorry but I've already messed around with my code too much for it to likely be of any additional help. The code above is the jist of what I'm trying to do. When I use INCLUDE (in my second example above ie. $query = include 'sched_TestingOnlyQuery1.php'; ) it just puts the code from the include file onto the browser screen when I complete my script. Instead what I'm trying to do is APPLY it into the INSERT query statement as a variable so my script can use it to INSERT into my table. I'm baffled why this isn't possible. As always anyone's help is greatly appreciated!!! Ed Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287497 Share on other sites More sharing options...
trq Posted November 12, 2011 Share Posted November 12, 2011 Are you returning the data within your include like I suggested a few days ago? Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287499 Share on other sites More sharing options...
ed01 Posted November 12, 2011 Author Share Posted November 12, 2011 yes, I think I've finally got the right code to return the data from the include file. The content in my include file (sched_TestingOnlyQuery1.php) is as follows: <? return "INSERT INTO sched_boys (game_id, division, level, hometeam, awayteam, field, weekdate, day, time, comment, referee) VALUES ('NULL', '{$_POST['division'][0]}', '{$_POST['level'][0]}', '{$_POST['hometeam'][0]}', '{$_POST['awayteam'][0]}', '{$_POST['field'][0]}', '{$_POST['weekdate'][0]}', '{$_POST['day'][0]}', '{$_POST['time'][0]}', '{$_POST['comment'][0]}', '{$_POST['referee'][0]}' ), ('NULL', '{$_POST['division'][1]}', '{$_POST['level'][1]}', '{$_POST['hometeam'][1]}', '{$_POST['awayteam'][1]}', '{$_POST['field'][1]}', '{$_POST['weekdate'][1]}', '{$_POST['day'][1]}', '{$_POST['time'][1]}', '{$_POST['comment'][1]}', '{$_POST['referee'][1]}' )"; ?> Now, as requested, below is my script code: Note; I've commented out the INSERT query, it is there for testing. Note: I suspect there may be a problem with the nested "if" statements under the comment "// Check if the form has been submitted." Thanks. <?php // Turn on output buffering ob_start(); // - sched_boys.php // This page allows login to view the Boys Schedule // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>CREATE SCHEDULE</title> </head> <body> <?php // is someone logged in? if (isset($_SESSION['loggedin'])) { require_once 'db.php'; if (!($connection = @ mysql_connect($hostname, $username, $password))) showerror(); if (!mysql_select_db($databasename, $connection)) showerror(); // Check if the form has been submitted. if ( isset ($_POST['submit-AddWeek'])) { if (($_POST['sched_templates']) == "sched_TestingOnly.htm") { $query = include 'sched_TestingOnlyQuery1.php'; $query = '$query = ' . '"' . $query . '"'; } //$query = "INSERT INTO sched_boys (game_id, division, level, hometeam, awayteam, field, weekdate, day, time, comment, referee) VALUES ('NULL', '{$_POST['division'][0]}', '{$_POST['level'][0]}', '{$_POST['hometeam'][0]}', '{$_POST['awayteam'][0]}', '{$_POST['field'][0]}', '{$_POST['weekdate'][0]}', '{$_POST['day'][0]}', '{$_POST['time'][0]}', '{$_POST['comment'][0]}', '{$_POST['referee'][0]}' ), ('NULL', '{$_POST['division'][1]}', '{$_POST['level'][1]}', '{$_POST['hometeam'][1]}', '{$_POST['awayteam'][1]}', '{$_POST['field'][1]}', '{$_POST['weekdate'][1]}', '{$_POST['day'][1]}', '{$_POST['time'][1]}', '{$_POST['comment'][1]}', '{$_POST['referee'][1]}' )";*/ // Execute the query. if (@mysql_query ($query)) { mysql_close(); // End adding Registration to the database header ('Location: CMFSCregistration_reply.htm'); exit(); } else { /*print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";*/ print "<p><b>Sorry, your entry could not be entered. This is most often due to punctuation, particularly single and double quotation marks. Please remove punctuation in the form and try again.</b></p><p>" . mysql_error() . "The query was $query.</p>"; // print $try; } // end else } else { // end if POST submit-AddWeek - LINE 36 // $month = ($_POST['month']); ?> <!-- DISPLAY THE 'CHOOSE A DATE' FORM --> <div align=center> <form id="form2" name="setdate-template" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post"> <table summary="choose a date" width="800" align="center" bgcolor="#ffffff" cellpadding="0" cellspacing="0" border="0"> <tr><td align="center"><h2>Create Schedule for New Week (boys or girls)</h2></td></tr> <tr><td align="center"><strong>Choose a date for the new schedule: </strong> <?PHP // insert require file to build menus for dates require_once("date_time.php"); // print out months for selection menu print 'month: '; print '<select name="month">'; print $month_; // call up the array from the require file print '</select> '; // print out day for selection menu print ' day: '; print '<select name="day">'; print $day_; // call up the array from the require file print '</select> '; // print out year for selection menu print '<select name="year">'; print $year_; // call up the array from the require file print '</select> '; $month = ($_POST['month']); if ($month == "January") {$month = 1;} if ($month == "February") {$month = 2;} if ($month == "March") {$month = 3;} if ($month == "April") {$month = 4;} if ($month == "May") {$month = 5;} if ($month == "June") {$month = 6;} if ($month == "July") {$month = 7;} if ($month == "August") {$month = 8;} if ($month == "September") {$month = 9;} if ($month == "October") {$month = 10;} if ($month == "November") {$month = 11;} if ($month == "December") {$month = 12;} $day = ($_POST['day']); $year = ($_POST['year']); $timestamp = mktime (0, 0, 0, (int)$month, (int)$day, (int)$year); print '<br />'."You must choose and set a date for the new schedule before adding a week".'<br />'; ?> </td> </tr> <tr><td align="center">You have chosen to create a schedule for the following date: <font color="#FF0000"><? echo date ('l, n/j/Y', $timestamp); ?></font></td></tr> </table> <br /> <?php $weekday = date ('l', $timestamp); $weekdate = date ('n/j/Y', $timestamp); ?> <!-- DISPLAY FORM TO CALL UP A SCHEDULE TEMPLATE --> <?php // create list of years for sticky forms $templatearr = array( "sched_boysTemplate1.htm", "sched_boysTemplate2.htm", "sched_girlsTemplate1.htm", "sched_girlsTemplate2.htm", "sched_TestingOnly.htm" ); //$template_ = ''; foreach($templatearr as $key) { $template_ .= "<option value=\"$key\"".($_POST['sched_templates'] == $key ? " selected=\"selected\"" : "").">{$key}</option>\n"; } if (strpos($template_, "selected=")) { $template_ = "<option value=\"\">choose</option>\n{$template_}"; } else { $template_ = "<option value=\"\" selected=\"selected\">choose</option>\n{$template_}"; } ?> <?php // display selection menu print 'Choose a template to import: '; print '<select name="sched_templates">' . "echo $template_;" . '</select> '; // print $template_; // call up the array // print '</select> '; ?> <!-- <input type="submit" name="form1" id="form-choosetemplate" value="Choose a template" />--> <input type="submit" name="setdate-template" id="form2" value="Lock in Date and working template now" /> </form> </div> <br /> <!-- DISPLAY THE 'CREATE A NEW WEEK' FORM AND RECORDS --> <form name="submit-AddWeek" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" > <table summary="choose a date" width="700" align="center" cellpadding="3" cellspacing="1" border="0" > <tr> <td></td> <td>DIV</td> <td>LVL</td> <td>HOME TEAM</td> <td>AWAY TEAM</td> <td>FIELD</td> <td></td> <td></td> <td>TIME</td> <td>COMMENT</td> <td></td> </tr> <? if (($_POST['sched_templates']) == "") { } else { include "{$_POST['sched_templates']}"; } ?> <td colspan="11"> <div align="left"> <p align="center"> <? if ( isset ($_POST['setdate-template'])) { ?> <? //if ($week == 0) { //print $week; //$weekerror = "You must choose a new week before adding a week"; //print $weekerror; //print '<input type="submit" name="sub_mit" value="Add week" />'. "{$weekerror}"; //print $weekerror; //} else { print '<input type="submit" name="submit-AddWeek" value="Add week now" />'; //} ?> <? } else { print "You must choose a date first before the Submit button is available here"; } ?> </p> </div> </td> </tr> </table> </form><!-- close submit-AddWeek form --> <? } // end if isset POST submit // end if isset SESSION } else { // Basic HTML formatting stuff. // Check if the form has been submitted. if ( isset ($_POST['submit-login'])) { // Handle the form. if ( (!empty ($_POST['username'])) && (!empty ($_POST['password'])) ) { if ( ($_POST['username'] == 'removed') && ($_POST['password'] == 'removed') ) { // Okay. // Do session stuff $_SESSION['username'] = 'removed'; $_SESSION['password'] = 'removed'; $_SESSION['loggedin'] = time(); // redirect logged in user back to this page header("Location: {$_SERVER['PHP_SELF']}"); exit(); } else { // Not okay. print '<p>The submitted username and password do not match those on file!<br />Go back and try again.</p>'; } } else { // Forgot a field. print '<p>Please make sure you enter both a username and a password!<br />Go back and try again.</p>'; } } else { // Basic HTML formatting stuff. print '<h2>Field Scheduler Login</h2> <p>Please enter your User Name and Password.</p>'; // Display the form. print '<form action="sched_create.php" method="post"><p> Username: <input type="text" name="username" size="20" /><br /> Password: <input type="password" name="password" size="20" /><br /> <input type="submit" name="submit-login" value="Log In!" /></p> </form>'; } // end else } ?> </body> </html> <?php // Turn off output buffering ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287533 Share on other sites More sharing options...
ed01 Posted November 12, 2011 Author Share Posted November 12, 2011 Just to be clear; this script is still NOT WORKING . The output of the include file appears to create a complete insert query but it still isn't captured such that it can be used in the overall script. Bah! Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287650 Share on other sites More sharing options...
trq Posted November 12, 2011 Share Posted November 12, 2011 This: $query = '$query = ' . '"' . $query . '"'; Is making the query invalid. Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287685 Share on other sites More sharing options...
ed01 Posted November 13, 2011 Author Share Posted November 13, 2011 That may be, but I have a bigger problem. When I click the button to POST the submit-AddWeek form it will not even look at the following IF statement that asks it to compare POST 'schedule_template'. It always bypasses it entirely! And continues to output to the screen only "Query was emptyThe query was ." - UGH! See the code below that does NOT print out "testing". Is there a rule about not being able to capture an IF statement POST within ANOTHER IF statement POST?? Thanks again! // Check if the form has been submitted. if ( isset ($_POST['submit-AddWeek'])) { if (($_POST['sched_templates']) == 'sched_TestingOnly.htm') { // $query = include 'sched_TestingOnlyQuery1.php'; // $query = '$query = ' . '"' . $query . '"'; print "testing"; } /*$query = "INSERT INTO sched_boys (game_id, division, level, hometeam, awayteam, field, weekdate, day, time, comment, referee) VALUES ('NULL', '{$_POST['division'][0]}', '{$_POST['level'][0]}', '{$_POST['hometeam'][0]}', '{$_POST['awayteam'][0]}', '{$_POST['field'][0]}', '{$_POST['weekdate'][0]}', '{$_POST['day'][0]}', '{$_POST['time'][0]}', '{$_POST['comment'][0]}', '{$_POST['referee'][0]}' ), ('NULL', '{$_POST['division'][1]}', '{$_POST['level'][1]}', '{$_POST['hometeam'][1]}', '{$_POST['awayteam'][1]}', '{$_POST['field'][1]}', '{$_POST['weekdate'][1]}', '{$_POST['day'][1]}', '{$_POST['time'][1]}', '{$_POST['comment'][1]}', '{$_POST['referee'][1]}' )";*/ // Execute the query. if (@mysql_query ($query)) { mysql_close(); // End adding Registration to the database header ('Location: CMFSCregistration_reply.htm'); exit(); Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287737 Share on other sites More sharing options...
trq Posted November 13, 2011 Share Posted November 13, 2011 Can you please use tags? Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287756 Share on other sites More sharing options...
ed01 Posted November 13, 2011 Author Share Posted November 13, 2011 Oh OK. Sure will do in future. Ed Quote Link to comment https://forums.phpfreaks.com/topic/250839-how-to-use-content-from-include-file-within-insert-query-statement/#findComment-1287759 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.