Jump to content
Pardon our ads (a necessary update) ×

Editing database info


pgsjoe

Recommended Posts

So I've got it to pull the information from my database and put it into the form fields, but for some reason, when I hit submit, it's not going through. It's reading the correct area because I get the message stating that it has been updated. So, no syntax errors, it's just not Updating the database.

[code]if($_GET['cmd']=="edit" || $_POST["cmd"]=="edit") {
   if (!isset($_POST["submit"]))   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM `calendar` WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
      
<div id="addevent">

<h1>EDIT Event Form</h1>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="edit" id="edit">
      <input type="hidden" name="id" value="<?php echo $myrow["id"] ?>">
  <p>
      <b>Start Date: </b><input name="date" type="text" VALUE="<?php echo $myrow["date"] ?>"/>
  </p>
  <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="120"><div align="right"><strong>Length of Event: </strong></div></td>
      <td><INPUT TYPE="TEXT" NAME="remove" VALUE="<?php echo $myrow["remove"] ?>" SIZE=30></td>
    </tr>
  </table>
  <br />
  <table border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><p align="right"><b>Type: </b></p></td>
    <td>    <select name="type" id="type">
      <option value="Chapter" selected="selected">Chapter</option>
      <option value="Regional">Regional</option>
      <option value="National">National</option>
    </select>
</td>
  </tr>
  <tr>
    <td width="120"><p align="right"><b>Chapter/Region/National: </b></p></td>
    <td>    <INPUT TYPE="TEXT" NAME="chapter" VALUE="<?php echo $myrow["chapter"] ?>" SIZE=30></td>
    </tr>
</table>
  
  <br />
  <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="120"><div align="right"><b>Event Title: </b></div></td>
      <td><input type="text" name="title" size="35" VALUE="<?php echo $myrow["title"] ?>"/>
        *</td>
    </tr>
    <tr>
      <td width="120"><div align="right"><strong>Location: </strong> </div></td>
      <td><input name="location" type="text" id="location" size="35" VALUE="<?php echo $myrow["location"] ?>"/></td>
    </tr>
    <tr>
      <td width="120"><div align="right"><strong>Address:  </strong></div></td>
      <td><input type="text" name="address" size="35" VALUE="<?php echo $myrow["address"] ?>"/></td>
    </tr>
  </table>
  <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="120"><div align="right"><strong>City: </strong></div></td>
      <td><input type="text" name="city" size=35 VALUE="<?php echo $myrow["city"] ?>"/></td>
    </tr>
    <tr>
      <td width="120"><div align="right"><strong>State: </strong></div></td>
      <td><INPUT TYPE="TEXT" NAME="state" VALUE="<?php echo $myrow["state"] ?>" SIZE=30></td>
    </tr>
    <tr>
      <td width="120"><div align="right"><strong>Zip: </strong></div></td>
      <td><input type="text" name="zip" size=10 VALUE="<?php echo $myrow["zip"] ?>"/></td>
    </tr>
  </table>
  <br />
  <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="120" valign="top"><div align="right"><b>Description: </b></div></td>
      <td><textarea name="description" cols="40" rows="5" wrap="physical"><?php echo $myrow["description"] ?></textarea></td>
    </tr>
  </table>
  <br />
  <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="120"><div align="right"><strong>Contact Email: </strong></div></td>
      <td><input name="email" type="text" id="email" VALUE="<?php echo $myrow["email"] ?>"/></td>
    </tr>
    <tr>
      <td width="120"><div align="right"><strong>Event web page: </strong></div></td>
      <td><input type="text" name="website" size="35" VALUE="<?php echo $myrow["website"] ?>"/></td>
    </tr>
  </table>
  <br />
  <table border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="120"><div align="right"><strong>Display Event: </strong></div></td>
      <td><select name="select">
        <option value="yes" selected="selected">yes</option>
        <option value="no">no</option>
      </select>
      </td>
    </tr>
  </table>
  <p><input type="submit" name="submit" value="submit"> </p>
      
      <input type="hidden" name="cmd" value="edit">
</form>
</div>
  
<?php }

   if ($_POST["submit"])   {
   $date = $_POST["date"];
   $remove = $_POST["remove"];
   $chapter = $_POST["chapter"];
   $website = $_POST["website"];
   $title = $_POST["title"];
   $location = $_POST["location"];
   $address = $_POST["address"];
   $city = $_POST["city"];
   $state = $_POST["state"];
   $zip = $_POST["zip"];
   $description = $_POST["description"];
   $email = $_POST["email"];
  
   $title = addslashes($title);
   $description = addslashes($description);
  $location = addslashes($location);
      
      $sql = "UPDATE calendar SET
       date='$date',
       type='$type',
       chapter='$chapter',
       website='$website',
       title='$title',
       location='$location',
       address='$address',
       city='$city',
       state='$state',
       zip='$zip',
       description='$description',
       email='$email',
          display='$display',
       remove='$remove'
       WHERE id='$id'";
      
      //replace event with info from above
         if (@mysql_query($sql)) {
     echo '<p class="notice">This event has been updated. Way to go, champ!</p>';
   } else {
     echo '<p class="notice">Error adding submitted event: ' .
         mysql_error() . '</p><p class="notice">Please contact our web support.</p>';
   }
          $result = mysql_query($sql);
    }
}[/code]

Am I missing something completely obvious here?
Link to comment
https://forums.phpfreaks.com/topic/5784-editing-database-info/
Share on other sites

