Jump to content

[SOLVED] Updating Databases


docpepper

Recommended Posts

Hi, this is my first time using this forum so just finding my feet!

 

Not sure whether this topic should be in this thread or over in mySQL but ill try it here!

 

I am having problems with updating and deleting from a mySQL table. I have set up a script to add a row to the table giving the user an ID (Unique key) and password. But when it comes to updating and deleting it just wont have it. I am not receiving any errors with my code but it just is not effecting the current table data what so ever. I will post my Update code below and hopefully if I can get a helping hand with that I can try to figure out the issue with the deleting myself.

 

Any help would be greatly appreciated, and go easy if my code is pap as i am novice!!

 

<?php
$id=$_GET['ID'];

$query="SELECT * FROM teachers WHERE ID='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 

$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"title");
$f_name=mysql_result($result,$i,"f_name");
$l_name=mysql_result($result,$i,"l_name");
$email=mysql_result($result,$i,"email");
$qual=mysql_result($result,$i,"qual");
$Subject=mysql_result($result,$i,"Subject");
$Role=mysql_result($result,$i,"Role");
$Position=mysql_result($result,$i,"Position");
$username=mysql_result($result,$i,"username"); 
$encrypt_password= md5($password);
$password=mysql_result($result,$i,"password");
?>
<h2>Edit <?php echo "$f_name"?> <?php echo "$l_name"?>'s Details</h2>
<form name="Editteacher" class="staffadd" method="post" action="<?php echo $PHP_SELF;?>">
<input type="hidden" name="ID" value="<?php echo "$id"?>">
Title: <input type="text" name="title" value="<?php echo "$title"?>"><br />
Initials: <input type="text" name="f_name" value="<?php echo "$f_name"?>"><br />
Surname: <input type="text" name="l_name" value="<?php echo "$l_name"?>"><br />
Email: <input type="text" name="email" value="<?php echo "$email"?>"><br />
Qualifications: <input type="text" name="qual" value="<?php echo "$qual"?>"><br />
Subject(s): <input type="text" name="Subject" value="<?php echo "$Subject"?>"><br />
Role: <input type="text" name="Role" value="<?php echo "$Role"?>"><br />
<select name="Position" />
<option value="Selected"><?php echo "$Position"?></option>
<option value="Senior Position">Senior Position</option>
<option value="Senior School (Full Time)">Senior School (Full Time)</option>
<option value="Senior School (Part Time)">Senior School (Part Time)</option>
<option value="Shared Staff">Shared Staff</option>
<option value="Junior School (Full Time)">Junior School (Full Time)</option>
<option value="Junior School (Part Time)">Junior School (Part Time)</option>
<option value="Infant School (Full Time)">Infant School (Full Time)</option>
<option value="Infant School (Part Time)">Infant School (Part Time)</option>
<option value="Nursery School (Full Time)">Nursery School (Full Time)</option>
<option value="Nursery School (Part Time)">Nursery School (Part Time)</option>
<option value="Visiting Staff">Visiting Staff</option>
<option value="Support Staff">Support Staff</option>
</select>
Username: <input type="text" name="username" value="<?php echo "$username"?>"><br />
Password: <input type="password" name="password" value="<?php echo "$password"?>"><br />
<input type="submit" name="submit" value="Update" class="Staffaddbutt" />
</form>
<?php
if($_POST['submit']) {
$title = $_POST['title'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$qual = $_POST['qual'];
$Subject = $_POST['Subject'];
$Role = $_POST['Role'];
$Position = $_POST['Position'];
$username = $_POST['username'];
$encrypt_password= md5($password);
$password = $_POST['password'];

$query="UPDATE teachers SET title='$title', f_name='$f_name', l_name='$l_name', qual='$qual', Subject='$Subject', Role='$Role', username='$username', password='$password', email='$email' WHERE ID='$id'";

mysql_query($query) or die(mysql_error());
}
?>
<?php
++$i;
} 
?>

 

It is for a school data teacher database. I have a full table layout with an edit and delete button to the very right of each row. Once clicked on 'Edit' it uses the id of the row and takes you to 'staffedit.php' where the above code can be found. It throws in all the details of the current teacher into the form and allows the user to edit the details. Then in my head, after the submit is pressed it should run the 'If' statement and UPDATE the table teachers with all the new data WHERE ID= $id. I think it might be some issue with calling the id but...'shrug'

 

