user2699948
user2699948

Reputation: 21

update a the value in the table using php

I want to update my table in the database using php but it did not update. I don't know where is the problem

emember.php

<?php

$id= $_GET['id'];
$con=mysqli_connect("localhost","root","","members");
$result=mysqli_query($con,"select * from member_info");

echo "<form action='update.php' method='post'>";
while($row=mysqli_fetch_array($result))
{
echo "<table border=1>";

//echo "Profile Id: <input type='text' name='profile_id' value = '$row[profile_id]'>    <br>"
echo "<tr>";
echo "<br>"."<tr>"."ID: <input type='text' name='id' value = '$row[id]'"."</tr>";
echo "<br>"."<tr>"."Username: <input type='text' name='username' value = '$row[username]'"."</tr>";
echo "<br>"."Password: <input type='text' name='password' value = '$row[password]'"."<br>";
 echo "<br>"."Firstname: <input type='text' name='firstname' value = '$row[firstname]'"."<br>";
echo "<br>"."Lastname: <input type='text' name='lastname' value = '$row[lastname]'"."<br>";
echo "<br>"."Address: <input type='text' name='address' value = '$row[address]'"."<br>";
echo "<br>"."Gender: <input type='text' name='gender' value = '$row[gender]'"."<br>";
echo "<br>"."Birthdate: <input type='text' name='birthdate' value ='.$row[birthdate]'"."<br>";


}
echo"</tr>";
echo "</table>";
echo "<input type='submit' value = 'Save'>";
echo "</form>";

?>

the update.php

<?php
$id=$_POST['id'];
$username= $_POST['username'];
$password= $_POST['password'];
$firstname= $_POST['firstname'];
$lastname= $_POST['lastname'];
$address= $_POST['address'];
$gender= $_POST['gender'];
$birthdate= $_POST['birthdate'];

$con= mysqli_connect("localhost","root","","members");
mysqli_query($con,"update member_info set     id='$id',username='$username',password='$password',firstname='$firstname',lastname='$lastname',address='$address',gender='$gender',birthdate='$birthdate' where id='$id'");

echo "Successfully updated!";
echo "Back to <a href='index.php'>home</a>";
?>

anyone can help what's wrong with my code?

Upvotes: 0

Views: 101

Answers (3)

RaviRokkam
RaviRokkam

Reputation: 789

The problems I see in your code :

emember.php

<input tag is not closed

$row[id] should be accessed as :

echo "<br>"."<tr>"."ID: <input type='text' name='id' value = '".$row["id"]."' /></tr>";

update.php

I think you shouldn't update id since you are updating a record for that specific id. It should be:

"update member_info set username='$username',password='$password',firstname='$firstname',lastname='$lastname',address='$address',gender='$gender',birthdate='$birthdate' where id='$id'"

Upvotes: 0

Joran Den Houting
Joran Den Houting

Reputation: 3163

Do it like this:

<?php

// Your vars:
$id = $_POST['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$birthdate = $_POST['birthdate'];

// Use of object based MySQLi:
$mysqli = new mysqli("localhost","root","","members");

// Prepare your query:
$stmt = $mysqli->prepare("UPDATE member_info SET username = ?,
   password = ?,
   firstname = ?, 
   lastname = ?, 
   address = ?,
   gender = ?,
   birthdate = ?
   WHERE id = ?");
$stmt->bind_param($username,
   $password,
   $firstname,
   $lastname,
   $address,
   $gender,
   $birthdate,
   $id);

// Execute your query:
$stmt->execute();

// And close it! You're done!
$stmt->close();

echo "Successfully updated!";
echo "Back to <a href='index.php'>home</a>";
?>

Upvotes: 1

NewInTheBusiness
NewInTheBusiness

Reputation: 1475

First off, if the column "id" is auto_incremented, you won't be able to update it

Upvotes: 0

Related Questions