Your variable $id is only specified if submit is not set. Therefore, your last query has no value for $id.
Change the top of your script to this:
[code]if($_GET['cmd']=="edit" || $_POST["cmd"]=="edit") {
   $id = $_GET["id"];
   if (!isset($_POST["submit"]))   {
   $sql = "SELECT * FROM `calendar` WHERE id=$id";[/code]

Link to comment
https://forums.phpfreaks.com/topic/5784-editing-database-info/#findComment-20611
Share on other sites

[!--quoteo(post=358283:date=Mar 25 2006, 11:34 AM:name=annihilate)--][div class=\'quotetop\']QUOTE(annihilate @ Mar 25 2006, 11:34 AM) [snapback]358283[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Your variable $id is only specified if submit is not set. Therefore, your last query has no value for $id.
Change the top of your script to this:
[code]if($_GET['cmd']=="edit" || $_POST["cmd"]=="edit") {
   $id = $_GET["id"];
   if (!isset($_POST["submit"]))   {
   $sql = "SELECT * FROM `calendar` WHERE id=$id";[/code]
[/quote]

Tried...didn't work. Still gives me the echo statement..."This event has been updated", but still not actually updating the database.
Link to comment
https://forums.phpfreaks.com/topic/5784-editing-database-info/#findComment-20619
Share on other sites

[!--quoteo(post=358302:date=Mar 25 2006, 12:06 PM:name=annihilate)--][div class=\'quotetop\']QUOTE(annihilate @ Mar 25 2006, 12:06 PM) [snapback]358302[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Echo out the value of $sql to the browser and see if it contains the correct value for id.
[/quote]
As is...just got this...
[code]UPDATE calendar SET date='2006-03-01', type='', chapter='No Chapter Currently Listed', website='http://', title='Jon B', location='', address='', city='', state='', zip='', description='No Description', email='', display='', remove='2006-03-07' WHERE id=''

This event has been updated. Way to go, champ![/code]

Removed the quotes from $id to make this....
[code]      $sql = "UPDATE calendar SET
          date='$date',
       type='$type',
       chapter='$chapter',
       website='$website',
       title='$title',
       location='$location',
       address='$address',
       city='$city',
       state='$state',
       zip='$zip',
       description='$description',
       email='$email',
          display='$display',
       remove='$remove'
       WHERE id=$id";
      
       echo $sql;
      //replace event with info from above
[/code]

And got this...[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]UPDATE calendar SET date='2006-03-01', type='', chapter='No Chapter Currently Listed', website='http://', title='Boxing Day', location='', address='', city='', state='', zip='', description='No Description', email='', display='', remove='2006-03-07' WHERE id=

Error adding submitted event: You have an error in your SQL syntax near '' at line 16

Please contact our web support.[/quote]
Do note though...the title is the updated title I entered in. So it's reading that much correctly.

Got it to work with this pretty little puppy right here! Thanks for all your help man.

$id = $_POST['id'];
Link to comment
https://forums.phpfreaks.com/topic/5784-editing-database-info/#findComment-20639
Share on other sites

Think I have a smiilar problem, but can't figure it out from the past posts....

I'm trying to update info in my database, but the stuff isnt actually updating.

Here's my code:

[code]<?session_start();

include 'db.inc.php';

if (isset($_POST['forename'])):

$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$password = $_POST['password'];
$jsid = $_POST['jsid'];

$sql = "UPDATE jobseekers SET
          forename='$forename',
          surname='$surname'
          WHERE jsid='$jsid'";


  if (mysql_query($sql))
  {
    echo '<p>Your details have been updated.</p>';
  } else
  {
    echo '<p>Error updating details: ' .
        mysql_error() . '</p>';
  }

  else:

  $jsid = $_GET['jsid'];
  $jobseeker = mysql_query(
      "SELECT forename, surname, email, password FROM jobseekers, jslogin WHERE jobseekers.jsid='$jsid'");
  if (!$jobseeker)
  {
    exit('<p>Error fetching jobseeker details: ' .
        mysql_error() . '</p>');
  }

  $jobseeker = mysql_fetch_array($jobseeker);

  $forename = $jobseeker['forename'];
  $surname = $jobseeker['surname'];
  $email = $jobseeker['email'];
  $password = $jobseeker['password'];

  // Convert special characters for safe use
  // as HTML attributes.
  $forename = htmlspecialchars($forename);
  $surname = htmlspecialchars($surname);
  $email = htmlspecialchars($email);
  $password = htmlspecialchars($password);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Edit jobseeker details:</p>
<label>Forename: <input type="text" name="forename" value="<?php echo $forename; ?>" /></label><br />
<label>Surname: <input type="text" name="surname" value="<?php echo $surname; ?>" /></label><br />

<label>Email: <input type="text" name="email" value="<?php echo $email; ?>" /></label><br />
<input type="hidden" name="jsid" value="<?php echo $jsid; ?>" />
<input type="submit" value="SUBMIT" /></p>
</form>

<?php endif; ?>

</body>
</html>[/code]

I'm getting the message that details *have* been updated, but when I check the database, nothing has been.

Is probably something really simple, but I just can't see it! If anyone can shed some light it'd be much appreciated.
Link to comment
https://forums.phpfreaks.com/topic/5784-editing-database-info/#findComment-21283
Share on other sites

Are you remembering to pass it as a variable in the url. ie on first load of the page, it should be yourpage.php?jsid=7 for example. Then the hidden form variable for jsid will have the value of 7. And then on submission of the form, the update query should have the value of 7 for $jsid.
Link to comment
https://forums.phpfreaks.com/topic/5784-editing-database-info/#findComment-21291
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • 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.