In this code there is one input field which is used for update data. data is updating perfectly. but i want to show the previous data in my input field when i will click 'Add' button. So that when i click add button i can see my previous data in the input field and then i can update.
I want to show biotext in the input field from database when I update data. i used $biotext in the input field. but the previous data is not showed.The input field shows ->
Notice: Undefined variable: biotext in /Applications/XAMPP/xamppfiles/htdocs/Social/edit.php on line 76
I don't know what is the problem. please help me to find out the problem. thanks in advance.
<?php
// edit bio
// edit bio
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
$id = $_GET['edit_id'];
$stmt= $db->select('SELECT biotext FROM bio WHERE userid =:userid',array('userid'=>$id));
}
if(isset($_POST['update']))
{
$userid = $_SESSION['userid'];
$biotext = $_POST['biotext'];
if(empty($biotext))
{
$errMSG = "Please update your bio.";
}
else
{
$user_bio = $db->update("UPDATE bio SET biotext=:biotext WHERE userid=:userid",array('biotext'=>$biotext,'userid'=>$userid));
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
?>
<div class="row main-row">
<div class="col-md-11 ">
<?php
if(isset($errMSG))
{
echo $errMSG;
}
?>
<form method="post" name="form1" action="edit.php">
<table width="25%" border="0">
<tr>
<td>Add your bio</td>
<td><input type="text" name="biotext" value="<?php echo $biotext; ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="update" value="Add"></td>
</tr>
</table>
</form>
</div>
</div>
<?php
}
// edit bio
// edit bio
?>
</div>
Hope I was able to understand your concern.
Here, you can use the below code I've written. Change the connection details and edit the query with the session variable you were fetching the user id from.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'stackDB');
$con = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
$result= mysqli_query($con, "SELECT biotext FROM bio WHERE userid = 1234");
$fieldValDemo = mysqli_fetch_row($result);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="row main-row">
<div class="col-md-11 ">
<form method="post" name="form1" action="edit.php">
<table width="25%" border="0">
<tr>
<td>Add your bio</td>
<td><input type="text" name="biotext" value="<?php echo $fieldValDemo[0]; ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="update" value="Add"></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
Stored in DB -
Fetched from DB and shown to user before editing