cedtech23 Posted November 30, 2007 Share Posted November 30, 2007 I am attempting to use an IF statement to see if the submit button was not pressed display a form. That works. The ELSE part means that the submit button has been pressed so add the data to a MYSQL table. The window is blank after the submit button is pressed. I looked at the table and no information is transferred. I put an echo statement and still no results. Why is the ELSE statement not executing? <?php #if submit button not pressed show form if(!isset($_POST['submit'])) { ?> <form id="insert" method="post" action="<?PHP $_SERVER['PHP_SELF']; ?>" > <fieldset> <p><label for="prov_f_name">Provider First Name<br /></label><input type="text" name="prov_f_name" id="prov_f_name" /></p> <p><label for="prov_l_name">Provider Last Name<br /></label><input type="text" name="prov_l_name" id="prov_l_name" /></p> <p class="btns"><input type="submit" name="submit" value="Submit" /></p> </fieldset> </form> </div> <?php } else{ include('include/conn.inc.php'); $sql = "INSERT INTO providers(f_name, l_name) VALUES ('$_POST[prov_f_name]', '$_POST[prov_l_name]')"; $result = mysql_query($sql, $conn) or die(mysql_error()); echo $result; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/ Share on other sites More sharing options...
revraz Posted November 30, 2007 Share Posted November 30, 2007 Your Insert statement is incorrect in regards to your $_POST variables. Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403007 Share on other sites More sharing options...
stuffradio Posted November 30, 2007 Share Posted November 30, 2007 I'm not really sure... do you have errors turned off? Is there cPanel on that machine? If so, check the error file. It might tell you in there Btw... instead of using include(); using include_once(); or require_once(); Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403008 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 Your Insert statement is incorrect in regards to your $_POST variables. I don't see the error in the insert statement the $_POST variables are named prov_f_name and prov_l_name. Please give more detail Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403027 Share on other sites More sharing options...
revraz Posted November 30, 2007 Share Posted November 30, 2007 Look at this thread and see how they are posting to the DB http://www.phpfreaks.com/forums/index.php/topic,94674.0.html you could also do it other ways. Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403032 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 I'm not really sure... do you have errors turned off? Is there cPanel on that machine? If so, check the error file. It might tell you in there Btw... instead of using include(); using include_once(); or require_once(); errors is turned on, it's a local server in house fedora server. what's the difference in using nclude(); using include_once(); or require_once(); ? Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403034 Share on other sites More sharing options...
revraz Posted November 30, 2007 Share Posted November 30, 2007 Include will include it as many times as you call it Include_once will only call it once Require will provide a different error if the file does not exist Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403043 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 I did an echo statement of the $sql and it proceduced INSERT INTO providers (f_name, l_name) VALUES ('ter' , 'ty') Can you tell me what's wrong with that statement? the reason I as is that the same statements work in my other scripts. The table it's writing into structure is: +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default Extra | +---------+-------------+------+-----+---------+----------------+ | prov_id | int(11) | NO | PRI | NULL | auto_increment | | f_name | varchar(50) | YES | | NULL | | | l_name | varchar(50) | YES | | NULL | | +---------+-------------+------+-----+---------+----------------+ Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403105 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 After sometime I have narrowed it down to this line causing the issue $result = mysql_query($sql, $conn) or die(mysql_error()); If I uncomment it I can see the page? I am lost any ideas Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403127 Share on other sites More sharing options...
revraz Posted November 30, 2007 Share Posted November 30, 2007 <?php $fname = (!isset($_POST["prov_f_name"]) || trim($_POST["prov_f_name"]) == "") ? die ("ERROR: Enter a user name") : mysql_escape_string(trim($_POST["prov_f_name"])); $lname = (!isset($_POST["prov_l_name"]) || trim($_POST["prov_l_name"]) == "") ? die ("ERROR: Enter a password") : mysql_escape_string(trim($_POST["prov_l_name"])); include('include/conn.inc.php'); $insert = "INSERT INTO providers (f_name, l_name) VALUES ('$fname', '$lname')"; $result = mysql_query($insert) or die ("Error in query: $result. " . mysql_error()); echo "User Added: " . $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403129 Share on other sites More sharing options...
revraz Posted November 30, 2007 Share Posted November 30, 2007 Also, change your DB to for l_name and f_name, so they can't be NULL. Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403131 Share on other sites More sharing options...
revraz Posted November 30, 2007 Share Posted November 30, 2007 If that doesn't work, you may want to make sure your conn.inc.php is right or just add the connection info right to the page. Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403139 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 The query is fine. Just add some protection to it. <?php $fname = mysql_real_escape_string(trim($_POST['prov_f_name'])); $lname = mysql_real_escape_string(trim($_POST['prov_l_name'])); if($fname && $lname){ $sql = "INSERT INTO providers (`f_name`,`l_name`) VALUES('$fname','$lname');"; $res = mysql_query($sql) or die(mysql_error()); echo "You have been added as a provider successfully, " . $fname . " " . $lname; }else { echo "You did not supply a first name and/or last name!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403146 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 I made those changes to the database.. here is the connection code Thanks efine('SQL_HOST','localhost'); define('SQL_USER','testadmin'); define('SQL_PASS','test123'); define('SQL_DB','testdb'); $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Could not connect to MySQL database. ' . mysql_error()); mysql_select_db(SQL_DB,$conn) or die(mysql_error());; Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403152 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 Why do we need that information? Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403160 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 I followed your advice mgallforever but the script kept returning 'You did not supply a first name and/or last name!' so I did some testing of a value before and after I apply mysql_real_escape_string and trim using the code below. Example if I put Cedric Young before the function are apply the echo statement produces "Cedric Spence' After mysql_real_escape_string and trim applied the results are blank. echo $_POST['prov_f_name']; $fname = mysql_real_escape_string(trim($_POST['prov_f_name'])); $lname = mysql_real_escape_string(trim($_POST['prov_l_name'])); echo $fname; Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403162 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 I am create a basic form that will allow user to add information to that table.. It's crazy because that same code works in my other scripts.. Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403167 Share on other sites More sharing options...
cedtech23 Posted November 30, 2007 Author Share Posted November 30, 2007 I need to allow my coworkers to add information into that table. It's crazy because that same code works in other scripts. I missing something simple.. WOW Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403168 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 This worked for me. <?php if(!$_POST['submit']){ echo "<form method=\"post\" action=\"\">"; echo "First: <input type=\"text\" name=\"fname\"><br>\n"; echo "Last: <input type=\"text\" name=\"lname\"><br>\n"; echo "<input type=\"submit\" name=\"submit\" value=\"Go\">\n"; echo "</form>\n"; }else { $fname = trim($_POST['fname']); $lname = trim($_POST['lname']); if($fname && $lname){ echo "You have been added as " . $fname . " " . $lname; }else { echo "Missing one or two fields!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79570-solved-blank-window-after-if-else-statement-to-check-submit/#findComment-403181 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.