Jump to content

[SOLVED] Blank Window after If Else statement to check submit


cedtech23

Recommended Posts

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;


}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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    |                |

+---------+-------------+------+-----+---------+----------------+

 

 

Link to comment
Share on other sites

<?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;
?>

Link to comment
Share on other sites

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!";
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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; 

 

Link to comment
Share on other sites

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!";
	}
}
?>

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.