Reputation: 3293
I am very new to php and need some help please!
I am using a text box to display a value from a table which I would like then be able to be edited and used for an update statement.
This text box currently gets populated based on what the $studentId is and this works fine.
$conn = connection();
$sql = "SELECT * FROM students WHERE studentId=$studentId";
foreach ($conn->query($sql) as $row) {
?>
<form>
Student Name: <input name="name" type="text" method="post" value="<?php echo( htmlspecialchars( $row['name'] ) ); ?>" />
<br>
<input name="submitStudentUpdate" type="submit" value="Update" />
</form>
<?php
}
connection(); is being provided from another page.
It's this line I need the value of
Student Name: <input name="name" type="text" method="post" value="<?php echo( htmlspecialchars( $row['name'] ) ); ?>" />
I would like to retrieve the value from this form and post to another page to be used in an update statement but how do I get at it?
I thought it may be:
if (isset($_POST['name'])) {
echo $_POST['name'];
}
So currently, text box gets populated with
[Bob]
And I would like to be able to change it to, e.g.
[Bobby]
hit the submit button and retrieve this value.
I am using PDO.
Hope this makes sense and any help much appreciated. Thankyou.
Upvotes: 0
Views: 1102
Reputation: 688
You didn't define the method in your form so it defaults to get
So you should be able to get your value via $_GET['name']
I hope I'm understanding you correctly.
<?PHP
if(isset($_GET['name'])) {
$sql = "UPDATE students SET name = '".mysql_real_escape_string($_GET['name'])."' WHERE studentID = '".(int)$_GET['studentId']."' LIMIT 1";
// or with pdo prepared statements (more secure)
$stmt = $conn->prepare("UPDATE students SET name = ? WHERE studentID = ? LIMIT 1");
$stmt->execute(array($_GET['name'], $_GET['studentId']));
}
$conn = connection();
$sql = "SELECT * FROM students WHERE studentId=$studentId";
foreach ($conn->query($sql) as $row) {
?>
<form>
Student Name: <input name="name" type="text" method="post" value="<?php echo( htmlspecialchars( $row['name'] ) ); ?>" />
<br>
<input type="hidden" name="id" value="<?php echo $studentId; ?>">
<input name="submitStudentUpdate" type="submit" value="Update" />
</form>
<?php
}
?>
Upvotes: 1
Reputation:
$conn = connection();
$sql = "SELECT * FROM students WHERE studentId=$studentId";
if( isset($_GET['studentId']) )
{
"UPDATE FROM students SET name = '".$_POST['name']."' WHERE studentId=".$_POST['studentId'];
}
foreach ($conn->query($sql) as $row) {
?>
<form action="?studentId=<?php echo $row['studentId']; ?>" method="post">
Student Name: <input name="name" type="text" method="post" value="<?php echo( htmlspecialchars( $row['name'] ) ); ?>" />
<br>
<input name="submitStudentUpdate" type="submit" value="Update" />
</form>
<?php
}
Upvotes: 0