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.
Related
Hello freinds i am unable to print in newline after clicking submit form i am getting all the data in a same row even I have used "\r\n", here is my MWE for html coding
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form name="StudentRegistration" action="proces.php" method="post">
<table cellpadding="2" width="20%" bgcolor="99FFFF" align="center" cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername" size="30"></td>
</tr>
<tr>
<td>Mother Name</td>
<td><input type="text" name="mothername" id="mothername" size="30"></td>
</tr>
<tr>
<td>Institute</td>
<td><select name="institute">
<option value=" " selected>Select..</option>
<option value="Institute-1">Institute-1</option>
<option value="Institute-2">Institute-2</option>
</select></td>
</tr>
<td>Category</td>
<td><select name="category">
<option value=" " selected>Select..</option>
<option value="Regular">Regular</option>
<option value="Private">Private</option>
</select></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value="Male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<td>Stream</td>
<td><select name="stream">
<option value=" " selected>Select..</option>
<option value="ARTS">ARTS</option>
<option value="COMMERCE">COMMERCE</option>
<option value="SCIENCE">SCIENCE</option>
</select></td>
</tr>
<tr>
<td>Core-1</td>
<td><select name="core-1">
<option value=" " selected>Select..</option>
<option value="ENGL">English</option>
</select></td>
</tr>
<tr>
<td>Core-2</td>
<td><select name="core-2">
<option value=" " selected>Select..</option>
<option value="ALTE">Alternative English</option>
</select></td>
</tr>
<tr>
<td>Elective-1</td>
<td><select name="elective-1">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-2</td>
<td><select name="elective-2">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-3</td>
<td><select name="elective-3">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-4</td>
<td><select name="elective-4">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Compulsory</td>
<td><select name="compulsory">
<option value=" " selected>Select..</option>
<option value="ENVE">Environmental Education</option>
</select></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
Here is MWE for php for exporting data to .csv file to save data, I want for every each Submit Form i want to print data in newlines
<?php
$fieldAA = $_POST[" "];
$fieldA = $_POST["textnames"];
$fieldB = $_POST["fathername"];
$fieldC = $_POST["mothername"];
$fieldD = $_POST["institute"];
$fieldE = $_POST["category"];
$fieldF = $_POST["sex"];
$fieldG = $_POST["stream"];
$fieldH = $_POST["core-1"];
$fieldI = $_POST["core-2"];
$fieldJ = $_POST["elective-1"];
$fieldK = $_POST["elective-2"];
$fieldL = $_POST["elective-3"];
$fieldM = $_POST["elective-4"];
$fieldN = $_POST["compulsory"];
$keys = array($fieldAA,$fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,'','',$fieldI,'','',$fieldJ,'','',$fieldK,'','',$fieldL,'','',$fieldM,'','',$fieldN,''); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = $keys;
foreach( $keys as $key ){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'data.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line ;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line ;
fwrite($fcon,$csv_line);
fclose($fcon);
echo "Successfully done! $keys[1]"
?>
In php we can use the PHP_EOL to echo out the end of a line, using this method will not rely on the current document(s)/platforms line endings.
Example:-
$string = "I want a new line here" . PHP_EOL . "This is my new line";
Line breaks in csvs are dependent on what you use to view them also.
Also you probably want to explain what MWE means in your question.
I've seen some people have the same issue, but different scenarios. And searching just gives me 'how to use joins'.
When I execute this through my php page, two records are inserted. One with the content, and one with just the date. I can't seem to find where the date field is being executed. I've had this issue in the past, but with a past version of mySQL and with depreciated values, so using the new values is someone newer to me.
Below is my php and sql statement:
<?php
$link = mysqli_connect('server', 'username', 'password');
if (!$link) {
die('Could Not Connect: ' . mysqli_error());
}
else echo 'Connected Succesffully';
mysqli_select_db($link, 'database');
mysqli_query($link, "INSERT INTO tblcc
(date , production , productionco , city , state ,
genre , type , details , part1 , gender1 ,
char1 , part2 , gender2 , char2 , part3 ,
gender3 , char3 , part4 , gender4 , char4 , part5 ,
gender5 , char5)
VALUES(now() , '$_POST[production]' , '$_POST[productionco]' , '$_POST[city]' , '$_POST[state]' ,
'$_POST[genre]' , '$_POST[type]' , '$_POST[details]' , '$_POST[part1]' , '$_POST[gender1]' ,
'$_POST[char1]' , '$_POST[part2]' , '$_POST[gender2]' , '$_POST[char2]' , '$_POST[part3]' ,
'$_POST[gender3]' , '$_POST[char3]' , '$_POST[part4]' , '$_POST[gender4]' , '$_POST[char4]' ,
'$_POST[part5]' , '$_POST[gender5]', $_POST[char5]')");
mysqli_close($link);
?>
The HTML:
<form action="http://enhstudios.com/bc1/submitcc.php/" method="post">
<table border="3" cellpadding="3" cellspacing="3">
<tr>
<th>Production:</th>
<th>Production Company:</th>
<th colspan="2">Location:</th>
</tr>
<tr>
<td><input type="text" name="production"></td>
<td><input type="text" name="productionco"></td>
<td><input type="text" name="city"></td>
<td><select name="state">
<option value=""></option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kentucky">Kentucky</option>
<option value="Missouri">Missouri</option>
<option value="Ohio">Ohio</option>
<option value="Wisconsin">Wisconsin</option>
</select>
</td>
</tr>
<tr>
<th>Genre:</th>
<th>Type:</th>
<th colspan="2">Details:</th>
</tr>
<tr>
<td><select name="genre">
<option value=""></option>
<option value="Action">Action</option>
<option value="Comedy">Comedy</option>
<option value="Drama">Drama</option>
<option value="Horror">Horror</option>
<option value="Romance">Romance</option>
</select>
</td>
<td><select name="type">
<option value=""></option>
<option value="Feature Length">Feature Length</option>
<option value="Mini-Series">Mini-Series</option>
<option value="Short">Short</option>
<option value="Web Series">Web Series</option>
</select>
</td>
<td colspan="2"><textarea maxlength="255" rows="5" cols="30" name="details"></textarea></td>
</tr>
<tr>
<th colspan="4">Parts:</th>
</tr>
<tr>
<th>Role Type:</th>
<th>Gender:</th>
<th colspan="2">Character:</th>
</tr>
<tr>
<td><select name="part1">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender1">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char1">
</td>
</tr>
<tr>
<td><select name="part2">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender2">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char2">
</td>
</tr>
<tr>
<td><select name="part3">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender3">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char3">
</td>
</tr>
<tr>
<td><select name="part4">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender4">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char4">
</td>
</tr>
<tr>
<td><select name="part5">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender5">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char5">
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td colspan="3"></td>
</tr>
</table>
</form>
I found the "issue". I nestled the form inside the table instead of the table inside the form.
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.
I have simple for you problem... :-)
I want to insert data into Database that has 2 tables one is users and second is userprofile
here is my db:
As you see i have foreign-key users->id with userprofile->userid
the problem is that when i want to make registration of new user, i can't....
but when i don't have foreign-key then its all ok.
but i need registration with foreign-key her is the register.php code hope you can hell me :-)
and pleas show me the examples of code were i am making the mistake.
<?php
session_start();
if (isset($_SESSION['user']) != "") {
header("Location: home.php");
}
include_once 'dbconnect.php';
if (isset($_POST['btn-signup'])) {
$y = $_POST['yer'];
$m = $_POST['month'];
$d = $_POST['day'];
$dob = $d.'-'.$m.'-'.$y;
$uname = mysql_real_escape_string($_POST['uname']);
$upass = md5(mysql_real_escape_string($_POST['pass']));
$email = mysql_real_escape_string($_POST['email']);
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$country = mysql_real_escape_string($_POST['country']);
$height = mysql_real_escape_string($_POST['height']);
$hair_color = mysql_real_escape_string($_POST['hair_color']);
$eye_color = mysql_real_escape_string($_POST['eye_color']);
$Body_type = mysql_real_escape_string($_POST['Body_type']);
$gend = mysql_real_escape_string($_POST['gend']);
if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')") &&
mysql_query("INSERT INTO userprofile(gender, dateofbirth, firstname, Lastname, height, eyecolor, haircolor, bodytype, country_town, email) VALUES ('$gend','$dob' ,'$fname','$lname','$height','$eye_color','$hair_color','$Body_type','$country','$email')")) {
?>
<script>alert('successfully registered '); </script>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php"); ?>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php");
}
else {
?>
<script>alert('error while registering you...');</script>
<?php
}
}
?>
<!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=utf-8" />
<title>Login & Registration System</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td>
<select name="gend">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select></td>
</tr>
<tr>
<td>
<select name="day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input class="yer" type="text" name="yer" placeholder=" yer" required />
</td>
</tr>
<tr>
<td><input type="text" name="uname" placeholder="User Name" required /></td>
</tr>
<tr>
<td><input type="text" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><input type="text" name="fname" placeholder="Your firstname" required /></td>
</tr>
<tr>
<td><input type="text" name="lname" placeholder="Your Lastname" required /></td>
</tr>
<tr>
<tr>
<td>
<select name="country">
<option value="Greece-Athens">Greece-Athens</option>
<option value="Italy-Rome">Italy-Rome</option>
<option value="France-Paris">France-Paris</option>
</select>
<select name="hair_color">
<option value="Black">Black</option>
<option value="Blonde">Blonde</option>
<option value="Dark-Brown">Dark Brown</option>
</select>
<select name="eye_color">
<option value="Black">Black</option>
<option value="Blue">Blue</option>
<option value="Green">Green</option>
<option value="Brown">Brown</option>
<option value="Hazel">Hazel</option>
<option value="Gray">Gray</option>
</select>
<select name="Body_type">
<option value="Slim">Slim</option>
<option value="Athletic">Athletic</option>
<option value="Average">Average</option>
<option value="Overweight">Overweight</option>
</select>
<select name="height">
<option value="152">152</option>
<option value="154">154</option>
<option value="156">156</option>
<option value="158">158</option>
<option value="160">160</option>
<option value="162">162</option>
<option value="164">164</option>
<option value="166">166</option>
<option value="168">168</option>
<option value="170">170</option>
<option value="172">172</option>
<option value="172">172</option>
<option value="174">174</option>
<option value="176">176</option>
<option value="178">178</option>
<option value="180">180</option>
<option value="182">182</option>
<option value="184">184</option>
<option value="186">186</option>
<option value="188">188</option>
<option value="190">190</option>
<option value="192">192</option>
<option value="192">192</option>
<option value="194">194</option>
<option value="196">196</option>
</select>
</td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
<tr>
<td>Sign In Here</td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
Try this instead:
if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')") &&
mysql_query("INSERT INTO userprofile(
gender,
dateofbirth,
firstname,
Lastname,
height,
eyecolor,
haircolor,
bodytype,
country_town,
email,
userid)
VALUES (
'$gend',
'$dob',
'$fname',
'$lname',
'$height',
'$eye_color',
'$hair_color',
'$Body_type',
'$country',
'$email',
last_insert_id()
)")) {
Noticed the "last_insert_id()" value and the "userid" column name added.
i recommend you just use one table to store information about the user, password and all.
eg:
$sql = "insert into user (username,email,password,acctype) values ('$uname','$email','$password_hash','$acctype')";
$result = mysqli_query($conn,$sql);
then when a user does certian things you can track that using the user id. It means for every action, you need to post the user id as foreign key in that table.
$sql = "select * from bookings WHERE uid ='".$_SESSION['user_id']."' order by id desc limit 15";
$result = mysqli_query($conn,$sql);
then you can select based on session or uid...
OK i find the way
I modified the code like this:
if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')")) {
//$sql="select id from users";
$test=mysql_result(mysql_query("SELECT id FROM users LIMIT 1"),0);
if (mysql_query("INSERT INTO userprofile(userid,gender, dateofbirth, firstname, Lastname, height, eyecolor, haircolor, bodytype, country_town, email) VALUES ('$test','$gend','$dob' ,'$fname','$lname','$height','$eye_color','$hair_color','$Body_type','$country','$email')"))
{
?>
<script>alert('successfully registered '); </script>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php"); ?>
<?php
$_SESSION['user'] = $row['id'];
header("Location: Index.php");
} else {
?>
<script>alert('error while registering you...');</script>
<?php
}
}
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 ;
?>