PDA

View Full Version : UPDATE row


tony09uk
09-12-2012, 09:07 PM
Can anyone tell me why this isnt working?

Its not throwing up any errors either, so Im kinda stuck.

Ive been trying for over an hour now but cant work it out.

<?php

include('connection.php');


if(!isset($_POST['submit'])){
$id = $_GET['id'];
}else{
$id = $_POST['id'];
}

$query = "SELECT * FROM people WHERE id = $id";
$result = mysqli_query($link, $query);

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

$name = $row['name'];
$age = $row['age'];
$hobby = $row['hobby'];
echo $name . $age . $hobby;

}

if(isset($_POST['submit'])){

$id = $_POST['id'];
$update = "UPDATE people
SET name = '$name',
age = '$age',
hobby = '$hobby'
WHERE id = $id
LIMIT 1";
$r = @mysqli_query($link, $update) or die(mysqli_error($link));

if(mysqli_affected_rows($link)){
echo "done";
}else{
echo mysqli_error($link);
}

}

?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here

?>

<form method="post" action="update.php">

<p>Name<input type="text" name="name" value="<?php echo "$name"; ?>" /></p>
<p>Age<input type="text" name="age" value="<?php echo "$age"; ?>" /></p>
<p>Hobby<input type="text" name="hobby" value="<?php echo "$hobby"; ?>" /></p>
<p><input type="hidden" name="id" value="<?php echo "$id"; ?>" /></p>
<p><input type="submit" name="submit" value="SUBMIT" /></p>

</form>
</body>
</html>