Jump to content

Archived

This topic is now archived and is closed to further replies.

Fearpig

Editing MySQL Data

Recommended Posts

Hello again Guys,
I'm stuck yet again!! I've now created the following code to populate three textboxes with data from a row in MySQL (linked through the $id from the page before). I now want to edit these fields, click submit and have it update the row in the database.

<html>
<head>
</head>
<body>
<?php
$db = mysql_connect("localhost", "root", "password");
$id = $_GET['id'];
mysql_select_db("Telephonelist",$db);
$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);
$myrow = mysql_fetch_array($result)
?>

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

<INPUT TYPE="HIDDEN" NAME="ID" VALUE="<?php echo $myrow["ID"] ?>"><br> 
<INPUT TYPE="TEXT" NAME="First_Name" VALUE="<?php echo $myrow["First_Name"] ?>" SIZE=30><br>
<INPUT TYPE="TEXT" NAME="Last_Name" VALUE="<?php echo $myrow["Last_Name"] ?>" SIZE=30><br>
<INPUT TYPE="TEXT" NAME="Role" VALUE="<?php echo $myrow["Role"] ?>" SIZE=30><br>


<input type="Submit" value="Update">
</form>
 
</body>
</html>

I know I need another SQL query like:

$query="UPDATE contacts SET First_Name='$ud_first', Last_Name='$ud_last', Role='$ud_role' WHERE id='$ud_id'";

But how do I include it into this page and tie it to the submit button? If anyone could suggest some code or a tutorial I would be very grateful.

Cheers.
Tom.

Share this post


Link to post
Share on other sites
You do not need all of those <?php ?>'s that is causeing unessicary drag on your server. You would noticed a lot faster processing time if you just echoed the entire form like this

[code]
<?php
$db = mysql_connect("localhost", "root", "password");
$id = $_GET['id'];
mysql_select_db("Telephonelist",$db);
$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);
while ($myrow = mysql_fetch_assoc($result)) {
echo '
<form action="Edit_SpecificPerson.php" method="post">

<INPUT TYPE="HIDDEN" NAME="ID" VALUE="' .  $myrow["ID"] . '">
   
<INPUT TYPE="TEXT" NAME="First_Name" VALUE="' . $myrow["First_Name"] . '" SIZE=30>

<INPUT TYPE="TEXT" NAME="Last_Name" VALUE="' . $myrow["Last_Name"] . '" SIZE=30>

<INPUT TYPE="TEXT" NAME="Role" VALUE="' . $myrow["Role"] . '" SIZE=30>
<input type="Submit" value="Update">
</form>';
}
mysql_free_result($result);
?>[/code]

Now to your question. I would do this. Use an [code=php:0]if (isset($_POST['whatever'])) {[/code] . Now before I get to far into this let me make sure I understand you correctly. You want to edit the information obtained from the script that you posted? If so then I would do this.

[code]
<?php
if (isset($_POST['submit'])) {
   $id = $_POST['id'];
   $first_name = $_POST['first_name'];
   $last_name = $_POST['last_name'];
   $role = $_POST['role'];
   
  $query = mysql_query("UPDATE `contacts` SET `first_name` = '$first_name', `last_name` = '$last_name', `role`= '$role' WHERE `id` = '$id'");
  if (!$query) {
     echo "Unable to update the users information";
  }else{
     echo "The users information was updated";
  }
}
?>
<html>
<head>
</head>
<body>
<?php
$db = mysql_connect("localhost", "root", "password");
$id = $_GET['id'];
mysql_select_db("Telephonelist",$db);
$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);
while ($myrow = mysql_fetch_assoc($result)) {
echo '
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">

<INPUT TYPE="HIDDEN" NAME="ID" VALUE="' .  $myrow["ID"] . '">
   
<INPUT TYPE="TEXT" NAME="First_Name" VALUE="' . $myrow["First_Name"] . '" SIZE=30>

<INPUT TYPE="TEXT" NAME="Last_Name" VALUE="' . $myrow["Last_Name"] . '" SIZE=30>

<INPUT TYPE="TEXT" NAME="Role" VALUE="' . $myrow["Role"] . '" SIZE=30>
<input type="Submit" value="Update">
</form>';
}
mysql_free_result($result);
?>
</body>
</html>
[/code]

You see in the version I have cut the processing of the <?php ?> down to two.

Hope this helps,
Tom

Share this post


Link to post
Share on other sites
Cheers for that tomfmason,
It looks as though that what I'm after. I'm now getting a new error though.

Notice: Undefined index: PHPSELF in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 28

Do you know what this is? I'm sure I've had it before but can't remember how to resolve it!

Cheers again though.
Tom

Share this post


Link to post
Share on other sites
sorry typo I wrote that on the fly. It is [code=php:0]$_SERVER['PHP_SELF'][/code] forgot the [b]_[/b] Also I forgot a couple ' in the query. Sees the edited version above ^


Good luck,
Tom

Share this post


Link to post
Share on other sites
Aaaaaarrrrrgggggghhhhh............

I'm sooo close to cracking this now! The problem seems to be now that when you click on submit the php file refers to itself but fails when it gets to the $id variable as that is defined on the page before.

(I now get the error message:

Notice: Undefined index: id in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 23

which is this line:

$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);

)

Any ideas on this one....?

Share this post


Link to post
Share on other sites
You should edit this:

[code]<input type="Submit" value="Update">[/code]

to

[code]<input name = "submit" type="Submit" value="Update">[/code]

Because you have:

[code=php:0]if (isset($_POST['submit']))[/code]

But before there was no submit.....

And your other problem this might fix it:

[code=php:0]$db = mysql_connect("localhost", "root", "password") or die('Error : ' . mysql_error());
$id = $_GET['id'];
mysql_select_db("Telephonelist") or die('Error : ' . mysql_error());
$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db) or die('Error : ' . mysql_error());[/code]
<br />
//Edit: "$db" isnt needed when selecting a database.

Share this post


Link to post
Share on other sites

Nope it didn't like that one.......


Notice: Undefined index: id in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 4

Notice: Undefined index: first_name in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 5

Notice: Undefined index: last_name in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 6

Notice: Undefined index: role in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 7

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 9

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 9
Unable to update the users information



Do you know where I can find a tutorial that I can follow as oppose to bugging you every few minutes! It looks as though I need to go through all of the basics again? Thanks again for your help.

Tom

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.