Current location: Hot Scripts Forums » Programming Languages » PHP » updating multiple records using php/mysql


updating multiple records using php/mysql

Reply
  #1 (permalink)  
Old 06-03-09, 11:32 AM
mikejkellyuk mikejkellyuk is offline
Newbie Coder
 
Join Date: Apr 2009
Location: salisbury uk
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
updating multiple records using php/mysql

Hi guys

i have a table called members which stores all the users information. what i am trying to do is allow the user to only update two fields in the database which are team number and pay scale for each user.

at the moment i can display all the user detials, i did this using a loop. what i want to be able to do is update certain fields for certain user, and it would be good if it could all be done in the same page.

any help would be good

here is the code i have so far

PHP Code:

$link mysql_connect('localhost','username','password');
if(!
$link){
die(
'Notconnected:'.mysql_error());
}
//make foo the current db
$db_selectedmysql_select_db('DB',$link);
if(!
$db_selected){
die(
'Can\'t use addressbook :'.mysql_error());};

$teamnumber=$_SESSION['teamnumber'];


$query="SELECT hrnumber, firstname, lastname, emailaddress, phonenumber, city, country, teamnumber, payscale FROM members WHERE teamnumber='$teamnumber'";
$result=mysql_query($query);
while(
$row = @mysql_fetch_array($result))
{
echo 
"<big><b>User Details</b></big>";
echo 
"<br>";
echo 
"<br>";
echo 
"HR Number:";
echo 
"" $row['hrnumber'] . "";
echo 
"<br>";
echo 
"First Name:";
echo 
"" $row['firstname'] . "";
echo 
"<br>";
echo 
"Last Name:";
echo 
"" $row['lastname'] . "";
echo 
"<br>";
echo 
"Email Address:";
echo 
"" $row['emailaddress'] . "";
echo 
"<br>";
echo 
"Phone Number:";
echo 
"" $row['phonenumber'] . "";
echo 
"<br>";
echo 
"city:";
echo 
"" $row['city'] . "";
echo 
"<br>";
echo 
"Country(e.g. UK,USA):";
echo 
"" $row['country'] . "";
echo 
"<br>";
echo 
"Team Number:";
echo 
"" $row['teamnumber'] . "";
echo 
"<br>";
echo 
"Pay Scale:";
echo 
"" $row['payscale'] . "";
echo 
"<br>";
echo 
"<br>";
}
echo 
"</form></p>"
thanks

mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-03-09, 11:45 AM
CreativeClans CreativeClans is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
You'll have to add input fields to the form for the data you want the user to change, and a hidden field for the table key value (HR number?).
Then, the script that receives the form values when the user submits the form, should check the validity of the values the user inserted, and if all is ok update the values in the database for that user.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-03-09, 12:16 PM
kodekin kodekin is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
you've got a form tag without a form open tag.
<FORM action="http://somesite.com/prog/adduser" method="post">

condense your code to something like:
echo "First Name: <input type='text' value='" . $row['firstname'] . "' /><br>";

then read this page:
SQL UPDATE Statement
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-03-09, 12:42 PM
mikejkellyuk mikejkellyuk is offline
Newbie Coder
 
Join Date: Apr 2009
Location: salisbury uk
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
i have done what you suggest, but my code is not updating the table here is my code

PHP Code:

$link mysql_connect('localhost','username','password');

if(!
$link){
die(
'Notconnected:'.mysql_error());
}
//make foo the current db
$db_selectedmysql_select_db('DB',$link);
if(!
$db_selected){
die(
'Can\'t use addressbook :'.mysql_error());};
$records = array();
if(
$_POST["count"])
{
 for(
$i=0;$i<$_POST["count"];$i++)
 {
  
$records[$i]["cb"] = $_POST["cb".$i];
  
$records[$i]["hrnumber"] = $_POST["hrnumber".$i];
  }
 for(
$i=0;$i<count($records);$i++)
 {
  if(
$records[$i]["cb"] == "on")
  {
   
mysql_query("UPDATE members SET teamnumber='" $row['teamnumber'] . "', payscale='" $row['payscale'] . "' WHERE hrnumber='".$records[$i]["hrnumber"]."'");
   }
  }
 }
$apprId 0;

$teamnumber=$_SESSION['teamnumber'];


$query="SELECT hrnumber, firstname, lastname, emailaddress, phonenumber, city, country, teamnumber, payscale FROM members WHERE teamnumber='$teamnumber'";
$result=mysql_query($query);
while(
$row = @mysql_fetch_array($result))
{
echo 
"<big><b>User Details</b></big>";
echo 
"<br>";
echo 
"<br>";
echo 
"<input type='checkbox' name='cb".$apprId."' />";
echo 
"<br>";
echo 
"HR Number:";
echo 
"" $row['hrnumber'] . "";
echo 
"<br>";
echo 
"First Name:";
echo 
"" $row['firstname'] . "";
echo 
"<br>";
echo 
"Last Name:";
echo 
"" $row['lastname'] . "";
echo 
"<br>";
echo 
"Email Address:";
echo 
"" $row['emailaddress'] . "";
echo 
"<br>";
echo 
"Phone Number:";
echo 
"" $row['phonenumber'] . "";
echo 
"<br>";
echo 
"city:";
echo 
"" $row['city'] . "";
echo 
"<br>";
echo 
"Country(e.g. UK,USA):";
echo 
"" $row['country'] . "";
echo 
"<br>";
echo 
"Team Number:";
echo 
"<input type='text' name='teamnumber' value='" $row['teamnumber'] . "'/>";
echo 
"<br>";
echo 
"Pay Scale:";
echo 
"<input type='text' name='payscale' value='" $row['payscale'] . "'/>";
echo 
"<br>";
echo 
"<input type='submit' value='Submit'><input type='hidden' name='count' value='".$apprId."'>";
echo 
"<br>";
}
echo 
"</form></p>";
?> 
thanks

mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-03-09, 12:57 PM
kodekin kodekin is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I'd say condense your two for loops into one, I could be wrong but i've never been able to define variables with a for loop only update variables outside of the loop.

so basically your second loop is trying to run through variables that aren't there anymore, so it fails your if test and does nothing.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-03-09, 01:06 PM
kodekin kodekin is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
something like this"

if(isset($_POST["count"])){
for($i=0;$i<$_POST["count"];$i++){
$records[$i]["cb"] = $_POST["cb".$i];
$records[$i]["hrnumber"] = $_POST["hrnumber".$i];

if($records[$i]["cb"] == "on"){
mysql_query("UPDATE members SET teamnumber='" . $row['teamnumber'] . "', payscale='" . $row['payscale'] . "' WHERE hrnumber='".$records[$i]["hrnumber"]."'");
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-03-09, 01:07 PM
mikejkellyuk mikejkellyuk is offline
Newbie Coder
 
Join Date: Apr 2009
Location: salisbury uk
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
i have used the same code in another function and it work in that, i think its something to do with the update statement
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-03-09, 01:13 PM
kodekin kodekin is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
update looks good, the only other thing I can think of is

if(isset($_POST["count"])){

maybe without isset it's not a reading a true/false value.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 06-04-09, 04:47 AM
CreativeClans CreativeClans is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Change this
PHP Code:

mysql_query("UPDATE members SET teamnumber='" $row['teamnumber'] . "', payscale='" $row['payscale'] . "' WHERE hrnumber='".$records[$i]["hrnumber"]."'"); 

in
PHP Code:

$query "

  UPDATE members 
  SET 
      teamnumber='" 
$row['teamnumber'] . "'
    , payscale='" 
$row['payscale'] . "' 
  WHERE hrnumber='"
.$records[$i]['hrnumber']."'
"
;
echo 
"query: $query<br />";
mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error()); 
The echo will show you the query that is executed, so you can check if it's correct. Delete the echo once everything is working fine.
The 'or die' will show you the error MySQL throws if there is a problem with the query.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to enter multiple records reshma PHP 2 04-03-09 01:12 AM
Updating Multiple Table Values w/ Post Data arcuivie PHP 5 12-11-08 03:58 PM
perl multiple selection of records bangarushiva Perl 1 12-05-08 09:46 PM
stuck on updating multiple records us0r PHP 6 05-21-04 12:19 AM


All times are GMT -5. The time now is 09:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.