Insert statement does absolutely nothing when i submit the form - php

My insert statement does nothing when i try and submit the form its supposed to show the conformation message but nothing shows up at all and it doesnt submit into the database.
And for reference there is no error code that shows up when i click submit its just that nothing happens.
<?php require_once('dbuser.php');?>
<?php
// if the submit button has been selected
if(isset($_POST['submit_registration'])) {
// assign variable to each for control to capture the values
$first = $_POST['first_name'];
$last = $_POST['last_name'];
$email = $_POST['email'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal = $_POST['postal_code'];
$phone = $_POST['phone'];
$distance = $_POST['dist'];
//assign null values for use with the isset function to identify null fields that are missing a value
$nofirst = null;
$nolast = null;
$noemail = null;
$noaddress1 = null;
$noaddress2 = '';
$nocity = null;
$nostate= null;
$nopostal = null;
$nophone = null;
$nodistance = null;
// if value of variable is nothing assign something other than null
if($first=="") {
$nofirst = '';
$novalue = '';
}
if($last==""){
$nolast = '';
$novalue ='';
}
if($email=="") {
$noemail = '';
$novalue = '';
}
if($address1=="") {
$noaddress1 = '';
$novalue = '';
}
if($address2=="") {
$noaddress2 = '';
$novalue = '';
}
if($city=="") {
$nocity = '';
$novalue = '';
}
if($state=="") {
$nostate = '';
$novalue = '';
}
if($postal=="") {
$nopostal = '';
$novalue = '';
}
if($phone=="") {
$nophone = '';
$novalue = '';
}
if($distance=="") {
$nodistance = '';
$novalue = '';
}
if(isset($novalue)) {$confim = 'Please enter all values';}
else {
$insertsql = "INSERT INTO `runner`(`fname`, `lname`,`email`,`address1`,`address2`,`city`,`state`,`postalcode`, `phone`, `distance`) VALUES ('$first', '$last', '$email', '$address1','$address2','$city','$state','$postal','$phone','$distance')";
mysql_query($connect, $insertsql) or die ("Insert failed: ".mysql_error($connect));
$confirm = 'Thank you for registering!';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Matthew Cromika Website</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<header><img src="images/eastersealsclevelogo.png" alt="Easter Seals Logo" width="445" height="300"</img> </header>
<nav>
<ul>
<li>Home</li>
<li>Run Sign-Up</li>
<li>Refer-a-Friend</li>
</ul>
</nav>
<h1>Sign up for a event!</h1>
<p>If you would like to join in any of the runs, then feel free to sign up using the form provided below.</p><br>
<?php {echo $confirm;} ?>
<form method="post" action="signup.php">
<form name="signup" title="signup" id="signup">
<table>
<tr>
<td><label for="first_name">First Name:</label></td>
<td><input type="text" name="first_name" id="first_name" title="first name" ></td>
<?php if(isset($nofirst)){echo"<span class='errormsg'> First name is required<br></span>";}?>
</tr>
<tr>
<td><label for="last_name">Last Name:</label></td>
<td><input type="text" name="last_name" id="last_name" title="last name" ></td>
<?php if(isset($nolast)){echo"<span class='errormsg'> Last Name is required<br></span>";}?>
</tr>
<tr>
<td><label for="email">Email:</label></td>
<td><input type="email" name="email" id="email" title="email" ></td>
<?php if(isset($noemail)){echo"<span class='errormsg'> Your Email is required<br></span>";}?>
</tr>
<tr>
<td><label for="address1">Address 1:</label></td>
<td><input type="text" name="address1" id="address1" title="address1" ></td>
<?php if(isset($noaddress1)) {echo"<span class='errormsg'> Your Address is required<br></span>";}?>
</tr>
<tr>
<td><label for="address2">Address 2:</label></td>
<td><input type="text" name="address2" id="address2" title="address2"></td>
</tr>
<tr>
<td><label for="city">City:</label></td>
<td><input type="text" name="city" id="city" title="city" value="Cleveland" ></td>
<?php if(isset($nocity)){echo"<span class='errormsg'> Your City is required<br> </span>";}?>
</tr>
<tr>
<td><label for="state">State:</label></td>
<td>
<select required name="state">
<option value="state" name="state" title="state">Select A Option</option>>
<option value="AL" name="AL" title="alabama">Alabama</option>
<option value="AK" name="AK" title="alaska">Alaska</option>
<option value="AZ" name="AZ" title="arizona">Arizona</option>
<option value="AR" name="AR" title="arkansas">Arkansas</option>
<option value="CA" name="CA" title="california">California</option>
<option value="CO" name="CO" title="colorado">Colorado</option>
<option value="CT" name="CT" title="connecticut">Connecticut</option>
<option value="DE" name="DE" title="delware">Delaware</option>
<option value="DC" name="DC" title="district of columbia">District Of Columbia</option>
<option value="FL" name="FL" title="florida">Florida</option>
<option value="GA" name="GA" title="georgia">Georgia</option>
<option value="HI" name="HI" title="hawaii">Hawaii</option>
<option value="ID" name="ID" title="idaho">Idaho</option>
<option value="IL" name="IL" title="illinois">Illinois</option>
<option value="IN" name="IN" title="indiana">Indiana</option>
<option value="IA" name="IA" title="iowa">Iowa</option>
<option value="KS" name="KA" title="kansas">Kansas</option>
<option value="KY" name="KY" title="kentucky">Kentucky</option>
<option value="LA" name="LA" title="louisiana">Louisiana</option>
<option value="ME" name="ME" title="maine">Maine</option>
<option value="MD" name="MD" title="maryland">Maryland</option>
<option value="MA" name="MA" title="massachusetts">Massachusetts</option>
<option value="MI" name="MI" title="michigan">Michigan</option>
<option value="MN" name="MN" title="minnesota">Minnesota</option>
<option value="MS" name="MS" title="mississippi">Mississippi</option>
<option value="MO" name="MO" title="missouri">Missouri</option>
<option value="MT" name="MT" title="montana">Montana</option>
<option value="NE" name="NE" title="nebraska">Nebraska</option>
<option value="NV" name="NV" title="nevada">Nevada</option>
<option value="NH" name="NH" title="new hampshire">New Hampshire</option>
<option value="NJ" name="NJ" title="new jersey">New Jersey</option>
<option value="NM" name="NM" title="new mexico">New Mexico</option>
<option value="NY" name="NY" title="new york">New York</option>
<option value="NC" name="NC" title="north carolina">North Carolina</option>
<option value="ND" name="ND" title="north dakota">North Dakota</option>
<option value="OH" name="OH" title="ohio" selected>Ohio</option>
<option value="OK" name="OK" title="oklahoma">Oklahoma</option>
<option value="OR" name="OR" title="oregon">Oregon</option>
<option value="PA" name="PA" title="pennsylvania">Pennsylvania</option>
<option value="RI" name="RI" title="rhode island">Rhode Island</option>
<option value="SC" name="SC" title="south carolina">South Carolina</option>
<option value="SD" name="SD" title="south dakota">South Dakota</option>
<option value="TN" name="TN" title="tennessee">Tennessee</option>
<option value="TX" name="TX" title="texas">Texas</option>
<option value="UT" name="UT" title="utah">Utah</option>
<option value="VT" name="VT" title="vermont">Vermont</option>
<option value="VA" name="VA" title="virginia">Virginia</option>
<option value="WA" name="WA" title="washington">Washington</option>
<option value="WV" name="WV" title="west virginia">West Virginia</option>
<option value="WI" name="WI" title="wisconsin">Wisconsin</option>
<option value="WY" name="WY" title="wyoming">Wyoming</option>
</select></td>
<?php if(isset($nostate)) {echo "<span class='errormsg'>Your state is required<br></span>";}?>
</tr>
<tr>
<td><label for="postal_code">Zip Code:</label></td>
<td><input type="text" name="postal_code" id="postal_code" title="postal_code" pattern="[\d]{5}(-[\d]{4})" ></td>
<?php if(isset($nopostal)) {echo "<span class='errormsg'> Your postal code is required<br></span>";}?>
</tr>
<tr>
<td><label for="phone">Phone:</label></td>
<td><input type="tel" name="phone" id="phone" title="phone" ></td>
<?php if(isset($nophone)) {echo "<span class='errormsg'> Your Phone number is required<br></span>";}?>
</tr>
<tr>
<td><label for="distance">Distance:</label></td><br>
<td><input type="radio" name="dist" value="1mile" id="dist" title="1mile" checked ><label>1 Mile</label><br>
<input type="radio" name="dist" value="5k" id="dist" title="5k"><label>5K</label><br>
<input type="radio" name="dist" value="10k" id="dist" title="10k"><label>10K</label></td>
<?php if(isset($nodist)) {echo "<span class='errormsg'> You must choose a distance</span>";}?>
</tr>
<tr>
<td> <input type="submit" name="submit_registration" id="submit_registration" title="submit" value="Submit Form"></td>
<td> <input type="button" name="cancel" id="cancel" title="cancel" value="Cancel" onClick="location.href='index.html'"></td>
</tr>
</table>
</form>
<p>If you would like to invite a friend then click here and have them join in!
</p>
</body>
</html>
Any help you can provide is greatly appreciated.

Related

Separating an insert from search MYSQL PHP

I'm having trouble with inserting and searching MYSQL database. If I insert data it adds and searches at the same time and vice versa. I would like to perform only one at a time. I have them both on separate buttons on the html form. Any ideas?
<?php
$host = "localhost";
$user = "root";
$password = "pass";
$dbname = "server";
$link = mysql_connect(localhost, root, pass, server);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(server, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$sql = "INSERT INTO nameOne" . "(firstName, lastName, phone, address, city, state, zip, birthdate, userName, sex, relationship)".
"VALUES ('$firstName', '$lastName', '$phone', '$address', '$city', '$state', '$zip', '$birthdate', '$userName', '$sex', '$relationship')";
mysql_select_db('server');
$retval = mysql_query( $sql, $link );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
$query = sprintf("SELECT firstName, lastName, phone, address, city, state, zip, birthdate, userName, sex, relationship FROM nameOne
WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstName),
mysql_real_escape_string($lastName));
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstName'];
echo $row['lastName'];
echo $row['phone'];
echo $row['address'];
echo $row['city'];
echo $row['state'];
echo $row['zip'];
echo $row['birthdate'];
echo $row['userName'];
echo $row['sex'];
echo $row['relationship'];
}
mysql_close($link);
HTML:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="results.php" method="post">
<table>
<tr>
<td>Enter Name</td>
<td><input type="text" name="firstName"> </td>
</tr>
<tr>
<td>Enter Last Name </td>
<td><input type="text" name="lastName">
</tr>
<tr>
<td>Phone Number</td>
<td><input type="text" name="phone"> </td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address"> </td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city"> </td>
</tr>
<tr>
<td>State</td>
<td class="selection">
<select name="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</td>
<tr>
<td>Zip </td>
<td><input type="text" name="zip"> </td>
</tr>
<tr>
<td>Birthdate</td>
<td><input type="text" name="birthdate"> </td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="userName"> </td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password"> </td>
</tr>
<tr>
<td>Sex</td>
<td><input name="sex" type="radio" checked="checked" value="Male">Male <input name="sex" type="radio" id="mypassword" value="Female">Female
</td>
</tr>
<tr>
<td>Relationship</td>
<td><input type="text" name="relationship"> </td>
</tr>
</table>
<input type="submit" name="submit" value="insert">
<input type="submit" name="update" value="update">
<input type="submit" name="search" value="search">
</form>
</body>
Use following structure of your PHP code:
<?php
// here your database connection
if (isset($_POST['submit'])) {
// your insert code here
}
if (isset($_POST['update'])) {
// your update code here
}
if (isset($_POST['search'])) {
// your search code here
}
First, I will recommend you to use mysqli and second make separate search form from insert and update form. Like:
<form action="search.php" method="post">
<input type="submit" name="search" value="search">
</form>
then write PHP search code in search.php and show.

how to pass values from view-controller-model in codeigniter ? error missing argument and undefined variable

I am new to ci framework so request you to help me . iam not sure iam correctly passing valueds from view-controller-model so please go throughly give ur feedback such that my problem is solved iam getting error as
1)Severity:warning MISSING ARGUMENT 9 FOR searchresultss::login
2)severity:notice Undefined variable
this is controller page
$look = $this->input->post('gender');
$age = $this->input->post('age');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');
$data['showdata'] = $this->searchresultss->login($look,$age,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification);
$this->load->view('searchresult',$data);
this mi my model page
function login($per_page=3,$look,$age,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
{
$query="SELECT *
FROM users
WHERE
if('$se_ct'!='',sect = '$se_ct' AND if('$subsect' !='',subsect = '$subsect',subsect like '%%'),sect like '%%' AND subsect like '%%')
AND
IF( '$cou_ntry' !='', country = '$cou_ntry'
AND
if('$sta_te' !='', state = '$sta_te'
AND
if('$ci_ty' !='',city = '$ci_ty',city like '%%'),state LIKE '%%'
AND city LIKE '%%'), country LIKE '%%'
AND state LIKE '%%'
AND city LIKE '%%' )
AND age >= '$age_from'
AND age <= '$age_to'
AND
IF('$qualification' !='',qualification = '$qualification', qualification LIKE '%%' )
AND gender = '$look'
And status='1'";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result();
$data['count']=$query->num_rows();
$data['pages']=ceil($count/$per_page);
return $data;
}
this is view page
<form action="<?php echo base_url();?>searchresult/users" method="post">
<div id="tab1" class="tab_content" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Looking for</td>
<td>
<input type="radio" name="look" id="look" value="female" checked="checked"/> <span style="color:#000000;">Bride</span> <input type="radio" name="look" id="look" value="male" /><span style="color:#000000;"> Groom</span></td>
</tr>
<tr>
<td>Age</td>
<td> <span style="color:#000000;">From </span>
<select name="age_from" id="age" class="inp_age" >
<option>18</option>
<option>19</option>
<option selected>20</option>
<option>21</option>
<option>22</option>
</select><span style="color:#000000;"> To</span>
<select name="age_to" id="age" class="inp_age" >
<option>18</option>
<option>19</option>
<option >20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option selected="selected">70</option>
</select>
</td>
</tr>
<tr>
<td>Sect</td>
<td><select name="sect" id="sect" onchange="return SelectSect(this.value);" class="inp_search" >
<!-- <select name="cont" id="cont" onchange="return SelectState(this.value);" class="inp">-->
<option value="">Any sect</option>
<?php
$sect=mysql_query("select sect_id,sect_name from `geo_sect` order by sect_name asc");
while($sects=mysql_fetch_array($sect))
{
?>
<option value="<?php echo $sects['sect_id'];?>"><?php echo $sects['sect_name'];?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td>Sub Sect</td>
<td><select name="subsect" id="subsect" onchange="return SelectSub(this.value);" class="inp_search" >
<!--<select name="state" id="state" onchange="return SelectCity(this.value);" class="inp" >-->
<option value="">Any subsect</option>
</select></td>
</tr>
<tr>
<td>Country</td>
<td> <select name="country" id="country" onchange="return SelectState(this.value);" class="inp_search">
<option value="">Any Country</option>
<?php
$countries=mysql_query("select country_id,Country_name from `kk.geo_countries` order by Country_name asc");
while($country=mysql_fetch_array($countries))
{
?>
<option value="<?php echo $country['country_id'];?>" style="width:10px"><?php echo $country['Country_name'];?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td>State</td>
<td><select name="state" id="state" onchange="return SelectCity(this.value);" class="inp_search" >
<option value="">Any state</option>
</select></td>
</tr>
<tr>
<td>City</td>
<td> <select name="city" id="city" class="inp_search" >
<option value="">Any city</option>
</select></td>
</tr>
<tr>
<td>Qualification</td>
<td><select name="qualification" id="qualification" class="inp_search" >
<option value="">Any qualification</option>
<?php
$qualification=mysql_query("select qualification_id,qualification from `qualification` order by qualification asc");
while($qualifications=mysql_fetch_array($qualification))
{
?>
<option value="<?php echo $qualifications['qualification_id'];?>"><?php echo $qualifications['qualification'];?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td> </td>
<td> <div style="padding-left:100px;"> <input type="submit" name="submit" class="button green search_btn" value="Search" /></div></td>
</tr>
</table>
</div>
</form>
I am getting the following errors:
1)Severity:warning MISSING ARGUMENT 9 FOR searchresultss::login
2)severity:notice Undefined variable
unable to find where is the error please help me
add one parameter in the controller function
$data['showdata'] = $this->searchresultss-> login($per_page=3,$look,$age,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification);
and the the model should be like
model function login($per_page,$look,$age,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
You have missed the parameter in the controller while calling the function.Please let me know if you need anything.

MySQL Database - Transferring inputted data to a text file (PHP, HTML, SQL)

I just can't seem to figure out the logic to write the inputted information (contact form) to a text file within the localhost.
I am uploading all of this information on my school's server. If I please could get some help. Truly can't figure this part out.
HTML CODE:
<form action="Registered.php" method="post">
<p>
<label>First Name:</label>
<input name="fName" type="text">
</p>
<p>
<label>Last Name:</label>
<input name="lName" type="text">
</p>
<p>
<label>Address:</label>
<input name="address" type="text">
</p>
<p>
<label>State:</label>
<select name="statedropdown">
<option value="Al"> Al </option>
<option value="AK"> AK </option>
<option value="AS">AS</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
</p>
<p>
<label>ZIP Code:</label>
<input name="zip" required="required" placeholder="12345" type="text">
</p>
<p>
<label>Email:</label>
<input name="email" required="required" placeholder="fake#email.com" type="email">
</p>
<p>
<label>Phone Number:</label>
<input name="phone" required="required" placeholder="912-555-1234" type="text">
</p>
<p>
<input value="Submit" type="submit">
<input type="reset" value="Reset">
</p>
<p>
<td align="center"> View contacts in database </td>
</p>
<p>
<td align="center"> View contacts in file </td>
</p>
</body>
</html>
PHP CODE:
<html>
<head>
<title> Thank You </title>
</head>
<body>
<?php
$username="YYYYYYYY";
$password="XXXXXXXX";
$database="ZZZZZZZZ";
#declare variables
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address = $_POST['address'];
$statedropdown = $_POST['statedropdown'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$DOCUMENT_ROOT = $SERVER['DOCUMENT_ROOT'];
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM contacts";
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();
?>
<h1 align = "center"> Thanks for Registering! </h1> <br /><br />
<p align = "center"> Your information is: </p>
<table align = "center">
<tr>
<td> First Name: </td>
<td> &nbsp </td>
<td> <?php echo $fName ?> </td>
</tr>
<tr>
<td> Last Name: </td>
<td> &nbsp </td>
<td> <?php echo $lName ?> </td>
</tr>
<tr>
<td> Address: </td>
<td> &nbsp </td>
<td> <?php echo $address ?> </td>
</tr>
<tr>
<td> State: </td>
<td> &nbsp </td>
<td> <?php echo $statedropdown ?> </td>
</tr>
<tr>
<td> Zip: </td>
<td> &nbsp </td>
<td> <?php echo $zip ?> </td>
</tr>
<tr>
<td> Telephone: </td>
<td> &nbsp </td>
<td> <?php echo $phone ?> </td>
</tr>
<tr>
<td> E-mail: </td>
<td> &nbsp </td>
<td> <?php echo $email ?> </td>
</tr>
</table>
<?php
if($_POST['saveToFile'] == 'Save to File') {
$outputstring =
"First Name: $fName
Last Name: $lName
Address: $address
State: $statedropdown
Zip: $zip
Telephone: $phone
Email: $email
-----------------------\n";
$fp = fopen("FormData.txt", 'a');
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
}
?>
<p align="center"> Return to Main Page </p>
</body>
</html>
If someone could please help me out this would be highly appreciated!
Where do you get the variable $_POST['saveToFile'] from? Shouldn't there be something like a checkbox in the form?
Are there any error messages?
I would go like this:
$outputstring =
. "First Name: $fName \n"
. "Last Name: $lName \n"
. "Address: $address \n"
. "State: $statedropdown \n"
. "Zip: $zip \n"
. "Telephone: $phone \n"
. "Email: $email \n"
. "-----------------------\n";
file_put_contents("FormData.txt", $outputstring, FILE_APPEND | LOCK_EX);
The function file_put_contents opens and locks the file, appends the data to it, unlocks and finally closes it. So the only difference is the better overview.

need to display different value from dynamic dropdown list using php mysql

Hi i'm creating students marks entry list using php and mysql. Let say students subjects as "english,maths,social,science and computers" in database and Now i need to display these values from dropdown list as each row will need to show different value automatically.
<?php
$user = $_SESSION['loginUserId'];
$commonQuery = mysql_query("select sno from users where userId = '$user' and status='Active'");
$count = mysql_num_rows($commonQuery);
if($count == 1)
{
$commonQueryRes = mysql_fetch_array($commonQuery);
$uId=$commonQueryRes['sno'];
$subjectsCount = mysql_query("select COUNT(subjectName) as sub_count from subjects where userId='$uId' and status='Active' ");
$subjectsCountRes = mysql_fetch_array($subjectsCount);
$scount=$subjectsCount['sub_count'];
}
$bg = "light";
for($i=1;$i<=$scount;$i++)
{
if ($bg == "light") $bg = "dark";
else $bg = "light";
?>
<tr class="<?php echo $bg; ?>">
<td>
<select name="subName[]">
<option value="none">--select Subject--</option>
<?php
$subjectOptions = mysql_query("select subjectName from subjects where userId='$uId' and status='Active' ");
while($subjectOptionsRes = mysql_fetch_array($subjectOptions))
{
$sub = $subjectOptionsRes['subjectName'];
$sub_Sub_Options = mysql_query("select sno from subjects where userId='$uId' and subjectName='$sub'");
$sub_Sub_OptionsRes = mysql_fetch_array($sub_Sub_Options);
?>
<option value="<?php echo $sub_Sub_OptionsRes['sno']; ?>" selected="selected"><?php echo $subjectOptionsRes['subjectName']; ?></option>';
<?php
}
?>
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass"><?php echo "Pass";?></option>
<option value="fail"><?php echo "Fail";?></option>
</select>
</td>
<td><input type="text" name="quartely[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="halfearly[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="anually[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="rank[]" required size="5"/>(Kgs)</td>
<?php
}
?>
And Expected OUTPUT Should Be like this and It should be dynamic:
<table>
<thead>
<tr>
<th style="background: #5792C8;" rowspan="2">subject Name</th>
<th style="background: #5792C8;" rowspan="2">Result</th>
<th style="background: #5792C8;" colspan="4"> Progress </th>
</tr>
<tr>
<th>Quarterly</th>
<th>Halfearly</th>
<th>Annually</th>
<th>Rank</th>
</tr>
</thead>
<tbody>
<tr class="dark">
<td>
<select name="subjectName[]">
<option value="none">--select Subject--</option>
<option value="6" selected="selected">English</option>';
<option value="7" >Maths</option>';
<option value="8" >Science</option>';
<option value="9" >Social</option>';
<option value="10" >Computers</option>';
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
<td><input type="text" name="qua[]" required size="5"/></td>
<td><input type="text" name="half[]" required size="5"/></td>
<td><input type="text" name="anual[]" required size="5"/></td>
<td><input type="text" name="rank[]" required size="5"/></td>
<tr class="light">
<td>
<select name="subjectName[]">
<option value="none">--select Subject--</option>
<option value="6" >English</option>';
<option value="7" selected="selected" >Maths</option>';
<option value="8" >Science</option>';
<option value="9" >Social</option>';
<option value="10" >Computers</option>';
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
<td><input type="text" name="qua[]" required size="5"/></td>
<td><input type="text" name="half[]" required size="5"/></td>
<td><input type="text" name="anual[]" required size="5"/></td>
<td><input type="text" name="rank[]" required size="5"/></td>
<tr class="dark">
<td>
<select name="subjectName[]">
<option value="none">--select Subject--</option>
<option value="6" >English</option>';
<option value="7" >Maths</option>';
<option value="8" selected="selected" >Science</option>';
<option value="9" >Social</option>';
<option value="10" >Computers</option>';
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
<td><input type="text" name="qua[]" required size="5"/></td>
<td><input type="text" name="half[]" required size="5"/></td>
<td><input type="text" name="anual[]" required size="5"/></td>
<td><input type="text" name="rank[]" required size="5"/></td>
</tr>
</tbody>
</table>
I got solution,here my solution is:
I took $count=0 and i'm increasing $count+=1 for each row adding dynamically. And i took subjuct name in one array as $sub_array[] and doing if($sub_array[$count]==$subjectOptionsRes['subjectName']){echo 'selected="selected"';}
<?php
$user = $_SESSION['loginUserId'];
$commonQuery = mysql_query("select sno from users where userId = '$user' and status='Active'");
$count = mysql_num_rows($commonQuery);
if($count == 1)
{
$commonQueryRes = mysql_fetch_array($commonQuery);
$uId=$commonQueryRes['sno'];
$subjectsCount = mysql_query("select COUNT(subjectName) as sub_count from subjects where userId='$uId' and status='Active' ");
$subjectsCountRes = mysql_fetch_array($subjectsCount);
$scount=$subjectsCount['sub_count'];
}
$bg = "light";
$count=0;
for($i=1;$i<=$scount;$i++)
{
if ($bg == "light") $bg = "dark";
else $bg = "light";
?>
<tr class="<?php echo $bg; ?>">
<td>
<select name="subName[]">
<option value="none">--select Subject--</option>
<?php
$subjectOptions = mysql_query("select subjectName from subjects where userId='$uId' and status='Active' ");
while($subjectOptionsRes = mysql_fetch_array($subjectOptions))
{
$sub = $subjectOptionsRes['subjectName'];
$sub_array[] = $subjectOptionsRes['subjectName'];
$sub_Sub_Options = mysql_query("select sno from subjects where userId='$uId' and subjectName='$sub'");
$sub_Sub_OptionsRes = mysql_fetch_array($sub_Sub_Options);
?>
<option value="<?php echo $sub_Sub_OptionsRes['sno']; ?>" if($sub_array[$count]==$subjectOptionsRes['subjectName']){echo 'selected="selected"';}><?php echo $subjectOptionsRes['subjectName']; ?></option>';
<?php
}
?>
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass"><?php echo "Pass";?></option>
<option value="fail"><?php echo "Fail";?></option>
</select>
</td>
<td><input type="text" name="quartely[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="halfearly[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="anually[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="rank[]" required size="5"/>(Kgs)</td>
<?php
$count+=1;
}
?>

Why isn't the value from my dropbox being inserted into my database?

I have a script that I am writing and I have encounted a problem. I can not get the information from a dropbox to submit into a MySQL database.
Everything will show up in the database except for the item from the dropbox.
Here is my code:
<form action="work.php" method="post">
<table>
<tr>
<td>Email:</td>
<td><input type="text" name="Email" tabindex="1" />
</td>
</tr>
<tr>
<td>Messgae:</td>
<td><textarea name="Message"
rows = "7"
cols = "35"
tabindex = "2" /></textarea>
</td>
</tr>
<tr>
<Td>Time</td>
<td>
<select name="hour" tabindex="3">
<option value="1">1AM</option>
<option value="2">2AM</option>
<option value="3">3AM</option>
<option value="4">4AM</option>
<option value="5">5AM</option>
<option value="6">6AM</option>
<option value="7">7AM</option>
<option value="8">8AM</option>
<option value="9">9AM</option>
<option value="10">10AM</option>
<option value="11">11AM</option>
<option value="12">12PM</option>
<option value="13">1PM</option>
<option value="14">2PM</option>
<option value="15">3PM</option>
<option value="16">4PM</option>
<option value="17">5PM</option>
<option value="18">6PM</option>
<option value="19">7PM</option>
<option value="20">8PM</option>
<option value="21">9PM</option>
<option value="22">10PM</option>
<option value="23">11PM</option>
<option value="24">12AM</option>
</select><br>
<input type="submit" name="formSubmit" value="Submit" >
</form>
And then here is work.php
<?php
$time = $_POST['hour'];
$email1 = $_POST['Email'];
$message1 = $_POST['Message'];
mysql_connect ('localhost', 'myuser', 'mypassword') or die ('Error: ' .mysql_error());
mysql_select_db ("mydatabase");
$query="INSERT INTO Reminder (Email, Message, Time)
VALUES
('$email1','$message1','$time')";
mysql_query($query) or die ('Error submitting');
echo "You Chose: " .$time ;
?>

Categories