<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/
jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
</script>
<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to)
VALUES
('".$driver_name[$k]."','$s','".$from[$k]."','".$to[$k]."')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"NOT".mysqli_error($conn);
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\"
value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."
</option>";
}
echo "</select></td>";
echo "<td>".'<input type="text" name="date_from[]" class="datepicker">'."</td>";
echo "<td>".'<input type="text" name="date_to[]" class="datepicker">'."</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
</body>
</html>
I want to store date to database from jquery datepicker. above is my code and error is Warning: strtotime() expects parameter 1 to be string, array given. Can i store date to database as array.Plz help.
Change Input name to :-
echo "<td>".'<input type="text" name="date_from" class="datepicker">'."</td>"; // remove []
echo "<td>".'<input type="text" name="date_to" class="datepicker">'."</td>"; // remove []
Use Dateformat like this
Try: date("Y-m-d") which uses the numeric equivalents.
#vove This is code-
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/
jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
</script>
<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
$ins = "insert into driver_status(driver_name,status,date_from,date_to)
VALUES
('$driver_name','$s','$from','$to')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"NOT".mysqli_error($conn);
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name\"
value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."
</option>";
}
echo "</select></td>";
echo "<td>".'<input type="text" name="date_from" class="datepicker">'."</td>";
echo "<td>".'<input type="text" name="date_to" class="datepicker">'."</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
Related
In my university database project I have an auto increment field roll number in my SQL. What I want is that when new admission take place and a student record is inserted it displays all finds on same page including roll number. However despite my best efforts all it returns 0 in roll number.
Here is the code:
<?php
$con = mysqli_connect("localhost", "root", "") or die("conection error");
mysqli_select_db($con, "hamdard university") or die("dbase error");
if (isset($_POST['subbtn'])) {
$r = "SELECT RollNo FROM admission_form";
$result = mysqli_query($con, $r);
if (mysqli_query($con, $r)) {
$last_id = mysqli_insert_id($con);
}
$n = $_POST['txtname'];
$f = $_POST['txtfac'];
$s = $_POST['txtsem'];
$sql = "insert into admission_form(name,faculty,semester)values ('$n','$f','$s')";
mysqli_query($con, $sql);
echo "<table border=1>
<th>RollNo</th>
<th>Name</th>
<th>Faculty</th>
<th>Semester</th>";
echo "<tr>";
echo "<td>";
echo $last_id;
echo "</td>";
echo "<td>";
echo $n;
echo "</td>";
echo "<td>";
echo $f;
echo "</td>";
echo "<td>";
echo $s;
echo "</td>";
echo "<br>";
}
?>
<html>
<head></head>
<body>
<form name="f1" action="" method="POST">
RollNo:
<input type="text" name="txtroll" readonly> Name:
<input type="text" name="txtname"> Faculty:
<input type="text" name="txtfac"> Semester:
<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>
</html>
You need get $last_id after INSERT query
$sql="insert into admission_form(name,faculty,semester)values ...
mysqli_query($con,$sql);
$last_id = mysqli_insert_id($con);
You have misplaced insert query. Change it as:
<?php
$con=mysqli_connect("localhost","root","")or die("conection error");
mysqli_select_db($con,"hamdard university")or die("dbase error");
if(isset($_POST['subbtn']))
{
$sql="insert into admission_form(name,faculty,semester)values ('$n','$f','$s')";
if (mysqli_query($con, $sql))
{
$last_id = mysqli_insert_id($con);
}
$n=$_POST['txtname'];
$f=$_POST['txtfac'];
$s=$_POST['txtsem'];
$r="SELECT RollNo FROM admission_form";
$result=mysqli_query($con, $r);
echo "<table border=1>
<th>RollNo</th>
<th>Name</th>
<th>Faculty</th>
<th>Semester</th>";
echo "<tr>";
echo "<td>";
echo $last_id;
echo "</td>";
echo "<td>";
echo $n;
echo "</td>";
echo "<td>";
echo $f;
echo "</td>";
echo "<td>";
echo $s;
echo "</td>";
echo "<br>";
}
?>
<html>
<head></head>
<body>
<form name="f1" action="" method="POST">
RollNo:<input type="text" name="txtroll" readonly>
Name:<input type="text" name="txtname">
Faculty:<input type="text" name="txtfac">
Semester:<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>
</html>
You have some problems in this page.
As stated before, the INSER query must be before the select.
If you do "SELECT RollNo FROM admission_form" you will get ALL existing RollNo in admission_form and not the lastest one. If this is an int with auto increment (that seams to be) you should do "SELECT max(RollNo) FROM admission_form".
You don't need to do the above select, as mysqli_insert_id get the ID inserted in the last query.
You might not see the result in your table because it is on HTML head, but it should be in the body of the page.
The table you create is not "closed".
You should be good to go with the code bellow.
<HTML>
<HEAD></HEAD>
<BODY>
<?php
$con=mysqli_connect("localhost","root","")or die("conection error");
mysqli_select_db($con,"hamdard university")or die("dbase error");
if(isset($_POST['subbtn']))
{
$n=$_POST['txtname'];
$f=$_POST['txtfac'];
$s=$_POST['txtsem'];
$sql="insert into admission_form(name,faculty,semester) values ('$n','$f','$s')";
if (mysqli_query($con, $r))
{
$last_id = mysqli_insert_id($con);
}
echo "<table border=\"1\"><th>RollNo</th> <th>Name</th> <th>Faculty</th> <th>Semester</th>";
echo "<tr>";
echo "<td>";
echo $last_id;
echo "</td>";
echo "<td>";
echo $n;
echo "</td>";
echo "<td>";
echo $f;
echo "</td>";
echo "<td>";
echo $s;
echo "</td>";
echo "</tr></table>";
}
?>
<form name="f1" action="" method="POST">
RollNo:<input type="text" name="txtroll" readonly>
Name:<input type="text" name="txtname">
Faculty:<input type="text" name="txtfac">
Semester:<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>
</html>
Try this:
$mysqli = new mysqli(SQLI_SERVER, MYSQLI_USER, MYSQLI_PWD, MYSQLI_DBNAME);
if ($result = $mysqli->query("INSERT INTO admission_form(name, facility,semester) VALUES..) {
echo 'The ID is: '.$mysqli->insert_id;
}
<?php
$status=&$_POST['status'];
$from=&$_POST['date_from'];
$to=&$_POST['date_to'];
$conn=mysqli_connect('localhost','root','','punbus') or die("Database not connected".mysqli_error());
if(isset($_POST['sub'])){
$ins="insert into driver_status(driver_name,status,date_from,date_to)
select Driver_name,'$status','$from','$to' from driver_master";
if(mysqli_query($conn,$ins)){
echo "added";
}
else{
echo "NOT".mysqli_error($conn);
}
}
$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['Driver_name']."</td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
?>
<td>
<select name="status">
<?php
while($row1=mysqli_fetch_assoc($query1)){
$st=$row1['d_status'];
echo "<option value='$st'>$st</option>";
}
?>
</select>
</td>
<?php
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
That is my code. I want to save option selected from 4 drop down list to mysql. When I submit the form, the value selected from last box are getting saved in all rows of mysql table. Now, please tell me what should I do?
I am getting drop down box values from database table properly so what is the problem?
this is your code.
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from = $_POST['date_from'];
$to = $_POST['date_to'];
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
if (mysqli_query($conn, $ins)) {
echo "added";
} else {
echo "NOT" . mysqli_error($conn);
}
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td></tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
this is similar to other textbox
$op="select * from client where active=0";
$op1=mysql_query($op);
echo '<select name="c" id="c" style="width:160px;" required>';
while ($row = mysql_fetch_array($op1))
{
echo $s=$row["c_name"];
if($company!=$s)
{
echo '<option value="'.$row["c_name"].'">'.$row["c_name"].'</option>';
}
}
$c=$_REQUEST['c'];
$sql="insert into project c_name) values('$c')";
$sql1=mysql_query($sql);
I currently have these PHP pages which lets me add a record to a database. (in this case its members) It works perfectly in the sense that I can ADD, DELETE and VIEW. But Im not sure how to get the edit(or UPDATE functionality working.
Here is my db connection Code:
<?php
// Server Info
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'gamgam';
// Connect to database
$connection = new mysqli($server, $username, $password, $database);
?>
Here is my Add Code:
<!DOCTYPE html>
<html>
<head><title>Insert Users</title></head>
<body>
<h2>Insert User Confirmation</h2>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br>
<?php
require_once('connection.php');
echo "<label for='memberID' >Member ID:</label>";
echo "<input type='text' name='memberID' id='memberID' />";
echo "<br /><br />";
echo "<label for='username' >Username:</label>";
echo "<input type='text' name='username' id='username' />";
echo "<br /><br />";
echo "<label for='password' >Password:</label>";
echo "<input type='password' name='password' id='password' />";
echo "<br /><br />";
echo "<label for='fName' >Firstname:</label>";
echo "<input type='text' name='fName' id='fName' />";
echo "<br /><br />";
echo "<label for='lName' >Lastname:</label>";
echo "<input type='text' name='lName' id='lName' />";
echo "<br /><br />";
echo "<label for='address' >Address:</label>";
echo "<input type='text' name='address' id='address' />";
echo "<br /><br />";
echo "<label for='email' >Email:</label>";
echo "<input type='text' name='email' id='email' />";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "<input type='reset' value='Clear' />";
echo "<br /><br />";
?>
</form>
</section>
<p><a href='login.php'>Login</a></p>
<?php
if(!isset($_POST['submit'])) {
echo 'Please Register';
} else {
$memberID = $_POST['memberID'];
$username = $_POST['username'];
$password = $_POST['password'];
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address = $_POST['address'];
$email = $_POST['email'];
$query = "INSERT INTO `members`
(MemberID, Username, Password, FirstName, LastName,
StreetAddress, Email)
VALUES ('$memberID', '$username', '$password', '$fName',
'$lName', '$address', '$email')";
mysqli_query($connection, $query)
or die(mysqli_error($connection));
$rc = mysqli_affected_rows($connection);
if ($rc==1)
{
echo '<h4>The database has been updated with the following details: </h4> ';
echo 'MemberID: '.$memberID.'<br />';
echo 'Username: '.$username.'<br />';
echo 'Password: '.$password.'<br />';
echo 'First Name: '.$fName.'<br />';
echo 'Last Name: '.$lName.'<br />';
echo 'Address: '.$address.'<br />';
echo 'Email: '.$email.'<br />';
} else {
echo '<p>The data was not entered into the database this time.</p>';
}
}
?>
</body>
</html>
Here is my View Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border="1" style="width:100%" >
<?php
/*
VIEW.PHP
Displays all data from 'players' table
*/
// connect to the database
include('connection.php');
// get results from database
$result = mysqli_query($connection, "SELECT * FROM members")
or die(mysqli_error());
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['MemberID'] . '</td>';
echo '<td>' . $row['Username'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['FirstName'] . '</td>';
echo '<td>' . $row['StreetAddress'] . '</td>';
echo '<td>' . $row['Email'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
And here is the Delete Code:
<?php
// Connect to the database
include('connection.php');
// Confirm that the 'code' variable has been set
if (isset($_GET['MemberID']))
{
// Get the 'MemberID' variable from the URL
$MemberID = $_GET['MemberID'];
// Delete record from database
if ($stmt = $connection->prepare("DELETE FROM members WHERE MemberID = ? LIMIT 1")) {
$stmt->bind_param("i",$MemberID);
$stmt->execute();
$stmt->close();
} else {
echo "ERROR: could not prepare SQL statement.";
}
$connection->close();
// Redirect user after delete is successful
header("Location: view.php");
} else {
// If the 'code' variable isn't set, redirect the user
header("Location: view.php");
}
?>
I have gone through many basic php form templates online trying to incorporate what they have done to achieve results but have not had any success. What code needs to be written for my website to have the functionality to edit records already created in the database without going through phpmyadmin. Any help is apreciated.
Edit will be just just like Add, but you need to read the record first and populate the field values.
Start with the code from add and do something like:
<?php $MemberID = (int) $_GET['MemberID']; ?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br>
<input type="hidden" name="MemberID" value="<?php echo $MemberID; ?>"
<?php
require_once('connection.php');
$result = mysqli_query($connection, "SELECT * FROM members where MemberID = $MemberID") or die(mysqli_error());
// loop through results of database query, displaying them in the table
$row = mysqli_fetch_assoc($result);
extract($row);
echo "<label for='memberID' >Member ID:</label>";
echo "$memberID"; // member ID should not be editable
echo "<br /><br />";
echo "<label for='username' >Username:</label>";
echo "<input type='text' name='username' id='username' value="$username" />";
echo "<br /><br />";
The PHP code will have a query like
`UPDATE `members` SET `username` = '$username' ... WHERE `MemberID` = '$MemberID'"
I am required to create a drop down box which gathers the records from a database to populate the drop down. When one of the values is selected, a form is to be displayed which contains the data relating to the value selected.
The form is to have the function of showing the selected data, but also to update the record in the database when filled out and Submit.
I have created a php file to try and accomplish this, but Im getting errors such as undefined index and unknown column.
I am only new to PHP so this is a big task for me. Could someone have a look at my code and inform me if there are any errors.
Ive been trying to piece code together here and there from the net but its been tricky trying to get it all to work.
I am not getting any errors now after some tweaking, but the record wont update. I get a 'record updated successfully' message but the record isn't updated.
I am pretty sure the lack of a record update is coming down to the ID not getting collected properly via the $q=$row["BearId"] but if I use $q=$_GET["q"] I get nothing but errors. I am not completely positive this is the problem though, that is why Im asking the question here.
I would appreciate any help that you can give. Ive gotten so far with this thing and yet I cant get it to update the record.
EDIT: I have pinpointed the problem down to the id in
$sql = "UPDATE //snip WHERE BearId = '$q'";
$q=$row["BearId"];
If I manually change BearId to equal '1' then the record is updated.
updatebears.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>Update Bears</title>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("result").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getvalues.php?q="+str,true);
xmlhttp.send();
}
</script>
<style>
// style elements
</style>
</head>
<body>
<h1>Update Bears</h1>
Select a Bear:
<br />
<select name="bears" onchange="showUser(this.value)">
<option value="">Select a BearId</option>
<?php
$query = "SELECT * FROM bears";
$mysqli = new mysqli('localhost','User','123','bears');
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
echo '<option value="'.$row["BearId"].'">'.$row["BearId"].'</option>';
?>
</select>
<br />
<?php
$q=$row["BearId"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if(array_key_exists('_submit_check', $_POST))
{
$weight = $_POST['Weight'];
$sex = $_POST['Sex'];
$type = $_POST['Type'];
$colour = $_POST['Colour'];
$breed = $_POST['BreedId'];
$sql = "UPDATE bears SET Weight = '$weight', Sex = '$sex', Type = '$type', Colour = '$colour', Breed = '$breed' WHERE BearId = '$q'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
?>
<br />
<div id="result"></div>
<br />
Click here to Visit Task 2 (Insert Bears) | Click here to Visit Task 3 (Display Bears)
</body>
</html>
getvalues.php
<?php
$q=$_GET["q"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if($stmt = $mysqli->prepare($sql))
{
$stmt->execute();
$stmt->bind_result($BearId, $Weight, $Sex, $Type, $Colour, $Breed);
while ($stmt->fetch())
{
echo "<form method='post' name='form1' onsubmit='return validateForm()' action='updatebears.php'>";
echo "<p>";
echo "<label for='BreedId'>BreedId:</label>";
echo "<br />";
echo "<select id='BreedId' name='BreedId' />";
echo "<option value='".$Breed."'>".$Breed."</option>";
echo "<option value='1'>1. Polar</option>";
echo "<option value='2'>2. Brown</option>";
echo "<option value='3'>3. Panda</option>";
echo "</select>";
echo "</p>";
echo "<p>";
echo "<label for='Weight'>Weight(kg):</label>";
echo "<br />";
echo "<input type='text' id='Weight' name='Weight' value='".$Weight."' />";
echo "</label>";
echo "</p>";
echo "<p>";
echo "Sex: ";
echo "<br />";
echo "<label for='M'>Male</label><input type='radio' id='M' value='M' name='Sex'";
if($Sex=='M') echo "checked";
echo "/>";
echo "<label for='F'>Female</label><input type='radio' id='F' value='F' name='Sex'";
if($Sex=='F') echo "checked";
echo "/>";
echo "</p>";
echo "<p>";
echo "<label for='Type'>Type:</label> ";
echo "<br />";
echo "<input type='text' id='Type' name='Type' maxlength='100' value='".$Type."' />";
echo "</p>";
echo "<p>";
echo "<label for='Colour'>Colour:</label>";
echo "<br />";
echo "<input type='text' id='Colour' name='Colour' maxlength='20' value='".$Colour."' />";
echo "</p>";
echo "<p>";
echo "<input type='submit' value='Submit' />";
echo "<input type='reset' value='Reset' />";
echo "<input type='hidden' name='_submit_check' value=1 />";
echo "</p>";
echo "</form>";
}
}
else
{
echo 'Unable to connect';
exit();
}
?>
Thanks for the help.
I believe what you need to do is create a hidden input type for the BearId within the getvalues.php file. That way when your form performs the post you can get the BearId from the post as opposed to trying to get it from the $row['BearId']. I'm fairly certain $row['BearId'] is not the same $row['BearId'] that the user selected when he first goes to the getvalues.php form. Have you tried printing $row['BearId'] to html to verify it's a legitimate value?
if(array_key_exists('_submit_check', $_POST))
{
$id = $_POST['BearId']
$weight = $_POST['Weight'];
$sex = $_POST['Sex'];
$type = $_POST['Type'];
$colour = $_POST['Colour'];
$breed = $_POST['BreedId'];
$sql = "UPDATE bears SET Weight = '$weight', Sex = '$sex', Type = '$type', Colour = '$colour', Breed = '$breed' WHERE BearId = '$id'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
?>
<h1>getvalues.php</h1>
<?php
$q=$_GET["q"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if($stmt = $mysqli->prepare($sql))
{
$stmt->execute();
$stmt->bind_result($BearId, $Weight, $Sex, $Type, $Colour, $Breed);
while ($stmt->fetch())
{
echo "<form method='post' name='form1' onsubmit='return validateForm()' action='updatebears.php'>";
echo <input type="hidden" name="BearId" value='".$q."'>
echo "<p>";
echo "<label for='BreedId'>BreedId:</label>";
echo "<br />";
echo "<select id='BreedId' name='BreedId' />";
echo "<option value='".$Breed."'>".$Breed."</option>";
echo "<option value='1'>1. Polar</option>";
echo "<option value='2'>2. Brown</option>";
echo "<option value='3'>3. Panda</option>";
echo "</select>";
echo "</p>";
echo "<p>";
echo "<label for='Weight'>Weight(kg):</label>";
echo "<br />";
echo "<input type='text' id='Weight' name='Weight' value='".$Weight."' />";
echo "</label>";
echo "</p>";
echo "<p>";
echo "Sex: ";
echo "<br />";
echo "<label for='M'>Male</label><input type='radio' id='M' value='M' name='Sex'";
if($Sex=='M') echo "checked";
echo "/>";
echo "<label for='F'>Female</label><input type='radio' id='F' value='F' name='Sex'";
if($Sex=='F') echo "checked";
echo "/>";
echo "</p>";
echo "<p>";
echo "<label for='Type'>Type:</label> ";
echo "<br />";
echo "<input type='text' id='Type' name='Type' maxlength='100' value='".$Type."' />";
echo "</p>";
echo "<p>";
echo "<label for='Colour'>Colour:</label>";
echo "<br />";
echo "<input type='text' id='Colour' name='Colour' maxlength='20' value='".$Colour."' />";
echo "</p>";
echo "<p>";
echo "<input type='submit' value='Submit' />";
echo "<input type='reset' value='Reset' />";
echo "<input type='hidden' name='_submit_check' value=1 />";
echo "</p>";
echo "</form>";
}
}
else
{
echo 'Unable to connect';
exit();
}
I have been on this for hours on end. When I get one thing working something else doesn't work. Well now my page is not loading to the one it is suppose to. It is loading to the content page and not the form page.I have the home page in for the users to enter in there username and passowrd and if they dont have one then they can register on the form. I put all the forms on there. I don't know what I am doing wrong. Like I said I been working on this for hours. Can someone help me thanks.
Its not loading to the form it skips right over it to the content page. So if you click on register to the form you wont see it. It will go to the content page.
Here are my codes
This one is form page..
<?php
function FormDisplay($strMessage="**All fields are required!"){
echo "<p><strong>".$strMessage."</strong></p>\n";
echo "<form action=\"".$PHP_SELF."\" method=\"post\">\n";
echo "<table width=\"300\" cellpadding=\"2\" cellspacing=\"2\">\n";
echo "<tr>\n";
echo " <td><strong>Username:</strong></td>\n";
echo " <td><input type=\"text\" name=\"username\" value=\"". $_POST['username']."\"/> \n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>Password:</strong></td>\n";
echo " <td><input type=\"password\" name=\"password1\" />\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td nowrap=\"nowrap\"><strong>Repeat Password:</strong></td>\n";
echo " <td><input type=\"password\" name=\"password2\" /> \n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>First name:</strong></td>\n";
echo " <td><input type=\"text\" name=\"firstname\" value=\"". $_POST['firstname']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>Last name:</strong></td>\n";
echo " <td><input type=\"text\" name=\"lastname\" value=\"". $_POST['lastname']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>Email:</strong></td>\n";
echo " <td><input type=\"text\" name=\"email\" value=\"". $_POST['email']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>Address:</strong></td>\n";
echo " <td><input type=\"text\" name=\"address\" value=\"". $_POST['address']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>City:</strong></td>\n";
echo " <td><input type=\"text\" name=\"city\" value=\"". $_POST['city']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>State:</strong></td>\n";
echo " <td><input type=\"text\" name=\"state\" value=\"". $_POST['state']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>Zip:</strong></td>\n";
echo " <td><input type=\"text\" name=\"zip\" value=\"". $_POST['zip']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td><strong>Phone:</strong></td>\n";
echo " <td><input type=\"text\" name=\"phone\" value=\"". $_POST['phone']."\"/>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<tr>\n";
echo " <td> </td>\n";
echo " <td><input type=\"submit\" name=\"submit\" value=\"Sign up now!\". class=\"submit\" />\n";
echo "</tr>\n";
echo "<tr>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</form>\n";
}
if ($_POST['submit']!=""){
if ($_POST['username']==""||$_POST['password1']==""||$_POST['password2']==""||$_POST['firstname']==""||$_POST['lastname']==""||$_POST['address']==""||$_POST['email']==""||$_POST['city']==""||$_POST['state']==""||$_POST['zip']==""||$_POST['phone']=="");
$error=1;
}
else if ($_POST['password1']!=$_POST['password2']){
$error=2;
}
else{
$hostname="localhost";
$database="Contacts";
$mysql_login="Web_User";
$mysql_password="my1230";
if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){
echo "error on connect";
}
else{
if (!(mysql_select_db($database,$db))){
echo mysql_error();
echo "<br>error on table connection";
}
else{
$SQL="Insert into tblUsers(username,password,firstname,lastname,email,address,city,state,zip, phone,signupDate)values)'".$_POST['username']."',PASSWORD('".$_POST['password1']."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."',NOW())";
mysql_query($SQL);
if (is_numeric(mysql_insert_id())){
header("Location:member-content.php?name=".$_POST['username']);
}
else{
echo "Sorry, there was an error.Please try again ot contact the administrator";
}
mysql_close($db);//closeing out connection,done for now
}
}
}
?>
<!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=iso-8859-1" />
<link rel="style.css" type="text/css" >
<title>Members Only Framework::Signup page/title>
</head>
<body>
<hr />
<h2>Become a member of the coolest website on the net!</h2>
<hr />
<?php
if ($error==1){
FormDisplay("You did not enter all required fields");
}
elseif ($error==2){
FormDisplay("Your Passwords did not match");
}
else{
FormDisplay();
}
?>
</body>
</html>
This is the signin form:
<?php
include("config.php");
if ($_POST['username']==""|| $_POST['password']==""){
header("Location:member-home.php?mode=1");
}
else{
$hostname="localhost";
$database="contacts";
$mysql_login="Web_User";
$mysql_password="my1230";
if (!($db = mysql_connect($hostname, $mysql_login, $mysql_password))){
echo "error on connect";
}
else{
if (!(mysql_select_db($database,$db))){
echo mysql_error();
echo "<br>error on db connection";
}
}
$SQL="Select username from tblusers where username='".$_POST['username']."' AND password=PASSWORD('".$_POST['password']."')";
$resultSet=mysql_query($SQL);
if (mysql_num_rows($resultSet)>0){
$username=mysql_result($resultSet,0,"username");
header("Location:member-content.php?name=".$username);
}
else{
header("Location:member-home.php?mode=2");
}
}
?>
</body>
</html>
and this is the home:
<!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" />
<link href="style.css" type="text/css">
<title>Members Only Framework::Login Page</title>
</head>
<body>
<hr />
<h3>Welcome Members!!</h3>
<hr />
<p>Not a member yet? Click here to join!</p>
<?php
if ($_GET['mode']==1){
echo "<p style=\"color:red;\"><strong>Please enter your username and password!</strong></p>";
}
else if ($_GET['mode']==2){
echo "<p style=\"color:red;\"><strong>Sorry, there is no user with that name!</strong></p>";
}
else if ($_GET['mode']==3){
$username=$_GET['username'];
echo "<p style=\"color:red;\"><strong>".$username.".please login using the username and password</strong></p>";
}
?>
<p>Current members enter your username and password:</p>
<form action="member-signin.php" method="post">
<table width="200" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Username:</strong></td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td><strong>Password:</strong></td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Log in now!" class="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
You have a misplaced semicolon in your member-form.php:
if ($_POST['username']==""||$_POST['password1']==""||$_POST['password2']==""||$_POST['firstname']==""||$_POST['lastname']==""||$_POST['address']==""||$_POST['email']==""||$_POST['city']==""||$_POST['state']==""||$_POST['zip']==""||$_POST['phone']=="");
so you need to replace it with opening curly bracket.