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
}
}
Related
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>ShipOnline System</title>
</head>
<body>
<h1>ShipOnline System Request Page</h1>
<form method="post" action="request.php">
<fieldset>
<fieldset>
<legend>Item Information:</legend>
<p><label for="description">Description:</label>
<input type="text" name= "description" id="description" maxlength="100" size="20" required="required"/></p>
<p><label for="weight">Weight (kg):</label>
<select name="weight" id="weight" required="required">
<option value="">Please Select</option>
<option value="weight">0-2</option>
<option value="weight">3</option>
<option value='weight'>4</option>
<option value='weight'>5</option>
<option value='weight'>6</option>
<option value='weight'>7</option>
<option value='weight'>8</option>
<option value='weight'>9</option>
<option value='weight'>10</option>
<option value='weight'>11</option>
<option value='weight'>12</option>
<option value='weight'>13</option>
<option value='weight'>14</option>
<option value='weight'>15</option>
<option value='weight'>16</option>
<option value='weight'>17</option>
<option value='weight'>18</option>
<option value='weight'>19</option>
<option value='weight'>20</option>
</select></p>
</fieldset>
<fieldset>
<legend>Pick-up Information:</legend>
<p><label for="pickupAddress">Street Address:</label>
<input type="text" name= "pickupAddress" id="pickupAddress" maxlength="40" size="30" required="required"/></p>
<p><label for="pickupSuburb">Suburb:</label>
<input type="text" name= "pickupSuburb" id="pickupSuburb" maxlength="40" size="20" required="required"/></p>
<p><label for="date">Preferred date:</label>
<select name="day" id="date" required="required">
<option value="">Day</option>
<option value="day">1</option>
<option value="day">2</option>
<option value="day">3</option>
<option value='day'>4</option>
<option value='day'>5</option>
<option value='day'>6</option>
<option value='day'>7</option>
<option value='day'>8</option>
<option value='day'>9</option>
<option value='day'>10</option>
<option value='day'>11</option>
<option value='day'>12</option>
<option value='day'>13</option>
<option value='day'>14</option>
<option value='day'>15</option>
<option value='day'>16</option>
<option value='day'>17</option>
<option value='day'>18</option>
<option value='day'>19</option>
<option value='day'>20</option>
<option value='day'>21</option>
<option value='day'>22</option>
<option value='day'>23</option>
<option value='day'>24</option>
<option value='day'>25</option>
<option value='day'>26</option>
<option value='day'>27</option>
<option value='day'>28</option>
<option value='day'>29</option>
<option value='day'>30</option>
<option value='day'>31</option>
</select>
<select name="month" id="date" required="required">
<option value="">Month</option>
<option value='month'>1</option>
<option value='month'>2</option>
<option value='month'>3</option>
<option value='month'>4</option>
<option value='month'>5</option>
<option value='month'>6</option>
<option value='month'>7</option>
<option value='month'>8</option>
<option value='month'>9</option>
<option value='month'>10</option>
<option value='month'>11</option>
<option value='month'>12</option>
</select>
<select name="year" id="date" required="required">
<option value="">Year</option>
<option value='year'>2018</option>
<option value='year'>2019</option>
<option value='year'>2020</option>
<option value='year'>2021</option>
</select></p>
<p><label for="time">Prefered time:</label>
<select name="time" id="time" required="required">
<option value="">Please Select</option>
<option value="time">7:30-8:30</option>
<option value="time">8:30-9:30</option>
<option value="time">9:30-10:30</option>
<option value="time">10:30-11:30</option>
<option value="time">11:30-12:30</option>
<option value="time">12:30-13:30</option>
<option value="time">13:30-14:30</option>
<option value="time">14:30-15:30</option>
<option value="time">15:30-16:30</option>
<option value="time">16:30-17:30</option>
<option value="time">17:30-18:30</option>
<option value="time">18:30-19:30</option>
<option value="time">19:30-20:30</option>
</select></p>
</fieldset>
<fieldset>
<legend>Delivery Information:</legend>
<p><label for="receiver">Receiver Name:</label>
<input type="text" name= "receiver" id="receiver" maxlength="40" size="20" pattern="^[a-zA-Z ]+$" required="required"/></p>
<p><label for="deliveryAddress">Street Address:</label>
<input type="text" name= "deliveryAddress" id="deliveryAddress" maxlength="40" size="30" required="required"/></p>
<p><label for="deliverySuburb">Suburb:</label>
<input type="text" name= "deliverySuburb" id="deliverySuburb" maxlength="40" size="20" required="required"/></p>
<p><label for="state">State:</label>
<select name="state" id="state" required="required">
<option value="">Please Select</option>
<option value="state">VIC</option>
<option value="state">NSW</option>
<option value="state">QLD</option>
<option value="state">NT</option>
<option value="state">WA</option>
<option value="state">SA</option>
<option value="state">TAS</option>
<option value="state">ACT</option>
</select></p>
</fieldset>
<br/>
<input type= "submit" value="Request"/>
</fieldset>
</form>
<p>Home</p>
</body>
</html>
PHP:
<?php
require_once ("settings.php"); //connection info
$conn = #mysqli_connect($host,
$user,
$pwd,
$sql_db
);
$requestID = trim($_POST["requestID"]);
$customerID = trim($_POST["customerID"]);
$requestDate = trim($_POST["requestDate"]);
$description = trim($_POST["description"]);
$weight = trim($_POST["weight"]);
$pickupAddress = trim($_POST["pickupAddress"]);
$pickupSuburb = trim($_POST["pickupSuburb"]);
$date = trim($_POST["date"]);
$time = trim($_POST["time"]);
$receiver = trim($_POST["receiver"]);
$deliveryAddress = trim($_POST["deliveryAddress"]);
$deliverySuburb = trim($_POST["deliverySuburb"]);
$state = trim($_POST["state"]);
$sql_table="Request";
$query = "insert into $sql_table (description, weight, pickupAddress, pickupSuburb, date, time, receiver, deliveryAddress, deliverySuburb, state) values ('$description', '$weight', '$pickupAddress', '$pickupSuburb', '$date', '$time', '$receiver', '$deliveryAddress', '$deliverySuburb', '$state')";
//execute the query -we should really check to see if the batabase exists first.
$result = mysqli_query($conn, $query);
//checks if the exeution was succcessful
if (!$result) {
echo "<p class=\"wrong\">Something is wrong with your request please go back and check your details</p>";
// would not show in a production script
} else {
// display an operation successful message
echo "<p class=\"ok\">Thank you! Your request number is requestID. The cost is <cost>. We will pick-up the item at $time on $date.</p>";
} // if successful query operation
// close the database connection
mysqli_close($conn);
// if successful database connection
?>
I have a similar PHP document to this one which works perfectly. I believe the that I keep getting the message "Something is wrong with your request please go back and check your details" because of the 'date', 'time' or the drop boxes which are 'weight' and 'state' but I tried removing them and nothing changed I kept getting that there was something wrong. I can't seem to figure out what is causing the form to not successfully send. Could someone please try and find the error?
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.
I am new in PHP. I have no experience on Object oriented PHP. I have experience only in Raw PHP.
I am trying to inserted the value of my drop down box to the database. This is my code given here.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT * INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)"
or die (mysql_error());
$result= mysql_query($sql);
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
<form method="post">
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
<br></br>
</form>
<form method="post">
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post" action="">
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<input type="submit" name="submit" value="submit">
</form>
<br><br>
</form>
</html>
I have gotten $_post['roll'],$_post['first_exam'] these error.
You had a lot of mistakes in your code, here is a full working example:
HTMl & PHP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Divix Help</title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "
INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)
";
//echo $sql;
$result = mysql_query($sql);
if ($result){
echo 'ok doen';
} else {
echo 'dont';
}
}
?>
<form method="post">
<table>
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
</table>
<br></br>
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
As mentioned by others you had too many <form> tags, remember that you only need 1 form tag in order to send multiple fields via POST or GET method.
You had mistakes in SQL (do not put INSERT * INTO, star sign goes only into SELECT statements)
Broken HTML structure, like missing </body>, </html> tags
And last but not least or die (mysql_error()); you put that against execute or connect function of mysql not against strings.
There is only a simple mistake there. Instead you made many , just make one form & add all select into it...
Something like this:
<form method="post">
code here
</form>
There are few things I would like to point out.
Your include statements should always be at the top of your php document, it's a good programming practice.
Use require_once instead of include because it prevents the file from being included more than once in your php document.
Create and use a Database class to perform all your database operations.
Always sanitize your data using real_escape_string function before inserting to prevent SQL injection.
Always close your database connection at the end, it's a good programming practice.
And last but not the least, learn HTML and PHP properly.
Here's the complete code,
<?php
// instead of include, use require_once
require_once('db.php');
// create and use Database class for all your database operations
class Database{
public static function open_connection($host, $username, $password, $dbName){
return new mysqli($host, $username, $password, $dbName);
}
public static function check_connection(){
if(!mysqli_connect_error()){
return true;
}else{
return false;
}
}
public static function execute_query($connection,$query){
return $connection->query($query);
}
public static function close_connection($connection){
$connection->close();
}
}
?>
<?php
if (isset($_POST['submit'])){
$connection = Database::open_connection(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if(Database::check_connection()){
// sanitize your input data
$roll = $connection->real_escape_string($_POST['roll']);
$fifth_exam = $connection->real_escape_string($_POST['first_exam']);
$second_exam = $connection->real_escape_string($_POST['second_exam']);
$third_exam = $connection->real_escape_string($_POST['third_exam']);
$fourth_exam = $connection->real_escape_string($_POST['fourth_exam']);
$fifth_exam = $connection->real_escape_string($_POST['fifth_exam']);
// now insert into database
// instead of taking id as NULL, take id as AUTO_INCREMENT
$query = "INSERT INTO table(roll_no, first_exam, second_exam, third_exam, fourth_exam, fifth_exam) VALUES ($roll, '$first_exam', '$second_exam', '$third_exam', '$fourth_exam', '$fifth_exam')";
if(Database::execute_query($connection,$query)){
echo "record is successfully inserted";
}else{
echo "error: record could not be inserted";
}
}else{
echo "database connection failed";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Some Title</title>
</head>
<body>
<form action="test.php" method="post">
<table>
<tr>
<td>Roll No.</td>
<td><input type="text" name="roll" /></td>
</tr>
<tr>
<td>First exam</td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Second exam</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Third exam</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fourth exam</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fifth exam</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
// it's a good programming practice to always close database connection at the end
if(isset($connection)){
Database::close_connection($connection);
}
?>
Try this. Make 1 Form.
Change this too.
$sql= "INSERT INTO roll (Roll,1st,2nd,3rd,4th,5th) VALUES ($roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
This is also one problem. Use or die (mysql_error()); in mysql functions, not in statement.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form method="post">
<table>
<tr>
<th>Roll:</th>
<th>First Exam</th>
<th>Second Exam</th>
<th>Third Exam</th>
<th>Fourth Exam</th>
<th>Fifth Exam</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="roll" /></td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td><input type='submit' name='submit' value='submit'></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th) VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
</html>
Screenhot
I have one simple problem with mysql-db. I want to pass 3 values in one column of my db that is called DateOfBirth.
I have made 2 selectors, day and month, and one text are for year (yer).
I want to combine these values to php variables and pass it to mysql column that is VARCHAR.
Dose any one has solution for this?
<?php
session_start();
if(isset($_SESSION['user'])!="")
{
header("Location: home.php");
}
include_once 'dbconnect.php';
if(isset($_POST['btn-signup']))
{
$dob = mysql_real_escape_string($_POST['day'.'month'.'yer']);
if(mysql_query("INSERT INTO userprofile(gender,dateofbirth) VALUES('$dob')"))
{
?>
<script>alert('successfully registered ');</script>
<?php
}
else
{
?>
<script>alert('error while registering you...');</script>
<?php
}
}
?>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<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>
<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>
Change line
$dob = mysql_real_escape_string($_POST['day'.'month'.'yer']);
To
$y = $_POST['yer'];
$m = $_POST['month'];
$d = $_POST['day'];
$dob = strtotime($y.'-'.$m.'-'.$a);
EDIT: If you're inserting into the gender row, you need to supply a value for gender, otherwise it's going to either throw an error or go ahead with the query inserting dob into the gender row and then throw an error
I have to re-explain my situation they said.
my html form:
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="carform.css"/>
<head>
</head>
<body>
<form action="connection.php" method="post" class="smart-green">
<h1> Car listing
<span>Please fill all the text in the fields.</span>
</h1>
<label>
<span>Make:</span>
<select id="category">
<option value="Alfa Romeo">Alfa Romeo</option>
<option value="Aston Martin">Aston Martin</option>
<option value="Audi">Audi</option>
<option value="Austin">Austin</option>
<option value="Bentley">Bentley</option>
<option value="BMW">BMW</option>
<option value="Buddy">Buddy</option>
<option value="Buick">Buick</option>
<option value="Cadillac">Cadillac</option>
<option value="Chevrolet">Chevrolet</option>
<option value="Chrysler">Chrysler</option>
<option value="Citroen">Citroen</option>
<option value="Dacia">Dacia</option>
<option value="Daewoo">Daewoo</option>
<option value="Daihatsu">Daihatsu</option>
<option value="Dodge">Dodge</option>
<option value="Ferrari">Ferrari</option>
<option value="Fiat">Fiat</option>
<option value="Fisker">Fisker</option>
<option value="Ford">Ford</option>
<option value="GMC">GMC</option>
<option value="Honda">Honda</option>
<option value="Hummer">Hummer</option>
<option value="Hyundai">Hyundai</option>
<option value="Infiniti">Infiniti</option>
<option value="Isuzu">Isuzu</option>
<option value="Iveco">Iveco</option>
<option value="Jaguar">Jaguar</option>
<option value="Jeep">Jeep</option>
<option value="Kewet">Kewet</option>
<option value="Kia">Kia</option>
<option value="Koenigsegg">Koenigsegg</option>
<option value="Lada">Lada</option>
<option value="Lamborghini">Lamborghini</option>
<option value="Lancia">Lancia</option>
<option value="Land Rover">Land Rover</option>
<option value="Lexus">Lexus</option>
<option value="Lincoln">Lincoln</option>
<option value="Lotus">Lotus</option>
<option value="Maserati">Maserati</option>
<option value="Maybach">Maybach</option>
<option value="Mazda">Mazda</option>
<option value="McLaren">McLaren</option>
<option value="Mercedes-Benz">Mercedes-Benz</option>
<option value="Mercury">Mercury</option>
<option value="MG">MG</option>
<option value="MINI">MINI</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Morgan">Morgan</option>
<option value="Morris">Morris</option>
<option value="Nissan">Nissan</option>
<option value="Oldsmobile">Oldsmobile</option>
<option value="Opel">Opel</option>
<option value="Peugeot">Peugeot</option>
<option value="Piaggio">Piaggio</option>
<option value="Plymouth">Plymouth</option>
<option value="Pontiac">Pontiac</option>
<option value="Porsche">Porsche</option>
<option value="Renault">Renault</option>
<option value="Reva">Reva</option>
<option value="Rolls Royce">Rolls Royce</option>
<option value="Rover">Rover</option>
<option value="Saab">Saab</option>
<option value="Seat">Seat</option>
<option value="Skoda">Skoda</option>
<option value="Smart">Smart</option>
<option value="Ssangyong">Ssangyong</option>
<option value="Subaru">Subaru</option>
<option value="Suzuki">Suzuki</option>
<option value="Tazzari">Tazzari</option>
<option value="Tesla">Tesla</option>
<option value="Think">Think</option>
<option value="Toyota">Toyota</option>
<option value="Triumph">Triumph</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Volvo">Volvo</option>
<option value="Others">Others</option>
</select>
</label>
<label>
<span>Title:</span>
<input id="title" type="text" name="title">
</label>
<label>
<span>Price:</span>
<input id="price" type="text" name="price">$
</label> <label>
<span>Description:</span>
<textarea id="description" name="description" rows="20" cols="60"></textarea>
</label>
<label>
<span> </span>
<input type="button" class="button" value="Send"/>
</label>
</form>
</body>
</html>
and my php:
<?php
$con = mysqli_connect("", "", "", "");
if (mysqli_connect_errno()) {
echo "Failed to connect to mysqli: ".mysqli_connect_error();
}
$cat = mysqli_real_escape_string($con, $_POST['category']);
$tit = mysqli_real_escape_string($con, $_POST['title']);
$pri = mysqli_real_escape_string($con, $_POST['price']);
$des = mysqli_real_escape_string($con, $_POST['description']);
$sql = "INSERT INTO forsale (Category, Title, Price, Description) VALUES ('$cat', '$tit', '$pri', '$des')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
echo "1 record added";
mysqli_close($con);
?>
Now what I am missing is having uploading maximum of 12 images for this form per Id and being able to retrieve all the images that belongs to eash posting
your code if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
you are missing a closing curly brace like this, not sure if this is your onle problem but it certainly is a problem.
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));}