RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1616 | Replies: 7
Reply
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Help (again) on a modify record script

  #1  
Feb 22nd, 2007
Hi again. I've been tackling another tutorial which isn't working fully, basically it lists records in a table and an insert link sends the data to a text field which can then be modified. The data is displayed fine but when selected the form does not recieve the information. Its a simple table called test4 with unique key id and field Tom. Here is the code;

<?php 
$dbhost='localhost';
$dbusername='root';
$dbname = 'test';
mysql_connect ($dbhost, $dbusername);
mysql_select_db($dbname) or die('Cannot select database');
   if(!isset($cmd)) { 
   $result = mysql_query("SELECT * FROM test4"); 
   while($row=mysql_fetch_array($result)) 
 
   { 
    $Tom=$row["Tom"]; 
    $id=$row["id"]; 
 
echo "<strong> tom</strong>:  {$row['Tom']}</p>";
echo "<strong> id</strong>:  {$row['id']}</p>";
      echo "<a href='untitled.php?cmd=edit&id=$id'>$Tom - Edit</a>"; 
      echo "<br>"; 
    } 
    } 
?> 
 
<?php 
   if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 
   { 
   if (!isset($_POST["submit"])) 
   { 
   $id = (int)$_GET['id']; 
   $sql = mysql_query ("SELECT * FROM test4 WHERE id='$id'"); 
   $result = mysql_fetch_array($sql); 
   $myrow = @mysql_fetch_array($result); 
?> 
    <form action="untitled.php" method="post"> 
    <input type=hidden name="id" value="<?php echo $myrow["id"] ?>"> 
    Value in row Tom:<INPUT TYPE="TEXT" NAME="Tom" VALUE="<?php echo $myrow["Tom"] ?>" SIZE=20><br> 
      <input type="hidden" name="cmd" value="edit"> 
      <input type="submit" name="submit" value="submit"> 
      </form> 
<?php } ?> 
<?php 
 
   if ($_POST["submit"]) 
   { 
   $Tom = $_POST["Tom"]; 
   $sql = "UPDATE test4 SET Tom='$Tom' WHERE id='$id'"; 
      $result = mysql_query($sql); 
      echo "Success!"; 
   } 
} 
?>
 

Any help will be very much appreciated.
(Don't worry this is the last tutorila I'm doing, finished them for now!)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Posts: 37
Reputation: jblacdao is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
jblacdao jblacdao is offline Offline
Light Poster

Re: Help (again) on a modify record script

  #2  
Feb 22nd, 2007
Hi Tom, its me again. It seems that you over fetched your query result. You can modifying this part of your code

$result = mysql_fetch_array($sql); 
$myrow = @mysql_fetch_array($result); 

to this:

if (mysql_num_rows($sql) > 0)
 $myrow = @mysql_fetch_array($sql); 

The reason you were getting an empty result is because the second line in your original code you were trying to fetch a record from an array instead of the resource that you get by executing the mysql_query command.
Last edited by jblacdao : Feb 22nd, 2007 at 7:27 pm. Reason: accidentally hit the submit before finishing my post
Reply With Quote  
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Re: Help (again) on a modify record script

  #3  
Feb 23rd, 2007
Excellent, thank you very much it did the trick, the data is being sent to the form now. Any idea how to fix the final part? It isn't updating where the when submit is being clicked, I assume this is a fault of the query?
Thanks alot.
Reply With Quote  
Join Date: Jan 2007
Posts: 37
Reputation: jblacdao is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
jblacdao jblacdao is offline Offline
Light Poster

Re: Help (again) on a modify record script

  #4  
Feb 23rd, 2007
Originally Posted by tomhughes View Post
Excellent, thank you very much it did the trick, the data is being sent to the form now. Any idea how to fix the final part? It isn't updating where the when submit is being clicked, I assume this is a fault of the query?
Thanks alot.


Yep, that's easy as well =). The $id in the code block where you execute the update is empty. The reason for that is illustrated here:

   if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 
   { 
   if (!isset($_POST["submit"])) 
   { 
   $id = (int)$_GET['id']; <--- $id variable assignment statement when form is not submitted
   .
   .
   .
   }
   if ($_POST["submit"]) 
   { 
   $Tom = $_POST["Tom"]; 
   $sql = "UPDATE test4 SET Tom='$Tom' WHERE id='$id'"; <---- $id is not assigned a value because the previous if statement evaluates to false
      $result = mysql_query($sql); 
      echo "Success!"; 
   } 
   }

Based from the logic of your code when you update the record the id value is submitted via post so just assign the value of $id. Just add this line before your update statement

$id = $_POST['id'];

Let me know if it still doesn't work. Cheers :cheesy:
Last edited by jblacdao : Feb 23rd, 2007 at 6:30 pm.
Reply With Quote  
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Re: Help (again) on a modify record script

  #5  
Feb 26th, 2007
Thanks a lot you've been a huge help, I managed to sort out that last part myself, delighted it matches your answer.
One of these days I'll try a tutorial and it will work first time!
Again thanks.
Reply With Quote  
Join Date: Mar 2007
Posts: 13
Reputation: elderp is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
elderp elderp is offline Offline
Newbie Poster

Re: Help (again) on a modify record script

  #6  
Mar 14th, 2007
i need some help this script isn't working for me can anyone here help? This is a school project i am working on. The error code myscript gives me is at the while statement not sure what is up.
www.johnpedroza.com/policetrainer/iinvoice.php

<? 
@$empno = ($_POST['empno']);

include("iinvoice1.php"); 
@$host = "****";

@$username = "*****";

@$password = "****";

@$database = "*********";
if ($empno) // perform search only if a string was entered. 
{
mysql_connect($host,$username,$password)or die ("Problem connecting to Database");


$sql = "SELECT DISTINCT info647_crse . crsdesc , info647_crse . crsprice FROM info647_emp JOIN info647_reg USING ( empno ) JOIN info647_off USING ( offerno ) JOIN info647_crse USING ( crsno ) WHERE empno = '$empno' LIMIT 0, 30 ";





if ($sql) 
{ 
echo "<table width=100% border=0 cellpadding=0 cellspacing=5 class=Database_Output><tr>"; 


$counter = 0; 
while ($myrow = mysql_fetch_array($sql)){ // Begin while 
$desc = $myrow["crsdesc"];
$crsprice = $myrow["crsprice"];
echo "<tr class=sub_1><td colspan=4>".++$counter.".$desc : $crsprice</td></tr> 
<tr bgcolor=#55556F> 
<tr><td colspan=4>&nbsp;</td></tr>"; 
} // end while 
} else { echo "problems...."; } 
} else { 
echo "Search form is empty. <br> Go back and type a new search"; 
} 
echo "$empno";
?>
Reply With Quote  
Join Date: Jul 2006
Posts: 191
Reputation: vssp is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
vssp vssp is offline Offline
Junior Poster

Re: Help (again) on a modify record script

  #7  
Mar 15th, 2007
I am not clear can u expline briefly
Thanks
VSSP
Reply With Quote  
Join Date: Mar 2007
Posts: 13
Reputation: elderp is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
elderp elderp is offline Offline
Newbie Poster

Re: Help (again) on a modify record script

  #8  
Mar 15th, 2007
I actually fixed it on my own but I appreciate the consern.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:26 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC