Solved

Update a Table

Posted on 2009-05-14
12
198 Views
Last Modified: 2012-05-07
I am learning php and need some help please.

I need to update record which i change, maybe the first record, maybe the last. I THINK i should have a loop, but KNOW i need help
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	$updateTable = array($_POST['Business_type'], $_POST['hidBusinessTypeID']);
		foreach($updateTable as $val) { 
  $updateSQL = sprintf("UPDATE business_type SET Business_type=%s WHERE Business_Type_id=%s",
                       GetSQLValueString($_POST['Business_type'], "text"),
                       GetSQLValueString($_POST['hidBusinessTypeID'], "int"));
 
  mysql_select_db($database_CallBack, $CallBack);
  $Result1 = mysql_query($updateSQL, $CallBack) or die(mysql_error());
}
}

Open in new window

0
Comment
Question by:protype
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24383805
If you are only updating one row, you don't need a loop.

How does the code you have work, do you get an error? Does it update the row?

Do you have this somewhere before the code you showed us:

$CallBack = mysql_connect('localhost','user','password');
$database_CallBack = 'name_of_database';

If so, this should work:
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE business_type SET Business_type=%s WHERE Business_Type_id=%s",
                       GetSQLValueString($_POST['Business_type'], "text"),
                       GetSQLValueString($_POST['hidBusinessTypeID'], "int"));
  mysql_select_db($database_CallBack, $CallBack);
  mysql_query($updateSQL, $CallBack) or die(mysql_error());
}

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24383822
Note that I did not change anything in your code, just removed the unnecessary loop and the unnecessary assignment to $Result1.
0
 

Author Comment

by:protype
ID: 24383926
It is fine for the update of one record all the necessary code is in the file.

what i need to do is update records which have changes

i want to add a loop so that it will update a record which i change, the record could be the first, middle or last. could be 2 or 3 records, could be all records.

if there is a change update if no change dont update
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24383981
Is Business_Type_id the primary key in the business_type table? If so, there is only one row for each Business_Type_id, and only this one row will be updated.

If you need to update multiple rows at the same time, you must also have multiple input fields for Business_Type_id and Business_type. Do you have multiple fields in the HTML, two for each row? Can you show us the HTML?
0
 

Author Comment

by:protype
ID: 24384071
I display the fields via repeat.

                <?php do { ?>
                  <input name="hidBusinessTypeID" type="hidden" id="hidBusinessTypeID" value="<?php echo $row_rsBusinessType['Business_Type_id']; ?>" />
                  <input name="Business_type" type="text" class="TeleT" id="Business_type" value="<?php echo $row_rsBusinessType['Business_type']; ?>" />
                  <br />
                  <?php } while ($row_rsBusinessType = mysql_fetch_assoc($rsBusinessType)); ?></p>
            <p>&nbsp;                          </p>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24384190
This gives you multiple fields with the same name and id, that won't work. Try the code below. Note that the hidden field is not needed anymore, because the id is stored within the square brackets of the name of the text field.
                <?php do { ?>
                  <input name="Business_type[<?php echo $row_rsBusinessType['Business_Type_id']; ?>]" type="text" class="TeleT" id="Business_type_<?php echo $row_rsBusinessType['Business_Type_id']; ?>" value="<?php echo $row_rsBusinessType['Business_type']; ?>" />
                  <br />
                  <?php } while ($row_rsBusinessType = mysql_fetch_assoc($rsBusinessType)); ?></p>
            <p>                           </p>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24384211
To fetch the input and update the rows, do like this:
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  mysql_select_db($database_CallBack, $CallBack);
  foreach($_POST['Business_type'] as $key=>$value) {
    $updateSQL = sprintf("UPDATE business_type SET Business_type=%s WHERE Business_Type_id=%s",
                         GetSQLValueString($value, "text"),
                         GetSQLValueString($key, "int"));
    mysql_query($updateSQL, $CallBack) or die(mysql_error());
  }
}

Open in new window

0
 

Author Comment

by:protype
ID: 24384237
Ok this displays fine, thanks for the advice.

But the update is not working can you help with that please.
<?php do { ?>
                  <input name="Business_type[<?php echo $row_rsBusinessType['Business_Type_id']; ?>]" type="text" class="TeleT" id="Business_type_<?php echo $row_rsBusinessType['Business_Type_id']; ?>" value="<?php echo $row_rsBusinessType['Business_type']; ?>" />
                  <br />
                  <?php } while ($row_rsBusinessType = mysql_fetch_assoc($rsBusinessType)); ?></p>
            <p>                           </p>

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24384254
Did you see my last post? Do you get errors?
0
 

Author Comment

by:protype
ID: 24384616
SORRY did not see the POST and Had to go out for a min.

All looks to work fine Thanks A MIL

Because i am learning I need advice about other things how can i stick with someone or is it possible
0
 

Author Closing Comment

by:protype
ID: 31581394
Thank you very much, it a pitty i don't have someone to help me learn this stuff. I'm to old to go back to school.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24384678
I monitor the PHP zones, but after this question is closed, if you use the link that shows up "ask a related question" I will get an email notification.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question