Please help me check my code especially change_form.php
here's my code

update_form.php

<?php
$con = mysql_connect("localhost","root","");

mysql_select_db("exercise",$con);
$query = mysql_query("select * from test_mysql",$con);
$num = mysql_num_rows($query);

echo "<center>Show database data</center>";
echo "<br>";
echo "Database data : $num";

while($num =mysql_fetch_array($query))
 {
 echo "<br>";
 echo $num[0];
 echo "<br>";
 echo "Name : ";
 echo $num[1];
 echo "<br>";
 echo "Lastname : ";
 echo $num[2];
 echo "<br>";
 echo "Email : ";
 echo $num[3];
 echo "<br>";

echo "Homepage :
";
 echo $num[4];
 echo "<br><a href=change_form.php?id=$num[0]>edit</a>";
 echo "<br>";
 }

 ?>

change_form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<?
$con=mysql_connect("localhost","root","");
mysql_select_db("exercise",$con);
$query=mysql_query("select * from test_mysql where id='$id'",$con);
while ($row = mysql_fetch_array($query))
{
?>
<form method="post" action="change_record.php">
Name : <input type="text" name="nama" value="<?php echo $row[1] ?>"/><br/>
lastname : <input type="text" name="lastname" value="<?php echo $row[2] ?>"/><br/>
email : <input type="text" name="email" value="<?php echo $row[3] ?>"/><br/>
<input type="submit" name="submit" value="update"/>
<input type="hidden" name="id" value="$row[0]"/>
</form>
<?
}
?>
</body>
</html>

change_record.php

<?php

$con = mysql_connect("localhost","root","");
mysql_select_db("exercise",$con);
$query = mysql_query("update test_mysql set id='$id',name='$name', lastname='$lastname', email = '$email' where id='$id'");
echo "Data with id = $id has been updated";
?>

any suggestion is much appreciated, thanks

Sorry for that..
First of all, i have created update_form.php to retrieve information from the database and it works perfectly without any errors..
when i click the edit button so the system will go to change_form.php and show the form. After i have entered all the information and click the submit button, the data isn't stored in the database..

here's the code for change_form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    </head>
     
    <body>
    <?
    $con=mysql_connect("localhost","root","");
    mysql_select_db("exercise",$con);
    $query=mysql_query("select * from test_mysql where id='$id'",$con);
    while ($row = mysql_fetch_array($query))
    {
    ?>
    <form method="post" action="change_record.php">
    Name : <input type="text" name="nama" value="<?php echo $row[1] ?>"/><br/>
    lastname : <input type="text" name="lastname" value="<?php echo $row[2] ?>"/><br/>
    email : <input type="text" name="email" value="<?php echo $row[3] ?>"/><br/>
    <input type="submit" name="submit" value="update"/>
    <input type="hidden" name="id" value="$row[0]"/>
    </form>
    <?
    }
    ?>
    </body>
    </html>

Your query in change_record.php is using variables like $name and $email . These do not yet exist. You have to retrieve them from the posted form like this:

$email = isset($_POST['email']) ? $_POST['email'] : '';

Aha. The problem is in your form. The attribute name in the input field is called nama.

Change this line.

Name : <input type="text" name="nama" value="<?php echo $row[1] ?>"/><br/>

To this line.

Name : <input type="text" name="name" value="<?php echo $row[1] ?>"/><br/>

This should solve your problem. Tell us how you go with it.

thanks for your help..

here's my code after done a few changes

change_record.php

<?php
$con = mysql_connect("localhost","root","nothing0101");
mysql_select_db("exercise",$con);

$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

$query = mysql_query("update test_mysql set name='$name', lastname='$lastname', email = '$email' where id= $id");
echo "Data with id = $id has been updated";
?>

by the way, do you know how to get the id value from hidden form i created in change_form.php because i can't use id=$_POST in this code. it returns nothing..

$id=$_POST;

Aha. The problem is in your form. The attribute name in the input field is called nama.

Change this line.

Name : <input type="text" name="nama" value="<?php echo $row[1] ?>"/><br/>

To this line.

Name : <input type="text" name="name" value="<?php echo $row[1] ?>"/><br/>

This should solve your problem. Tell us how you go with it.

thanks for your help.. hahaha.. but i still have a problem on how to retrieve id value from change_record.php.. do you know what is the code to retrieve id value.. thanks

Try using $_GET

I hope all your problems are solved. If yes so, please kindly mark the thread as solved.

Use View Source on your form page to check that there is a value for the hidden id field as $_POST will work as long as there is a value to post.

In fact, where are you getting the value for $id from anyway to use in your query in the first instance as I can't see any reference to it at all in your code?

I think he wants to pass the value of the variable id from a file to another. $_GET[] should work.

I agree, but where is it??

I'm lost now. Where is what?

I mean where is the $_GET, that is what I was trying to ask the poster - where is the value for the $id variable being obtained from.

Oh. it's somewhere in his code.. have a closer look.. the line's number is around 20th

Not in the form page it isn't - there is nothing above this line of code

$query=mysql_query("select * from test_mysql where id='$id'",$con);

That declares or assigns a value to $id before it is used in the query in change_form.php

And as you said, it needs to be from the $_GET that is sent in the URL from update_form.php

I meant to say that using $_REQUEST to pass the value to another page will solve the problem.


@simplypixie .. I ain't sure what the guy is really trying to achieve. But, it seems like the problem is solved on his side.

rotten69 - let's hope so:)

It seems like it. He's welcome again to ask questions related to his code or any question.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.