Obv its not working so like before any help would by smashing.

 

Thanks.

Link to comment
Share on other sites

Hi, thanks for swift reply.

 

I added error_reporting(E_ALL);

 

I get 2 issues...

 

Undefined variable: password

 

and

 

Undefined index: submit

 

Not quite sure what to do with that info other than try to assume (dangerous i know!) that there is some problem with the $encrypt_password= md5($password); This could be that I added the passwords through our server directly before I wrote the editing script? and they may of had a different encryption?...

 

Not sure about the 'submit' one.

 

I tried echoing a 'Thankyou, the teachers details have been edited' line but was not sure whether it needed to be placed before or after the query had run, or in fact whether to insert it here...

 

mysql_query($query) --> * <-- or die(mysql_error());

 

 

Link to comment
Share on other sites

It is for an admin level to change the teachers details, so they need to see the current username and password. I suppose I dont need to echo them out?...

 

Back to the errors, does this mean then that the variables are not being called correctly, because as far as I can see they all match up...

Link to comment
Share on other sites

try this

 

 

<?php
$id=$_GET['ID'];

$query="SELECT * FROM teachers WHERE ID='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 

$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"title");
$f_name=mysql_result($result,$i,"f_name");
$l_name=mysql_result($result,$i,"l_name");
$email=mysql_result($result,$i,"email");
$qual=mysql_result($result,$i,"qual");
$Subject=mysql_result($result,$i,"Subject");
$Role=mysql_result($result,$i,"Role");
$Position=mysql_result($result,$i,"Position");
$username=mysql_result($result,$i,"username"); 
$encrypt_password= md5($password);
$password=mysql_result($result,$i,"password");
?>
<h2>Edit <?php echo "$f_name"?> <?php echo "$l_name"?>'s Details</h2>
<form name="Editteacher" class="staffadd" method="post" action="<?php echo $PHP_SELF;?>">
<input type="hidden" name="ID" value="<?php echo "$id"?>">
Title: <input type="text" name="title" value="<?php echo "$title"?>"><br />
Initials: <input type="text" name="f_name" value="<?php echo "$f_name"?>"><br />
Surname: <input type="text" name="l_name" value="<?php echo "$l_name"?>"><br />
Email: <input type="text" name="email" value="<?php echo "$email"?>"><br />
Qualifications: <input type="text" name="qual" value="<?php echo "$qual"?>"><br />
Subject(s): <input type="text" name="Subject" value="<?php echo "$Subject"?>"><br />
Role: <input type="text" name="Role" value="<?php echo "$Role"?>"><br />
<select name="Position" />
<option value="Selected"><?php echo "$Position"?></option>
<option value="Senior Position">Senior Position</option>
<option value="Senior School (Full Time)">Senior School (Full Time)</option>
<option value="Senior School (Part Time)">Senior School (Part Time)</option>
<option value="Shared Staff">Shared Staff</option>
<option value="Junior School (Full Time)">Junior School (Full Time)</option>
<option value="Junior School (Part Time)">Junior School (Part Time)</option>
<option value="Infant School (Full Time)">Infant School (Full Time)</option>
<option value="Infant School (Part Time)">Infant School (Part Time)</option>
<option value="Nursery School (Full Time)">Nursery School (Full Time)</option>
<option value="Nursery School (Part Time)">Nursery School (Part Time)</option>
<option value="Visiting Staff">Visiting Staff</option>
<option value="Support Staff">Support Staff</option>
</select>
Username: <input type="text" name="username" value="<?php echo "$username"?>"><br />
Password: <input type="password" name="password" value="<?php echo "$password"?>"><br />
<input type="submit" name="submit" value="Update" class="Staffaddbutt" />
</form>
<?php
if($_POST['submit']) {
$title = $_POST['title'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$qual = $_POST['qual'];
$Subject = $_POST['Subject'];
$Role = $_POST['Role'];
$Position = $_POST['Position'];
$username1 = $_POST['username'];
$password1 = $_POST['password'];
$encrypt_password= md5($password1);

$query="UPDATE teachers SET title='$title', f_name='$f_name', l_name='$l_name', qual='$qual', Subject='$Subject', Role='$Role', username='$username1', password='$encrypt_password', email='$email' WHERE ID='$id'";

mysql_query($query) or die(mysql_error());
}
?>
<?php
++$i;
} 
?>

 

