Current location: Hot Scripts Forums » Programming Languages » PHP » updating tables problem


updating tables problem

Reply
  #1 (permalink)  
Old 12-26-03, 03:09 AM
concatenate_man concatenate_man is offline
New Member
 
Join Date: Dec 2003
Location: Australia
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
updating tables problem

Hi folks. I have a problem with my code. basically i have three files to update a database table in mysql. the first

Code:
<?php 
$connection = mysql_connect('localhost'); 

$db = @mysql_select_db(marcat, $connection); 
?> 

<html> 
<head> 
   <title>MarCat Buyer Update</title> 
</head> 
<body bgcolor="#000000" text="#FFFFFF" link="#0000FF" 
     vlink="#000080" alink="#FF0000"> 
    <p align="center"><img src="images/logo.gif" width="850" height="89"></p> 
<p> 
<center> 
<h2>MarCat Buyer Update Page</h2> 
</center> 
<p> 
<hr> 
<form action="modify_buyer_results.php" method="POST"> 
<table border="1" > 
  <tr> 
      <td>BUYERS</td> 
      <td> 
      <?php 
      echo '<form><select name=Name>'; 
      $names = mysql_query("SELECT Name FROM BUYERS ORDER BY Name"); 
      while ($row = mysql_fetch_array($names)) 
      { 
        $name_entry=$row["Name"]; 
        echo "<option value=$name_entry>$name_entry\n"; 
      } 
      echo "</form>"; 
      ?> 
      </td> 
      <td> 
      <input type="Submit" name="submit" value="Select Buyer"> 
      </td> 
  </tr> 
</table> 
<p> 

<hr><p> 


</body> 
</html>
connects to the database and lists the 'Name' field of the BUYERS table and
allows you to select one "name" and then calls the next file.

the second file.

Code:
<html> 
<head> 
   <title>MarCat Buyer Update</title> 
</head> 
<body bgcolor="#000000" text="#FFFFFF" link="#0000FF" 
     vlink="#000080" alink="#FF0000"> 
    <p align="center"><img src="images/logo.gif" width="850" height="89"></p> 
<p> 
<center> 
<h2>MarCat Buyer Update Page</h2> 
</center> 
<p> 
<hr> 
<?php 
$connection = mysql_connect('localhost'); 
$db = @mysql_select_db(marcat, $connection); 
$database_name = $_POST['Name']; 
$query = " SELECT    * 
         FROM BUYERS 
         WHERE Name = '$database_name'"; 
             

$result = mysql_query($query); 

$buyers = mysql_fetch_array($result); 
mysql_close();       
?> 
<form action="buyer_updated.php" method="POST"> 
<input type="hidden" name ="database_name" value=" <php echo $database_name; ?> ">
<table border="0"> 
  <tr> 
    <td>Name: </td><td><input type="text" name="ud_name" value="<? echo "$buyers[Name]"?>"></td> 
  </tr> 
  <tr> 
    <td>Address: </td><td><input type="text" name="ud_address" value="<? echo "$buyers[Address]"?>"></td> 
  </tr> 
  <tr> 
    <td>Phone: </td><td><input type="text" name="ud_phone" value="<? echo "$buyers[Phone]"?>"></td> 
  </tr> 
  <tr> 
    <td>Company: </td><td><input type="text" name="ud_company" value="<? echo "$buyers[Company]"?>"></td> 
  </tr> 
  <tr> 
    <td>ABN: </td><td><input type="text" name="ud_abn" value="<? echo "$buyers[ABN]"?>"></td> 
  </tr> 
  <tr> 
    <td colspan="2"><input type="Submit" value="Update Buyer"></td> 
</table> 
</form> 
<hr> 
</body> 
</html>
selects all fields in the BUYERS table for the name specified and displays these
values in a form that allows you to modify the values, which then calls file three.

file three updates the fields and echo's Record Updated.


Code:
<html> 
<head> 
   <title>MarCat Buyer Update</title> 
</head> 
<body bgcolor="#000000" text="#FFFFFF" link="#0000FF" 
     vlink="#000080" alink="#FF0000"> 
    <p align="center"><img src="images/logo.gif" width="850" height="89"></p> 
<p> 
<center> 
<h2>MarCat Buyer Update Page</h2> 
</center><p><hr> 
<?php 



$database_name = $_POST['database_name']; 
$ud_name=$_POST['ud_name']; 
$ud_address=$_POST['ud_address']; 
$ud_phone=$_POST['ud_phone']; 
$ud_company=$_POST['ud_company']; 
$ud_abn=$_POST['ud_abn']; 

$connection = mysql_connect('localhost'); 
$db = @mysql_select_db(marcat, $connection); 

$query="UPDATE BUYERS SET Name='$ud_name', Address='$ud_address', Phone='$ud_phone', Company='$ud_company', ABN='$ud_abn' WHERE Name='$database_name' "; 

mysql_query($query) or die(MySQL_Error()); 
echo "Record Updated"; 
mysql_close(); 
?> 
<hr> 
</body> 
</html>
the first two file appear to be working but the third doesn't work, ie as in
updating the database. the echo part works in the third file but the query
doesn't....

can anyone see what i am doing wrong or suggest a better way of doing it?

cheers
Joe
Reply With Quote
  #2 (permalink)  
Old 12-26-03, 04:52 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
I can't see anything worng in the third file .. but add this line after the mysql_query() to check :
PHP Code:

$num=mysql_affected_rows();

echo 
$num
if the output was 0 .. then it's really not updating !!

hmm ...
but I wonder why you are escaping the errors that comes from database selection function ?
PHP Code:

$db = @mysql_select_db(marcat$connection); 

//why did you add @ ? 
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 12-26-03, 04:24 PM
concatenate_man concatenate_man is offline
New Member
 
Join Date: Dec 2003
Location: Australia
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
hi NeverMind, I put in the code you suggested to find out if any rows are affected, well none were. So it looks like i have other issues to deal with here. Do you have any ideas as to how I can get an update script to display row values from a table then allow me to update the said values?

any help would be appreciated...
Thanks,
Joe

oh, the @ before mysql_select_database comes from a tute i did on the web but it was for adding values in your code not the interactive type i want to achieve.
Reply With Quote
  #4 (permalink)  
Old 12-26-03, 08:25 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
In the second script...

<form action="buyer_updated.php" method="POST">
<input type="hidden" name ="database_name" value=" <php echo $database_name; ?> ">
<table border="0">
<tr>
<td>Name: </td><td><input type="text" name="ud_name" value="<? echo "$buyers[Name]"?>"></td>
</tr>

The hidden input isn't using the first "?" in the echo as the second line is. Try adding it and see what happens.

value=" <php echo $database_name; ?>
to
value=" <? echo $database_name; ?>
Reply With Quote
  #5 (permalink)  
Old 12-27-03, 07:35 PM
concatenate_man concatenate_man is offline
New Member
 
Join Date: Dec 2003
Location: Australia
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you mdhall you were correct about the <php and <?. Everything works fine now.
Reply With Quote
  #6 (permalink)  
Old 12-27-03, 07:59 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Glad it worked for ya.
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
Many tables or "sub" tables? rhunter007 PHP 5 12-02-03 12:37 PM
Linking two tables within a db Ported Valhalla PHP 2 10-14-03 10:36 AM
getting -2147217900 error while inserting and updating skchakri ASP 3 09-20-03 10:34 AM
Problem Updating sasi ASP 1 08-18-03 01:55 AM
script that measures height of tables and vertically aligns nested tables amj01 Script Requests 1 07-04-03 01:20 PM


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