Jump to content

[SOLVED] Problem with updating information in database using PHP


dennismonsewicz

Recommended Posts

<?php 
include "../includes/header.php";

$action = $_GET['action'];
$rowid = $_GET['id'];

		switch($action) 
		{
			/* MORE DETAILED VIEW */
			case "view":
			include "../includes/db_login.php";

                $query = "SELECT * FROM tblname WHERE rowid = '$rowid'";
                
                $result = mysql_query( $query );
                
                if(!$result)
                    {
                        die("Could not query the database: <br/>" . mysql_error());
                    }
                
                while($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					$rowid = $row['rowid'];
					$company = $row['company'];
					$product = $row['product'];
					$description = $row['description'];
					$web = $row['web'];
					$last = $row['last'];
					$used = $row['used'];
					$active = $row['active'];

						echo '
						<p class="casetext">Below is a more detailed description of <b>' . $product . '</b>.</p>

						<table border="0" align="center" cellpadding="4" style="margin-bottom: 5px;">
								<tr>
									<td>Company:</td>
									<td>' . $company . '</td>
								</tr>
								<tr>
									<td>Product:</td>
									<td>' . $product . '</td>
								</tr>
								<tr>
									<td>Description:</td>
									<td>' . $description . '</td>
								</tr>
								<tr>
									<td>Web Address:</td>
									<td><a href="' . $web . '" target= "_blank">' . $web . '</a></td>
								</tr>
								<tr>
									<td>Last Used:</td>
									<td>' . $last . '</td>
								</tr>
								<tr>
									<td>When was <b>' . $product . '</b> last used:</td>
									<td>' . $used . '</td>
								</tr>
								<tr>
									<td>Is <b>' . $product . '</b> Active or Inactive?:</td>
									<td>' . $active . '</td>
								</tr>
							</table>

						<p class="casetext" style="font-size: 125%;"><a href="hrdb.php">Go back to database?</a></p>

						';

				}


				mysql_close($connection);
			include_once "../includes/footer.php";
			exit;
			break;
			/* END DETAILED VIEW */

			/* EDIT MODE */
			case "edit":
			include "../includes/db_login.php";

                $query = "SELECT * FROM tblname WHERE rowid = '$rowid'";
                
                $result = mysql_query( $query );
                
                if(!$result)
                    {
                        die("Could not query the database: <br/>" . mysql_error());
                    }
                
                while($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					$id = $row['rowid'];
					$company = $row['company'];
					$product = $row['product'];
					$description = $row['description'];
					$web = $row['web'];
					$last = $row['last'];
					$used = $row['used'];
					$active = $row['active'];

						echo '
						<p class="casetext">Below is a more detailed description of <b>' . $product . '</b>.</p>

						<form action="hrdbcase.php?action=doedit" method="post" name="form1">
						<p><input type="hidden" name="rowid" value="' . $id . '" /></p>
						<table border="0" align="center" cellpadding="4" style="margin-bottom: 5px;">
								<tr>
									<td>Company:</td>
									<td><input type="text" name="company" value="' . $company . '" /></td>
								</tr>
								<tr>
									<td>Product:</td>
									<td><input type="text" name="product" value="' . $product . '" /></td>
								</tr>
								<tr>
									<td>Description:</td>
									<td><input type="text" name="description" value="' . $description . '" /></td>
								</tr>
								<tr>
									<td>Web Address:</td>
									<td><input type="text" name="web" value="' . $web . '" /></td>
								</tr>
								<tr>
									<td>Last Used:</td>
									<td><input type="text" name="last" value="' . $last . '" /></td>
								</tr>
								<tr>
									<td>When was <b>' . $product . '</b> last used:</td>
									<td><input type="text" name="used" value="' . $used . '" /></td>
								</tr>
								<tr>
									<td>Is <b>' . $product . '</b> Active or Inactive?:</td>
									<td><input type="text" name="active" value="' . $active . '" /></td>
								</tr>
								<tr>
									<td></td>
									<td><input type="submit" value="Submit" /></td>
								</tr>
							</table>
						</form>
						<p class="casetext" style="font-size: 125%;"><a href="hrdb.php">Go back to database?</a></p>

						';

				}


				mysql_close($connection);
				include "../includes/footer.php";
			exit;
			break;
			/* END EDIT MODE*/

			/* EDIT ACTION HELD HERE */
			case "doedit":	

			include "../includes/db_login.php";		

                $query="
				UPDATE tblname
				 SET
				 	company = '" . $company . "',
					product = '" . $product . "',
					description = '" . $description . "',
					web = '" . $web . "',
					last = '" . $last . "',
					used = '" . $used . "',
					active = '" . $active . "'
				WHERE
					rowid = '" . $rowid . "'
				";

					$result = mysql_query($query);

					if(!$result)
					{
					die("Could not query the database: <br/>" . mysql_error());
					} else {
						echo '<p class="casetext">Item Updated</p>';
						}


					mysql_close($connection); 


				include "../includes/footer.php";
			exit;
			break;
			/* END EDIT ACTION */


			case "delete":
			$id = $_GET['id'];
			echo '<p class="casetext">Are you sure you want to <strong>delete</strong> this item?<br/>';
			echo 'This is a final action. If you make a mistake, this cannot be undone.<br/>';
			echo '<a href="hrdbcase.php?action=dodelete&id=' . $id . '"><big>Yes, Delete This</big></a> | <a href="hrdb.php"><big>No, Go Back</big></a> ';
			echo '</p>';
			include_once "../includes/footer.php";
			exit;
			break;

			case "dodelete":
			include "../includes/db_login.php";
			$query="DELETE FROM hrlanding WHERE rowid='" . $id . "'";

			$result = mysql_query($query);

			if(!$result)
			{
				die("Could not query the database: <br/>" . mysql_error());
				} else {
					echo '<p class="casetext">Item has been permanetly deleted<br/>';
					echo 'To return to the database, <a href="hrdb.php">Click Here</a></p>';
					}

			include_once "../includes/footer.php";
			exit;
			break;			
		} 

include "../includes/footer.php";



?>

 

The following code above is being used to edit/updated/delete an item in a database. Well I can view the information perfectly, but when I try to edit the information, I get a successful message but the edits do not go through. And when you try to delete an item the delete command does not delete the item. I need help URGENTLY! Thanks for any suggestions in advance.

 

-Dennis

Link to comment
Share on other sites

If you are not getting any errors from the SQL statements, then it is most likely your WHERE clauses. For editing, you are checking the $rowid, print out $rowid right before your sql and see if it does, in fact, contain a value that is in your table.

Link to comment
Share on other sites

Try swapping out $rowid with a number that is known to be in the table. I am pretty sure that you shouldn't have any type of quotes when using a number so it would seem to me that $rowid might contain some kind of characters other than numerical. Just test it like this and see if it works for that row:

rowid = 1

Link to comment
Share on other sites

I am pretty sure the problem is that those variables (including $rowid) simply contain nothing. They are all being set in a different case, which means, if the case is edit, the case that sets them is never being executed. You need to either set them in each case, or pass them through using GET or POST so that eahc case can pull the values.

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.