This question is relating to 2 php scripts.
The first script is called pick_modcontact.php where I choose a contact (from a contact book like phone book), then posts to the script show_modcontact.php When I click the submit button on the form on pick.modcontact.php. As a result of submitting the form I am then taken to show_modcontact.php. As the variables are not present the user is directed back to pick_modcontact.php
I can not work out how to correct the code so that it will show the results of the script show_modcontact.php
This script shows all contacts in a database which is an "address book" this part works fine. please see below.
Name:pick_modcontact.php
if ($_SESSION['valid'] != "yes") {
header( "Location: contact_menu.php");
exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = #mysql_connect("localhost", "admin", "user") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT id, f_name, l_name FROM $table_name ORDER BY f_name";
$result = #mysql_query($sql, $connection) or die(mysql_error());
$num = #mysql_num_rows($result);
if ($num < 1) {
$display_block = "<p><em>Sorry No Results!</em></p>";
} else {
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$option_block .= "<option value\"$id\">$f_name, $l_name</option>";
}
$display_block = "<form method=\"POST\" action=\"show_modcontact.php\">
<p><strong>Contact:</strong>
<select name=\"id\">$option_block</select>
<input type=\"submit\" name=\"submit\" value=\"Select This Contact\"></p>
</form>";
}
?>
<!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>Modify A Contact</title>
</head>
<body>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact</em></h2>
<p>Select a contact from the list below, to modify the contact's record.</p>
<? echo "$display_block"; ?>
<br>
<p>Return to Main Menu</p>
</body>
</html>
This script is for modifying the contact: named show_modcontact.php
<?php
if (!$_POST['id']) {
header( "Location: pick_modcontact.php");
exit;
} else {
session_start();
}
if ($_SESSION['valid'] != "yes") {
header( "Location: pick_modcontact.php");
exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = #mysql_connect("localhost", "admin", "pass") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT f_name, l_name, address1, address2, address3, postcode, prim_tel, sec_tel, email, birthday FROM $table_name WHERE id = '" . $_POST['id'] . "'";
$result = #mysql_query($sql, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$address3 = $row['address3'];
$country = $row['country'];
$prim_tel = $row['prim_tel'];
$sec_tel = $row['sec_tel'];
$email = $row['email'];
$birthday = $row['birthday'];
}
?>
<!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>Modify A Contact</title>
</head>
<body>
<form action="do_modcontact.php" method="post">
<input type="text" name="id" value="<? echo $_POST['id']; ?>" />
<table cellpadding="5" cellspacing="3">
<tr>
<th>Name & Address Information</th>
<th> Other Contact / Personal Information</th>
</tr>
<tr>
<td align="top">
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75" /></p>
<p><strong>Last Name:</strong><br />
<input type="text" name="l_name" value="<? echo "$l_name"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address1:</strong><br />
<input type="text" name="f_name" value="<? echo "$address1"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address2:</strong><br />
<input type="text" name="f_name" value="<? echo "$address2"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address3:</strong><br />
<input type="text" name="f_name" value="<? echo "$address3"; ?>" size="35" maxlength="75" /> </p>
<p><strong>Postcode:</strong><br />
<input type="text" name="f_name" value="<? echo "$postcode"; ?>" size="35" maxlength="75" /></p>
<p><strong>Country:</strong><br />
<input type="text" name="f_name" value="<? echo "$country"; ?>" size="35" maxlength="75" /> </p>
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75" /></p>
</td>
<td align="top">
<p><strong>Prim Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$prim_tel"; ?>" size="35" maxlength="75" /></p>
<p><strong>Sec Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$sec_tel"; ?>" size="35" maxlength="75" /></p>
<p><strong>Email:</strong><br />
<input type="text" name="f_name" value="<? echo "$email;" ?>" size="35" maxlength="75" /> </p>
<p><strong>Birthday:</strong><br />
<input type="text" name="f_name" value="<? echo "$birthday"; ?>" size="35" maxlength="75" /> </p>
</td>
</tr>
<tr>
<td align="center">
<p><input type="submit" name="submit" value="Update Contact" /></p>
<br />
<p>Retuen To Menu</p>
</td>
</tr>
</table>
</form>
</body>
</html>
note for site admin, I am re posting this question with the hope of someone else reading over it. older questions seem to go dead after a while.
I think you're missing an "=" sign on the value attribute here:
$option_block .= "<option value\"$id\">$f_name, $l_name</option>";
Maybe you meant this?
$option_block .= "<option value=\"$id\">$f_name, $l_name</option>";
Related
I am having trouble updating my database data. I put the data that I want to update but when I click on the "Update" button it does
nothing. I have called the file on another php file by using Update
It also shows this error
Notice: Undefined variable: fname in
C:\xampp\htdocs\project\change1.php on line 71
Can someone help me figure this issue, please?
<?php include("config.php"); ?>
<?php
if (isset($_GET['edit'])) {
$update = true;
$record = mysqli_query($con, "SELECT * FROM employee WHERE id='".$_GET['edit']."'");
$row = mysqli_fetch_array($record,MYSQLI_BOTH);
}
if (isset($_POST['update'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$addr = $_POST['addr'];
$phone = $_POST['phone'];
$id=$_GET['edit'];
$query = "UPDATE employee SET fname='".$fname."',lname='".$lname."',password='".$password."',addr='".$addr."',phone='".$phone."' WHERE id='".$id."'";
$result = mysqli_query($con,$query) or die ("problem inserting new record into database");
if($result){
header('location: show_db.php');
}
else {echo "Update not successful"; }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Data</title>
</head>
<body>
Home
<br/><br/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</body>
</html>
Put the html inputs inside a form
<form name ="form1" method ="get" action="">
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</form>
I'm back once again, I know you guys are probably tired of me lol but there are somethings I figured out how to add the text boxes. I am still having trouble with the drop down menu. I am supposed to have a drop down menu that has the list of the states and when you submit it, it will be abbreviated but that doesn't want to work for me either. I'm pretty sure it is something easy to fix and I keep over looking it.
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php"
onsubmit="return validateForm()"
method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<p style="text-align:center">Main Page!</p>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text"
name="firstname" id="firstname"
size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text"
name="lastname" id="lastname"
size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text"
name="streetaddress" id="streetaddress"
size="35" />
</label>
<br>
<br>
<label>City
<input type="text"
name="city" id="city"
size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</label>
<br>
<br>
<label>Zip code:
<input type="text"
name="zipcode" id="zipcode"
size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment"
id="pref_payment_visa" value="visa" checked />
</label><br>
<label>MasterCard
<input type="radio" name="pref_payment"
id="pref_payment_master" value="master" checked />
</label><br>
<label>American Express
<input type="radio" name="pref_payment"
id="pref_payment_american" value="american" checked />
</label><br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
Try this:
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
?>
</table>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_id; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
</label>
That is why code indentation is so important.. You were missing closing PHP tags at two places. Refer to below code.
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php" onsubmit="return validateForm()" method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<p style="text-align:center">Main Page!</p>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0)
{
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult))
{
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text" name="firstname" id="firstname" size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text" name="lastname" id="lastname" size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</label>
<br>
<br>
<label>City
<input type="text" name="city" id="city" size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult_state = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult_state))
{
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
} ?>
</select>
<?php } ?> <!-- remove this -->
</label>
<br>
<br>
<label>Zip code:
<input type="text" name="zipcode" id="zipcode" size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment" id="pref_payment_visa" value="visa" checked />
</label>
<br>
<label>MasterCard
<input type="radio" name="pref_payment" id="pref_payment_master" value="master" checked />
</label>
<br>
<label>American Express
<input type="radio" name="pref_payment" id="pref_payment_american" value="american" checked />
</label>
<br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
MY PHP doesn't update data. Here is my code:
<html>
<body>
<meta charset="utf-8">
<title>სატელეფონო ცნობარი</title>
<?php
include('connection.php');
if(isset($_GET['id'])) {
$id=$_GET['id'];
if(isset($_POST['submit'])) {
$tarigi=$_POST['addedon'];
$teleponi2=$_POST['tel2'];
$teleponi3=$_POST['tel3'];
$departamenti2=$_POST['department2'];
$departamenti3=$_POST['department3'];
$web=$_POST ['url'];
$email=$_POST['email'];
$address=$_POST['address'];
$comment=$_POST['comment'];
$query3=mysql_query("update phonebook set addedon='$tarigi', tel2='$teleponi2',tel3='$teleponi3', department2='$departamenti2' , department3='$departamenti3' url='$web', email='$email', address='$address', comment='$comment' where id=$id");
if($query3) {
header('location:listcnobari.php');
}
}
$query1=mysql_query("select * from phonebook where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<fieldset style="width:325px;">
<form method="post" action="">
დამატების თარიღი:<input type="text" name="addedon" value="<?php echo $query2['addedon']; ?>" /><br />
ტელეფონი2:<input type="text" name="tel2" value="<?php echo $query2['tel2']; ?>" /><br />
ტელეფონი3:<input type="text" name="tel3" value="<?php echo $query2['tel3']; ?>" /><br>
დეპარტამენტი2:<input type="text" name="department2" value="<?php echo $query2['department2']; ?>" /><br />
დეპარტამენტი3:<input type="text" name="department3" value="<?php echo $query2['department3']; ?>" /><br />
ვებ.გვერდი:<input type="text" name="url" value="<?php echo $query2['url']; ?>" /><br />
ელ.ფოსტა:<input type="text" name="email" value="<?php echo $query2['email']; ?>" /><br />
მისამართი:<input type="text" name="address" value="<?php echo $query2['address']; ?>" /><br />
კომენტარი:<input type="text" name="comment" value="<?php echo $query2['comment']; ?>" /><br />
<input type="submit" name="submit" value="Update" />
</form>
<?php
}
?>
</body>
</html>
Change your query to
$query3=mysql_query("update phonebook set addedon='$tarigi', tel2='$teleponi2',
tel3='$teleponi3', department2='$departamenti2' , department3='$departamenti3', url='$web', email='$email',
address='$address', comment='$comment' where id=$id");
You missed a comma
department3='$departamenti3' url='$web'
You missed a comma.
change
department3='$departamenti3' url='$web'
to
department3='$departamenti3', url='$web'
I'm very new to PHP and MySQL. I've created a form which creates rows in my MySQL tables, I created a page that shows all those entries. Now I'm trying to edit those entries. I created another form that should populate with info from the selected row..the only problem is that it's not. Here's all my code including login.php...just in case. Please let me . I've looked everywhere for an answer and couldn't find one. All I found was mysql and not mysqli.
<?php //login.php
$db_hostname = 'localhost';
$db_database = 'mydata';
$db_username = 'user';
$db_password = '123#';
// Connect to server.
$link = mysqli_connect($db_hostname, $db_username, $db_password);
if (!$link) {
die('Not connected : ' . mysqli_error());
}
// Select the database.
$db_selected = mysqli_select_db($link, $db_database);
if (!$db_selected) {
die ('Can\'t use database : ' . mysqli_error());
}
?>
edit_client.php
<?php
$title = "Edit Client Form";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div> <!-- form container div -->
<form action="" method="post">
<div> <!-- form description div -->
<h2><?php echo $title ?></h2>
<p>Edit a client here.</p>
</div>
<?php
require_once = 'login.php';
// get value of id that sent from address bar
$id=$_GET['id'];
if(isset($id)) {
$sql="SELECT * FROM `clients` WHERE client_id='$id'";
$result=mysqli_query($sql);
$row=mysqli_fetch_assoc($result);
?>
<input type="hidden" name="id" value="<?php echo $row['client_id']; ?>">
<input type="text" name="first_name" value="<?php echo $row['first_name']; ?>" required><br />
<input type="text"name="last_name" value="<?php echo $row['last_name']; ?>" required><br />
<input type="text"name="company_name" value="<?php echo $row['company_name']; ?>" ><br />
<input type="text" name="address" value="<?php echo $row['address']; ?>"><br />
<input type="text" name="city" value="<?php echo $row['city']; ?>" required><br />
<input type="text" name="state" value="<?php echo $row['state']; ?>" required><br />
<input type="number" name="zip_code" value="<?php echo $row['zip_code']; ?>" required><br />
<input type="tel" name="tel_number" value="<?php echo $row['tel_number']; ?>" required><br />
<input type="email" name="email" value="<?php echo $row['email']; ?>" required><br />
<input value="Submit" type="submit">
</form>
</div>
</body>
</html>
here's the table from "display_client" that sends the ID to the edit_form.php page
<td align="left">' . '<a href="\edit_client.php?id=' . $row['client_id'] . '/">Edit</td>
any pointers in the right direction would be much appreciated. thank you.
As suggested below I have used PDO Prepared statements to achieve my desired result. Thank you thank you! #nomistic !
<?php
$title = "Edit Client Form";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div> <!-- form container div -->
<form action="update_client_submit.php" method="post">
<div> <!-- form description div -->
<h2><?php echo $title ?></h2>
<p>Edit a client here.</p>
</div>
<?php
require_once 'login.php';
if(!empty($_GET['id'])){
$client_id = intval($_GET['id']);
}
try {
$results = $db -> prepare('SELECT * FROM clients WHERE client_id = ?');
$results -> bindParam(1, $client_id);
$results -> execute();
} catch(Exception $e) {
echo $e -> getMessage();
die();
}
$row = $results -> fetch(PDO::FETCH_ASSOC);
?>
<input type="hidden" name="client_id" value="<?php echo $row['client_id']; ?>">
<input type="text" name="first_name" value="<?php echo $row['first_name']; ?>" required><br />
<input type="text" name="last_name" value="<?php echo $row['last_name']; ?>" required><br />
<input type="text" name="company_name" value="<?php echo $row['company_name']; ?>" required><br />
<input type="text" name="address" value="<?php echo $row['address']; ?>" required><br />
<input type="text" name="city" value="<?php echo $row['city'] ?>" ><br />
<input type="text" name="state" value="<?php echo $row['state'] ?>" required><br />
<input type="text" name="zip_code" value="<?php echo $row['zip_code']; ?>" required><br />
<input type="text" name="tel_number" value="<?php echo $row['tel_number']; ?>" required><br />
<input type="text" name="email" value="<?php echo $row['email']; ?>" required><br />
<input type="submit" name="sumbit">
</form>
</body>
</html>
Edit, reducing this and correcting a potential problem in you query.
you need a loop to get the data. It should be have something like this (using your data)
$id=$_GET['id'];
if(isset($id)) {
$sql="SELECT * FROM clients WHERE client_id=$id";
$result=mysqli_query($sql);
$row=mysqli_fetch_assoc($result);
while($row=mysqli_fetch_assoc($result)) {
echo '<input type="hidden" name="id" value="'.$row['client_id']. '">';
}
Note: You may want to look into using prepared statements; they are pretty easy to learn, especially at this stage, and you will have a lot of advantages, such as not having to worry about whether you are quoting int and varchar variables in your query; you'd declare them separately. They are also MUCH more secure (preventing SQL injection) and can make large queries run faster. You would just need to switch to object oriented style. (note, listing your variables in your select is what you should be doing anyway rather than select from *). The select is shortened a little for ease of explanation.
Here's an example of how yours would work with mysqli:
This is your database connection (not much different, just slightly different syntax)
$db_selected = new mysqli($db_hostname, $db_username, $db_password, $db_database)
if ($db_selected->connect_error) {
die("Connection failed: " . $dbc->connect_error);
}
Here are your queries. bind_param just identifies the item as an INT and then replaces the ? in the query with the variable $id, and bind_result takes the results of your query and turns them into variables.
The rest is fairly self-explanatory
$sql = $db_selected->prepare("SELECT client_id, first_name, last_name FROM clients WHERE client_id= ? ");
$sql->bind_param("i", $id);
$sql->execute();
$sql->store_result();
$sql->bind_result($client_id, $first_name,$last_name);
while ($sql->fetch()) {
echo '<input type="hidden" name="id" value="'.$client_id. '">';
echo '<input type="text" name="first_name" value="'.$first_name. '" required><br />';
echo '<input type="text" name="last_name" value="'. $last_name. '" required><br />';
}
$sql->free_result();
$sql->close();
If you need some more information, here are some resources: mysqli prepared statements
Part of my assignment to create a form to update specific row data via HTML form. I have successfully retrieved the row data from the mysql database but when I try to display the specific row data (StudentNumber, FirstName, LastName etc.) within the html form's fields, it just shows empty fields. What could I have possibly missed out?
Here's the coding for the php page that displays the form:
<!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>updating data record</title>
</head>
<body>
<?php
//connect to the database
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('ccm3413', $conn);
mysql_set_charset("utf8",$conn);
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM studentinfo WHERE recordID = '$id'");
while ($row = mysql_fetch_assoc($result)) {
}
?>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
<label>Student Number:
<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $row['StudentNumber']; ?>"/>
</label>
</p>
<p>
<label>First Name:
<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $row['FirstName']; ?>"/>
</label>
</p>
<p>
<label>Last Name:
<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $row['LastName']; ?>"/>
</label>
</p>
<p>
<label>Email Address:
<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $row['EmailAddr']; ?>"/>
</label>
</p>
<p>
<label>Telephone:
<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $row['PhoneNumber']; ?>"/>
</label>
</p>
<p>
<input type="submit" name="Update" id="Update" value="Update" />
</p>
</form>
</body>
</html>
Try this code instead:
<!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>updating data record</title>
</head>
<body>
<?php
//connect to the database
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('ccm3413', $conn);
mysql_set_charset("utf8",$conn);
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT * FROM studentinfo WHERE recordID = '$id'");
$student = (mysql_num_rows($result)==1) ? mysql_fetch_assoc($result) : null ; //You expect only 1 student, right?
if (is_array($student)){
?>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
<label>Student Number:
<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $student['StudentNumber']; ?>"/>
</label>
</p>
<p>
<label>First Name:
<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $student['FirstName']; ?>"/>
</label>
</p>
<p>
<label>Last Name:
<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $student['LastName']; ?>"/>
</label>
</p>
<p>
<label>Email Address:
<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $student['EmailAddr']; ?>"/>
</label>
</p>
<p>
<label>Telephone:
<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $student['PhoneNumber']; ?>"/>
</label>
</p>
<p>
<input type="submit" name="Update" id="Update" value="Update" />
</p>
</form>
<?php
else {
echo "The student has not been found" ;
} ?>
</body>
</html>
You need to put the form inside the while brackets.