Jump to content

Help with update database


webdevdea

Recommended Posts

I have created a sample database for a race.

I want to make it so you do not have to update all of the information when update is selected.

Link to the project

 and here is the update.php file

How do i get the fields to retain the information that was already entered so that user does not have to put data in every field upon update..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<meta name="description" content="" />

<meta name="keywords" content="" />

<meta name="author" content="" />

<link rel="stylesheet" type="text/css" href="style.css" media="screen" />

<title>Angels 5K & 1 Mile Family Fun Run/Walk </title>





		

<?php include('header.php'); ?>

<?php include('nav.php'); ?>

<?php include 'connection.php';?>

</head>
<body>
<div id="wrapper">
<fieldset>
<form method="post" action="">

<select name="select" id="del">

<option id="0"> --Select your Name--- </option>
<?php


$aaa=mysql_query("select * from DR_race");

while ($view = mysql_fetch_array($aaa))
{
?>
<option id=" <?php echo $view['ID'];?> "> 
<?php echo $view['LastName'];?> </option>
<?php } ?>
</select>
</fieldset>
<br>
<br>
<br>

<?php
//<input type="submit" /> save it to later use

$D= filter_input(INPUT_POST, "select");
include 'connection.php';
$con=mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);

if (isset($_POST['firstname'])&& isset($_POST['lastname']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['age']) && isset($_POST['size']))
{
 $firstname = $_POST['firstname'];
 $lastname = $_POST['lastname'];
 $Phone = $_POST['phone'];
 $email = $_POST['email'];
 $age = $_POST['age'];
 $size= $_POST['size'];

}
else
{
 $firstname = "(Not entered)";
 $lastname = "(Not entered)";
 $Phone = "(Not entered)";
 $email = "(Not entered)";
 $age  = "(Not entered)";
 $size = "(Not entered)";
}
echo <<<_END


<body>
<form method="post" action="">
<fieldset>
Firstname: <input type="text" name="firstname" required/><br><br>
LastName : <input type="text" name="lastname" required/><br><br>
Phone: <input type="text" name="phone" required /><br><br>
E_mail: <input type="text" name="email" required /><br><br>
Age: <input type="text" name="age"/ required ><br><br><br>
T-shirt size: <input type="text" name="size" required />
<input type="submit" value="update" />
</form>
</fieldset>
</body>
</html>
_END;

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
mysqli_query($con,"UPDATE DR_race SET FirstName='$firstname' , LastName='$lastname' , Phone='$Phone', Email='$email' , Age='$age' , Size='$size'
WHERE FirstName='$D' ");
//********************************************

?> 


</form>


<<?php include('footer.php'); ?>




</body>
</html>

Link to comment
Share on other sites

@Barand,

 

I don't think his question was clearly written. I think the key issue is this

 

 

How do i get the fields to retain the information that was already entered so that user does not have to put data in every field upon update..

 

I think what he wants is that after the user selects a record to edit that the form is displayed with all the current values entered/selected. I had started to edit his code, but it was kind of a mess and I just didn't have time to invest in fixing it and explaining it.

Link to comment
Share on other sites

the answer to your question is - you retrieve the selected data from your database table and output it in value=' ... ' attributes for each form field. when you select a name, you would also need to provide a way of submitting the name select/option value.

 

however, your code is a hodge-podge mess of mysql_/mysqli_ database functions, nested forms, form processing logic mixed in with the code producing your html document, unconditionally running form processing logic, no validation or escaping of data being put into queries...

 

the mysql_ database functions are depreciated and should not be used. since you ARE using mysqli_ functions, use those everywhere.

 

nested forms are invalid markup. if your form(s) do work, it's only because they have the same action='' attribute.

 

a majority of your php code that determines what to do on the page, process the form data, and runs database queries to retrieve data should be grouped together and come first on the page. this is referred to a the 'business logic.' the code producing the actual html document (and any css/javascript) should be grouped together and come last on the page. this is referred to as the 'presentation logic.' if you search the forum for the term 'business logic' you will find examples showing this organization principle.

 

your form processing logic must both test is a form has been submitted at all and it must validate the submitted data before using it. lastly, string data being put into sql query statements must be escaped and numerical data needs to be validated/cast as the appropriate numerical data type (or you need to use prepared queries.)

Link to comment
Share on other sites

Thank you so much, I am very new to this and learning. I had thought I asked the question right but I guess I didnt..

Im trying to learn and sometimes being a female I get treated like I am stupid.. Im going to try the above sample and see

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.