0

i have a table called horse, i would like to create a form to edit the horse_name and horse_height, but the data cannot be updated, it turns out there is a problem with the update query, when i print the query to show the values, the horse_name value is equal to horse_height

<body bgcolor="#009933">
<?php
     include("connection.php"); 
     $conn = oci_connect($UName,$PWord,$DB);
     if(empty($_POST["check"]))
     {
    $query="SELECT * FROM horse ORDER BY horse_name";
    $stmt = oci_parse($conn, $query);
    oci_execute($stmt);


   ?>
<center> <font face="Arial" size="3"><b>Horse Details</b></font>
</center>

<form method="post" action="multiplehorsemodify.php">  
<table border="1" align="center">
  <tr>
  <th>Horse ID</th>
  <th>Name</th>
  <th>Height</th>
  <th>Edit?</th>

  </tr>


  <?php 
        while($row = oci_fetch_array ($stmt))
        {
  ?>
         <tr>
         <td><?php echo $row["HORSE_ID"]; ?></td>
         <td align="center">
         <input type="text" size="24" 
            name="<?php echo $row["HORSE_ID"]; ?>" 
            value="<?php echo $row["HORSE_NAME"]; ?>"></td>

         <td align="center">
         <input type="text" size="5" 
            name="<?php echo $row["HORSE_ID"]; ?>" 
            value="<?php echo $row["HORSE_HEIGHT"]; ?>"></td>
         <td align="center">

<input type="checkbox" name="check[]" value="<?php echo $row["HORSE_ID"]; ?>">

      </td>

         </tr>
  <?php 
        }
  ?>
  </table>
<center>
    <input type="submit" 
      value="Update Selected Titles" />
  </center>
</form>
<?php
    oci_free_statement($stmt);
     }
     else
     {
         foreach($_POST["check"] as $horse_id)  
         {
             echo $horse_id;
             $query = "UPDATE horse set horse_name = ".$_POST[$horse_id]."horse_height = ".$_POST[$horse_id]." WHERE horse_id = '".$horse_id."'";
             echo $query;
            $stmt = oci_parse($conn,$query);
            oci_execute($stmt);
            header("location: multiplehorsemodify.php");
         }
     }
     oci_close($conn);
?>

</body>

1
  • im sorry, how can i copy html code here? Commented Sep 12, 2010 at 12:47

3 Answers 3

1

Well, you set name and height both to $_POST[$horse_id] in your query:

$query = "UPDATE horse set horse_name = ".$_POST[$horse_id]."horse_height = ".$_POST[$horse_id]." WHERE horse_id = '".$horse_id."'";

And you even give the input elements the same name:

name="<?php echo $row["HORSE_ID"]; ?>"

The name of an input element becomes the key of the $_POST array on the PHP side. So give the input fields the right names, e.g.:

 <input type="text" size="24" 
        name="horse_name[<?php echo $row["HORSE_ID"]; ?>]" 
        value="<?php echo $row["HORSE_NAME"]; ?>">

 <input type="text" size="5" 
        name="horse_height[<?php echo $row["HORSE_ID"]; ?>]" 
        value="<?php echo $row["HORSE_HEIGHT"]; ?>">

and change your query to

"UPDATE horse set horse_name = ".mysql_real_escape($_POST['horse_name'][$horse_id])
."horse_height = ".mysql_real_escape($_POST['horse_name'][$horse_id])
." WHERE horse_id = '".$horse_id."'"
Sign up to request clarification or add additional context in comments.

2 Comments

this way, the value of horse_name and horse_height point to the last horse in the list
@chandra wib: Have you really tested with the last version? Basically you have to create an array for the names, like you did for check[]. Sorry for the delay.
1

Both of your form fields have the same name ($row["HORSE_ID"]) which will cause problems!

Comments

1
 <input type="text" size="24" 
                name="HorseName"
id="name-<?=row["HORSE_ID"] ?>" 
                value="<?php echo $row["HORSE_NAME"]; ?>"></td>

             <td align="center">
             <input type="text" size="5" 
                name="HorseHeight" 
id="height-<?=row["HORSE_ID"] ?>"
                value="<?php echo $row["HORSE_HEIGHT"]; ?>"></td>
             <td align="center">

This way, you can still use the id to make the checkboxes for the particular horse unique

UPDATE:

Go ahead and do it like this for each horse:

<input type="text" size="24" name="HorseName"  value="<?= $row["HORSE_NAME"] ?>" />
<input type="text" size="5" name="HorseHeight"value="<?= $row["HORSE_HEIGHT"]; ?>" />
<input type="hidden" name="HORSE_ID" value="<?= $row["HORSE_ID"] ?>" />

Then your php and query can look like this:

$HORSE_NAME = $_POST['HORSE_NAME'];
$HORSE_HEIGHT = $_POST['HORSE_HEIGHT'];
$HORSE_ID = $_POST['HORSE_ID'];

"UPDATE table_name SET HORSE_NAME = $HORSE_NAME, HORSE_HEIGHT=$HORSE_HEIGHT WHERE HORSE_ID = $HORSE_ID"

2 Comments

what should i do with the query?
There you go Chandra, does that help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.