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
https://forums.phpfreaks.com/topic/104301-solved-mysql-update-query/
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.