Link to comment
Share on other sites

Unfortunately it brings up the same errors. I see you changed password and username to password1 and username1...Was this so the new data would not conflict with the current data?

 

It just doesnt seem to want to change the data....humm. If its not submitting the new data to db teachers with the current id then where is the info going?...

 

Brain starting to hurt!

Link to comment
Share on other sites

yes i changes the username and password to make sure things weren't conflicting. Keep them changed. Let break this down to keep in simple.

 

have your form page

 

<?php
$id=$_GET['ID'];

$query="SELECT * FROM teachers WHERE ID='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 

$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"title");
$f_name=mysql_result($result,$i,"f_name");
$l_name=mysql_result($result,$i,"l_name");
$email=mysql_result($result,$i,"email");
$qual=mysql_result($result,$i,"qual");
$Subject=mysql_result($result,$i,"Subject");
$Role=mysql_result($result,$i,"Role");
$Position=mysql_result($result,$i,"Position");
$username=mysql_result($result,$i,"username"); 
$encrypt_password= md5($password);
$password=mysql_result($result,$i,"password");
?>
<h2>Edit <?php echo "$f_name"?> <?php echo "$l_name"?>'s Details</h2>
<form name="Editteacher" class="staffadd" method="post" action="update.php">
<input type="hidden" name="ID" value="<?php echo "$id"?>">
Title: <input type="text" name="title" value="<?php echo "$title"?>"><br />
Initials: <input type="text" name="f_name" value="<?php echo "$f_name"?>"><br />
Surname: <input type="text" name="l_name" value="<?php echo "$l_name"?>"><br />
Email: <input type="text" name="email" value="<?php echo "$email"?>"><br />
Qualifications: <input type="text" name="qual" value="<?php echo "$qual"?>"><br />
Subject(s): <input type="text" name="Subject" value="<?php echo "$Subject"?>"><br />
Role: <input type="text" name="Role" value="<?php echo "$Role"?>"><br />
<select name="Position" />
<option value="Selected"><?php echo "$Position"?></option>
<option value="Senior Position">Senior Position</option>
<option value="Senior School (Full Time)">Senior School (Full Time)</option>
<option value="Senior School (Part Time)">Senior School (Part Time)</option>
<option value="Shared Staff">Shared Staff</option>
<option value="Junior School (Full Time)">Junior School (Full Time)</option>
<option value="Junior School (Part Time)">Junior School (Part Time)</option>
<option value="Infant School (Full Time)">Infant School (Full Time)</option>
<option value="Infant School (Part Time)">Infant School (Part Time)</option>
<option value="Nursery School (Full Time)">Nursery School (Full Time)</option>
<option value="Nursery School (Part Time)">Nursery School (Part Time)</option>
<option value="Visiting Staff">Visiting Staff</option>
<option value="Support Staff">Support Staff</option>
</select>
Username: <input type="text" name="username" value="<?php echo "$username"?>"><br />
Password: <input type="password" name="password" value="<?php echo "$password"?>"><br />
<input type="submit" name="submit" value="Update" class="Staffaddbutt" />
</form>

 

then have a page called update.php

 

<?php
if($_POST['submit']) {
$title = $_POST['title'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$qual = $_POST['qual'];
$Subject = $_POST['Subject'];
$Role = $_POST['Role'];
$Position = $_POST['Position'];
$username1 = $_POST['username'];
$password1 = $_POST['password'];
$encrypt_password= md5($password1);

$query="UPDATE teachers SET title='$title', f_name='$f_name', l_name='$l_name', qual='$qual', Subject='$Subject', Role='$Role', username='$username1', password='$encrypt_password', email='$email' WHERE ID='$id'";

mysql_query($query) or die(mysql_error());
}
?>
<?php
++$i;
} 
?>

 

in update.php echo out all the variables and make sure they are all present.

 

 

Link to comment
Share on other sites

Ok split and done...

 

staffedit.php still comes up with a password error but not the submit error.

 

Update.php echos all the variables correctly with no errors.

 

code below:

 

staffedit.php

 

<?php
error_reporting(E_ALL);
$id=$_GET['ID'];

$query="SELECT * FROM teachers WHERE ID='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 

$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"title");
$f_name=mysql_result($result,$i,"f_name");
$l_name=mysql_result($result,$i,"l_name");
$email=mysql_result($result,$i,"email");
$qual=mysql_result($result,$i,"qual");
$Subject=mysql_result($result,$i,"Subject");
$Role=mysql_result($result,$i,"Role");
$Position=mysql_result($result,$i,"Position");
$username=mysql_result($result,$i,"username"); 
$encrypt_password= md5($password);
$password=mysql_result($result,$i,"password");
?>
<h2>Edit <?php echo "$f_name"?> <?php echo "$l_name"?>'s Details</h2>
<form name="Editteacher" class="staffadd" method="post" action="staffupdated.php">
<input type="hidden" name="ID" value="<?php echo "$id"?>">
Title: <input type="text" name="title" value="<?php echo "$title"?>"><br />
Initials: <input type="text" name="f_name" value="<?php echo "$f_name"?>"><br />
Surname: <input type="text" name="l_name" value="<?php echo "$l_name"?>"><br />
Email: <input type="text" name="email" value="<?php echo "$email"?>"><br />
Qualifications: <input type="text" name="qual" value="<?php echo "$qual"?>"><br />
Subject(s): <input type="text" name="Subject" value="<?php echo "$Subject"?>"><br />
Role: <input type="text" name="Role" value="<?php echo "$Role"?>"><br />
Position: <select name="Position" />
<option value="Selected"><?php echo "$Position"?></option>
<option value="Senior Position">Senior Position</option>
<option value="Senior School (Full Time)">Senior School (Full Time)</option>
<option value="Senior School (Part Time)">Senior School (Part Time)</option>
<option value="Shared Staff">Shared Staff</option>
<option value="Junior School (Full Time)">Junior School (Full Time)</option>
<option value="Junior School (Part Time)">Junior School (Part Time)</option>
<option value="Infant School (Full Time)">Infant School (Full Time)</option>
<option value="Infant School (Part Time)">Infant School (Part Time)</option>
<option value="Nursery School (Full Time)">Nursery School (Full Time)</option>
<option value="Nursery School (Part Time)">Nursery School (Part Time)</option>
<option value="Visiting Staff">Visiting Staff</option>
<option value="Support Staff">Support Staff</option>
</select><br />
Username: <input type="text" name="username" value="<?php echo "$username"?>"><br />
Password: <input type="password" name="password" value="<?php echo "$password"?>"><br />
<input type="submit" name="submit" value="Update" class="Staffaddbutt" />
</form>

<?php
++$i;
} 
?>

 

staffupdated.php

 

<?php

if($_POST['submit']) {
$title = $_POST['title'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$qual = $_POST['qual'];
$Subject = $_POST['Subject'];
$Role = $_POST['Role'];
$Position = $_POST['Position'];
$username1 = $_POST['username'];
$password1 = $_POST['password'];
$encrypt_password= md5($password1);

echo ($title . $f_name . $f_name . $f_name . $l_name . $email . $qual . $Subject . $Role . $Position . $username1 . $password1);

$query="UPDATE teachers SET title='$title', f_name='$f_name', l_name='$l_name', qual='$qual', Subject='$Subject', Role='$Role', username='$username1', password='$encrypt_password', email='$email' WHERE ID='$id'";

mysql_query($query) or die(mysql_error());
}
?>

 

Any thoughts>?

Link to comment
Share on other sites

change

 

Username: <input type="text" name="username" value="<?php echo "$username"?>"><br />
Password: <input type="password" name="password" value="<?php echo "$password"?>"><br />

 

to

 

Username: <input type="text" name="username" value="<?php echo $username?>"><br />
Password: <input type="password" name="password" value="<?php echo $password?>"><br />

Link to comment
Share on other sites

Hey! I fiddled around a little and finally got it working!!

 

Thankyou so much for your help adam291086, without what you suggested I would still be lost!

 

FYI all I did was ad the $id variable to post list, and changed $encrypt_password to $password1 in update.php.

 

Im going to try and add the same principles to the staffdelete.php.

 

Wish me luck and thank you so much again.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.