I'm totally new to php and learning.
I have a table name persons which has columns
- FirstName
- LastName
- Gender
- Subject
- Hobbies
The Task: was to dynamically populate a drop down list with the data of FirstName, display the selected record of the name chosen in the drop down list only and update the last name of the displayed record.
The Problem: I need a better way to this, so far I was not able to update the last of one row only- all the last names would be updated. And the variable $name which carries the value of the selected gives an error "undefined index" when I place it in the IF condition for changing last name at the bottom of the script, so I had to send the value to the submit type button and from there perform the task.
The Question Is the problem happening because of the scope of the variable $name? is there any other better way to do this without using the submit button?
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<form name="form_update" method="post">
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
else
{
echo "Connected to mySQL</br>";
}
//==========connection code end
//==========dynamic drop down start
echo "<select name= 'FirstName' >";
echo '<option value="">'.'--- Please Select Person ---'.'</option>';
$query = mysqli_query($con,"SELECT FirstName FROM persons");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['FirstName']."'>".$row['FirstName'].'</option>';
}
echo '</select>';
echo ' ';
echo '<input type="submit" name="submit" value="Submit"/>';
//-------------displaying associated row from db
if(isset($_POST['FirstName'])) //getting name of drop down box which has value that is first name
{
$name = $_POST['FirstName']; //store the name into a variable
$chosenname = $name; //store the name into another variable
$fetch="SELECT Firstname,LastName,Gender,Subject,Hobbies FROM persons WHERE Firstname = '".$name."'";
$result = mysqli_query($con,$fetch);
if(!$result)
{
echo "Error:".(mysqli_error($con));
}
echo '<table border="1" align="center">'.'<tr>'.'<td align="center">'. 'From Database'. '</td>'.'</tr>'; //Outer table first row
//inner table columns
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td>'.'First Name'.'</td>'.'<td>'.'Last Name'.'</td>'.'<td>'. 'Gender' .'</td>'.'<td>'. 'Subject'. '</td>'.'<td>'. 'Hobbies' .'</td>'.'</tr>';
while($data=mysqli_fetch_row($result))
{
echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td><td>$data[4]</td></tr>");
}
echo '</table>'.'</td>'.'</tr>'.'</table>';
//---------to update last name only
echo '<br/>'.'<br/>'.'<br/>';
//echo 'First Name:'.'<input type="hidden" name="fn" value="'.$karma.'"/>';
echo '<br/>';
echo 'Change Last Name:'. '<input type="text" name="updated"/>';
echo '<br/>';
echo '<input type="submit" name="sub" value="'.$chosenname.'"/>'; //passed the value of the name to the button
echo '<br/>';
$recieved = "";
$ta = "";
$fku="";
echo '<br/>';
if(isset($_POST['sub']))
{
echo '<br/>';
//$fku=$_POST['fn'];
//echo "hi".$fn;
$recieved = $_POST['sub'];
echo "Hello:".$recieved; //so first name has been fetched here from the button value
echo '<br/>';
$ta = $_POST['updated']; //fetched value from text area
echo "The Last Name is"." ".$ta;
echo '<br/>';
$queryu = "UPDATE persons SET LastName ='".$ta."' WHERE FirstName = '".$recieved."'";
echo '<br/>';
if (!mysqli_query($con,$queryu))
{
die('Error: ' . mysqli_error($con));
}
echo "Last Name is updated in the record with "." " .$ta;
}
}
?>
</form>
<br/><br/><br/>
<a href="main.html"> Go back to Main Page </a>
</body>
</html>