Jump to content


Photo

Editing MySQL Data


  • Please log in to reply
7 replies to this topic

#1 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 10 August 2006 - 12:30 PM

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.


"Whats wrong with the cat?" - Mrs Schrödinger

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 August 2006 - 12:56 PM

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

<?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);
?>

Now to your question. I would do this. Use an
if (isset($_POST['whatever'])) {
. 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.

<?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>

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

Hope this helps,
Tom


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 10 August 2006 - 01:27 PM

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
"Whats wrong with the cat?" - Mrs Schrödinger

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 August 2006 - 01:34 PM

sorry typo I wrote that on the fly. It is
$_SERVER['PHP_SELF']
forgot the _ Also I forgot a couple ' in the query. Sees the edited version above ^


Good luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 10 August 2006 - 03:07 PM

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....?

"Whats wrong with the cat?" - Mrs Schrödinger

#6 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 10 August 2006 - 03:26 PM

You should edit this:

<input type="Submit" value="Update">

to

<input name = "submit" type="Submit" value="Update">

Because you have:

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

But before there was no submit.....

And your other problem this might fix it:

$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());
<br />
//Edit: "$db" isnt needed when selecting a database.

#7 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 10 August 2006 - 03:37 PM


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
"Whats wrong with the cat?" - Mrs Schrödinger

#8 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 10 August 2006 - 03:40 PM

I'm sorry l couldn't get it working.  It wouldn't even display :(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users