Jump to content

PHP form to update database


robert_a89

Recommended Posts

Hello,

 

I am attempting to create a form which would allow update of fields in a database.  Existing data stored in the database is used to populate the default values, and I want it so that they can change the fields and press a button to update the database.  A differently named form appears on the page for each row in the database.  The code uses a mixture of HTML and PHP.

 

However, when using the update button it does not update the database.  Any help would be muchly appreciated

<?php $db = new mysqli ('*****','*****,'*****','*****');//connect to db
            $results = $db->query("select * from images");
            while ($row = $results->fetch_assoc()){?>
   <form name="form[<?php echo $row['pkey']?>]" method="post" enctype="multipart/form-data"  action="">
      <h2><?php print $row['location']?></h2>
      <table width="456">

        <tr>
          <td>Title: </td>
          <td><input type="text" name="Title[<?php echo $row['pkey']?>]"  value="<?php echo $row['title']?>" ></td>
        </tr>
        <tr>
          <td>Type: </td>
          <td><select name="type_sel[<?php echo $row['pkey']?>]">
              <option><?php echo $row['type'];?></option>
              <?php 
            $results2 = $db->query("select type from work_types");
            while ($row2 = $results2->fetch_assoc()){?>
              <option><?php echo $row2['type']?></option>
              <?php }?>
            </select></td>
        </tr>
        <tr>
          <td>Collection: </td>
          <td><select name="coll_sel[<?php echo $row['pkey']?>]">
              <option><?php echo$row['type'];?></option>
              <?php 
            $results3 = $db->query("select distinct coll_name from coll_name");
            while ($row3 = $results3->fetch_assoc()){?>
              <option><?php echo $row3['coll_name']?></option>
              <?php }?>
            </select></td>
        </tr>
        <tr>
          <td>Date: </td>
          <td><input type="text" name="date[<?php echo $row['pkey']?>]" /></td>
        </tr>
        <tr>
          <td>Description: </td>
          <td><textarea cols=80 rows=5 name="desc[<?php echo $row['pkey']?>]"></textarea></td>
        </tr>
        <tr>
          <td><input name="Submit[<?php echo $row['pkey']?>]" type="submit" value="Upload image"></td>
        </tr>
      </table>
      <?php 
  


  if(isset($_POST['Submit["'.$row['pkey'].'"]'])) 
{
  $pkey = $row['pkey'];
  print $pkey;
  $title=$_POST['Title["'.$row['pkey'].'"]']; 
  $type_sel=$_POST['type_sel["'.$row['pkey'].'"]'];
  $coll_sel=$_POST['coll_sel["'.$row['pkey'].'"]'];
  $date=$_POST['date["'.$row['pkey'].'"]'];
  $desc=$_POST['desc["'.$row['pkey'].'"]'];   
$results4 = $db->query("UPDATE images SET title='$title' WHERE pkey='$pkey'");


}

  ?>
    </form>
    <?php 
    } 
   $db->close(); ?>

 

Thank you in advance for your help

Link to comment
Share on other sites

The names of teh inputs are followed by that so that each instance of it appearing on the page has it's own name.  I've found out that this is causing the issue with updating it and so cut it out.

 

Taking the POST block out of the while loop causes it to update only the last row of the database, whilst leaving it as it is updates all the field to the contents of the box where the button is pressed.

 

Heres a simplified code, using just one of the variables to update in the database

   <?php $db = new mysqli ('localhost','*****','*****','*****');//connect to db
			$results = $db->query("select * from images");

			while ($row = $results->fetch_assoc()){
				$counter=$row['pkey'];?>
   <form name="newad" method="post" enctype="multipart/form-data"  action="">
      <table width="456">

        <tr>
          <td>Title: </td>
          <td><input type="text" name="Title"  value="<?php echo $row['title']?>" ></td>
             <td><input type="submit" name="Submit" /></td></tr>
     </table>

      </form>
      <?php 
			}

  if(isset($_POST['Submit'])) 
{
  $title=$_POST['Title']; 

$results4 = $db->query("UPDATE images SET title='$title' WHERE pkey='$counter'");

}


  ?>
   
    <?php 
			//}

$db->close(); ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.