Jump to content

[SOLVED] MySQL Update query


gazfocus

Recommended Posts

I am trying to make my website allow users to update items in a MySQL database (I already have it so they can update their account details and that works fine), but the following code won't work. All the correct form components display, and when I click the final submit, it says "Your details have now been edited!" but it doesn't update the database. Any thoughts? Thanks

 

<?
if ( !isset($_SESSION['authorised']) || $_SESSION['authorised'] != 'yes') 
{
echo "You are not authorised to view this page";
}
else
{
	if (($_POST[viewed] !="yes")  && ($_POST[viewed] != "2"))
	{
	$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("bookshop", $conn) or die(mysql_error());
	$sql = "SELECT * FROM tblbook WHERE userID = '$_SESSION[user]'";
	$result = mysql_query ($sql, $conn);
	echo  "<form id=\"userform\" name=\"form\" method=\"post\" action=\"$_SERVER[php_SELF]\">

					<table>
					<tr>
						<td>
							<label>Book Title</label>
						</td>
  							
						<td>
							<select name = \"bookTitle\">";
							while ($newArray = mysql_fetch_array($result))
								{
								$title = $newArray[bookTitle];
							echo "<option> $title </option>";
							}							
							echo "</select></td>
					</tr>
  								<tr>
							<td>
							<label>
							<input type=\"hidden\" name=\"viewed\" value=\"yes\">
							<input type=\"submit\" name=\"Submit\" value=\"Edit Details!\">
								</label>
						</td>
					</tr>
					</table>

				</form>";
				}
				elseif ($_POST[viewed] != "2")
				{

	$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("bookshop", $conn) or die(mysql_error());
	$sql = "SELECT * FROM tblbook WHERE bookTitle = '$_POST[bookTitle]'";
	$result = mysql_query ($sql, $conn);

		while ($newArray = mysql_fetch_array($result))
	{
	$bookTitle = $newArray[bookTitle];
	$author = $newArray[author];
	$description = $newArray[description];
	$isbn = $newArray[isbn];
	$price = $newArray[price];
	$bookID = $newArray[bookID];
	}


	echo  "<form id=\"userform\" name=\"form\" method=\"post\" action=\"$SERVER[php_SELF]\" onsubmit=\"return validate()\">

					<table>
					<tr>
						<td>
							<label>Book Title
						</td>
  							
						<td>
							<input name=\"booktitle\" type=\"text\" id=\"booktitle\" value=\"$bookTitle\">
							</label>
						</td>
					</tr>
  					
					<tr>	
						<td>
  								<label>Author
						</td>

						<td>
							<input name=\"author\" type=\"text\" id=\"author\" value=\"$author\">
		 						</label>
						</td>
  						
					</tr>

					<tr>	
						<td>
  								<label>Description
						</td>

						<td>
						    <textarea name=\"description\" id=\"description\" cols=\"25\" rows=\"5\" >$description</textarea>
		 						</label>
						</td>
  						
					</tr>

					<tr>	
						<td>
							<label>ISBN Number
						</td>
						<td>
							<input name=\"isbn\" type=\"text\" id=\"isbn\" value=\"$isbn\">
							</label>
						</td>

					<tr>
						<td>
							<label>Price £
						</td>

						<td>
							<input name=\"price\" type=\"text\" id=\"price\" value=\"$price\">
							</label>
						</td>

					</tr>

					<tr>
						<td>
							<input type=\"hidden\" name=\"viewed\" value=\"2\">

							<label>
							<input type=\"submit\" name=\"Submit\" value=\"Edit Book!\">
								</label>
						</td>
					</tr>


				</form>";
}

else

{	
	$user=$_SESSION[user];
	$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("bookshop", $conn) or die(mysql_error());
	$sql = "UPDATE tblbook 
	SET bookTitle='$_POST[bookTitle]',
	author='$_POST[author]',
	description='$_POST[description]', 
	isbn='$_POST[isbn]',
	price='$_POST[price]'
	WHERE bookID = '$_POST[bookID]'";

	mysql_query ($sql,$conn) or die(mysql_error());

	echo "Your details have now been edited!";

}
}


?>

 

 

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.