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!)

Recommended Answers

All 7 Replies

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.

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.

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:

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.

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";
?>

I am not clear can u expline briefly

I actually fixed it on my own but I appreciate the consern.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.