Jump to content

[SOLVED] Need some help with Bookstore administration page


sstoveld

Recommended Posts

Your not using title_id in your query so why do you need it?

 

i need it for the front end of the bookstore that displays a list of all the titles with their prices and quantities. if its not inserted with it, it wont show the title in the front end table

Just call mysql_insert_id() after the inserts are done.

 

oh

 

$title_id = mysql_insert_id();

 

like that?

 

edit:

 

oh nvm, i got it thanks :P

 

$sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";

 

that part is working now thanks

 

now i gotta make it so the user can edit the quantity and price of the existing titles in the db

i thought i'd try it myself but it seems i'm lost without help lol.

 

here's what the table currently looks like:

jkjuiuy.jpg

 

i need it to have all the price and quantity fields replaced with forms that the user can edit, then select the radio button for which one he wants to update, then hit the submit button for it to update it

 

here's my current pastebin:

http://pastebin.com/m2461827e

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">
<div id="banner">
    	<h1><center><a href="index.php">Bookstore Administration</a></center></h1>
    </div><!-- banner -->
    
  <div id="mainContent">
  	
    
    <?php	
echo "<pre>",print_r($_POST),"</pre>";
////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', '*******') or die ('I cannot connect to the database because: ' .mysql_error());

///////// CONNECT TO DATABASE ////////////
mysql_select_db('dbase18', $dbcnx);
if (!@mysql_select_db('dbase18')) {
	exit ('<p>Error in query.'. mysql_error(). '<p>');
}

/////////// FIRST SQL QUERY ///////////

if (isset($_POST['update'])){
	$books_id=$_POST['books_id'];
	$title = htmlentities($_POST['title']);
	$sql1 = "INSERT INTO books SET title='$title'";
	if (!mysql_query($sql1)){
		echo "<p>Error updating. ".mysql_error()."</p>";
	}
}

/////////// GET PRIMARY KEY FROM PREVIOUS QUERY /////////
$title_id = mysql_insert_id();	
////////// SECOND SQL QUERY //////////
if (isset($_POST['update'])){
	$title=$_POST['title'];
	$stock_id=$_POST['stock_id'];
	$quantity = htmlentities($_POST['quantity']);
	$price = htmlentities($_POST['price']);
	$sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";
	if (!mysql_query($sql2)) {
		echo "<p>Error updating. ".mysql_error()." $sql2</php>";
	}else{
		echo "<p>Updated successfully.</p>";
	}
}

////////// QUERY DATABASE //////////
$queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
if (!$queryresult) {
	exit ('<p>Error in query.'. mysql_error().'</p>');
} 


///////// MAKE A TABLE CONTAINING FORMS FOR USER TO ADD NEW TITLES //////////
?>
    
    <p>
    <h2>Add Titles</h2>
    <table>
    <tr>
    	<td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <tr>
    	<td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    	<td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    	<td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    	<td><input name="update" type="submit" value="update"></td>
    </tr>
    </form>
    </table>
    </p>
    
    <p>
    <h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
    	<td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
    	while ($row = mysql_fetch_array($queryresult))
        	{
			echo('<tr><td>' . $row['title'] . '</td><td>' . $row['quantity'] . '</td><td>' . $row['price'] . '</td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
?>
    <tr><td colspan="4"><input type="submit" value="Submit" name="submit" /></td></tr>
    </form>
    </table>
    </p>
    
    
  </div><!-- mainContent -->
    
    <div id="footer">
    	<p>Coded and designed by: <a href="mailto:[email protected]">Steve Stoveld</a></p>
    </div><!-- footer -->
</div><!-- wrapper -->

</body>
</html>

 

im not sure how to fit it in there :/

ok ive got just about everything working now, except when i update the prices for the stock and quantities, it's taking them from the wrong place. it seems to be taking it from the last title in the list eg: Watchmen

 

pastebin: http://pastebin.com/m3a604c

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">
<div id="banner">
    	<h1><center><a href="index.php">Bookstore Administration</a></center></h1>
    </div><!-- banner -->
    
  <div id="mainContent">
  	
    
    <?php	
////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', '*******') or die ('I cannot connect to the database because: ' .mysql_error());

///////// CONNECT TO DATABASE ////////////
mysql_select_db('dbase18', $dbcnx);
if (!@mysql_select_db('dbase18')) {
	exit ('<p>Error in query.'. mysql_error(). '<p>');
}

/////////// FIRST SQL QUERY ///////////

if (isset($_POST['update'])){
	$books_id=$_POST['books_id'];
	$title = htmlentities($_POST['title']);
	$sql1 = "INSERT INTO books SET title='$title'";
	if (!mysql_query($sql1)){
		echo "<p>Error updating. ".mysql_error()."</p>";
	}
}

/////////// GET PRIMARY KEY FROM PREVIOUS QUERY /////////
$title_id = mysql_insert_id();	
////////// SECOND SQL QUERY //////////
if (isset($_POST['update'])){
	$title=$_POST['title'];
	$stock_id=$_POST['stock_id'];
	$quantity = htmlentities($_POST['quantity']);
	$price = htmlentities($_POST['price']);
	$sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";
	if (!mysql_query($sql2)) {
		echo "<p>Error updating. ".mysql_error()." $sql2</php>";
	}else{
		echo "<p>Updated successfully.</p>";
	}
}

////////// QUERY DATABASE //////////
$queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
if (!$queryresult) {
	exit ('<p>Error in query.'. mysql_error().'</p>');
} 


///////// MAKE A TABLE CONTAINING FORMS FOR USER TO ADD NEW TITLES //////////
?>
    
    <p>
    <h2>Add Titles</h2>
    <table>
    <tr>
    	<td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <tr>
    	<td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    	<td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    	<td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    	<td><input name="update" type="submit" value="update"></td>
    </tr>
    </form>
    </table>
    </p>
    
    <p>
    <h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
    	<td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php

echo "<pre>",print_r($_POST),"</pre>";
/////////// QUERY ////////////////////
if (isset($_POST['subsend'])){
	$title=$_POST['title'];
	$stock_id=$_POST['stock_id'];
	$quantity = htmlentities($_POST['quantity']);
	$price = htmlentities($_POST['price']);
	$sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
	if (!mysql_query($sql3)) {
		echo "<p>Error updating. ".mysql_error()." $sql3</php>";
	}else{
		echo "<p>Updated successfully.</p>";
	}
}
    	while ($row = mysql_fetch_array($queryresult))
        	{
			echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
?>
    <tr><td colspan="4"><input type="submit" value="Submit" name="subsend" /></td></tr>
    </form>
    </table>
    </p>
    
    
  </div><!-- mainContent -->
    
    <div id="footer">
    	<p>Coded and designed by: <a href="mailto:[email protected]">Steve Stoveld</a></p>
    </div><!-- footer -->
</div><!-- wrapper -->

</body>
</html>

 

qwertyl.jpg

 

its taking the values from the 2 fields from Watchmen, instead of the ones from other titles

sorry for the spam, but i cant edit my last post...

 

but for instance, if i put in a quantity of 1 and a price of 1.00 in the Watchmen row, and selected the radio button for Linked, it would update the quantity and price for Linked with the values for Watchmen instead of the values for linked

let all understand this.

 

are you saying that , when you use this code it updating but in reverse order.

 

so if you update two fields it does it but backwards putting the info in but in the wrong fields reverse,

 

or in fields but not the correct ones.

 

<h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
   
   echo "<pre>",print_r($_POST),"</pre>";
   /////////// QUERY ////////////////////
   if (isset($_POST['subsend'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
      if (!mysql_query($sql3)) {
         echo "<p>Error updating. ".mysql_error()." $sql3</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
       while ($row = mysql_fetch_array($queryresult))
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>

let all understand this.

 

are you saying that , when you use this code it updating but in reverse order.

 

so if you update two fields it does it but backwards putting the info in but in the wrong fields reverse.

 

<h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
   
   echo "<pre>",print_r($_POST),"</pre>";
   /////////// QUERY ////////////////////
   if (isset($_POST['subsend'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
      if (!mysql_query($sql3)) {
         echo "<p>Error updating. ".mysql_error()." $sql3</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
       while ($row = mysql_fetch_array($queryresult))
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>

 

well no, whatever i have in the 2 fields for watchmen, those values will be put into whatever radio button i select.

 

so say i put 1 and 1.00 into the quantity and price fields for LINKED, and put 99 and 99.99 in the quantity and price fields for WATCHMEN, and select the radio button for LINKED, LINKED will be updated to 99 and 99.99. it takes whatever is in the fields for WATCHMEN, and puts it into whatever title i select, whichever radio button i select. i cant figure out why

this select statement is the problam i think you need to create another

 

then update.

 

 

 

i say that because all the variables are correct.

 

only conclusion  i got as the while loop ends after the update statement (( must be that i am guessing here))

 

or i mean the while loop used is on a join,you only need the stock i d am i right.

 

$queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
   if (!$queryresult) {
      exit ('<p>Error in query.'. mysql_error().'</p>');
   }

while ($row = mysql_fetch_array($queryresult)) <<< i mean this why are you selecting this and not selecting what in the databse cheek your database it is updating.

 

your selecting old info with new updated info.

 

i might be wrong but as you post cheek the database for the correct figures do they really go into the correct database fields.

 

would that do anything different i don't no so always back up.

 

it a complete guess.

 

<?php
$sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='{$queryresult['stock_id']}';";
?>

while ($row = mysql_fetch_array($queryresult)) <<< i mean this why are you selecting this and not selecting what in the databse cheek your database it is updating.

 

your selecting old info with new updated info.

 

 

would that do anything different i don't no so always back up.

 

it a complete guess.

 

<?php
$sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='{$queryresult['stock_id']}';";
?>

 

no go man :(

let me explain properly, if i use a join to collect the info i need,,  then i need to update via that join, and out put info with a new select to show the new updated info.

 

your selecting the joined info not the new collected info you dont  need to use a join will you.

 

get mixed results.

 

i might be completely wrong sorry if i am.

let me explain properly, if i use a join to collect the info i need,,  then i need to update via that join, and out put info with a new select to show the new updated info.

 

your selecting the joined info not the new collected info you dont  need to use a join will you.

 

get mixed results.

im sorry im not following you on that. i dont understand.

your currenly showing the join sql

 

  while ($row = mysql_fetch_array($queryresult))<<<< goes to a join
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>

  while ($row = mysql_fetch_array($queryresult))<<<< goes to a join

it selecting this

   $queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
   if (!$queryresult) {
      exit ('<p>Error in query.'. mysql_error().'</p>');
   } 
      

that was to get the info to curretly update the database ....

 

 

you need a new select to show what in the database now...

 

 

your not showing what in the current database that was updated.

 

might be wrong

your currenly showing the join sql

 

  while ($row = mysql_fetch_array($queryresult))<<<< goes to a join
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>

 

your not showing what in the current database that was updated.

 

might be wrong

 

oh i think i understand what you're saying.

 

how do i show the updated database?

trie this please always update info

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">
   <div id="banner">
       <h1><center><a href="index.php">Bookstore Administration</a></center></h1>
    </div><!-- banner -->
   
  <div id="mainContent">
     
   
    <?php   
   ////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', '*******') or die ('I cannot connect to the database because: ' .mysql_error());

   ///////// CONNECT TO DATABASE ////////////
   mysql_select_db('dbase18', $dbcnx);
   if (!@mysql_select_db('dbase18')) {
      exit ('<p>Error in query.'. mysql_error(). '<p>');
   }
   
   /////////// FIRST SQL QUERY ///////////
   
   if (isset($_POST['update'])){
      $books_id=$_POST['books_id'];
      $title = htmlentities($_POST['title']);
      $sql1 = "INSERT INTO books SET title='$title'";
      if (!mysql_query($sql1)){
         echo "<p>Error updating. ".mysql_error()."</p>";
      }
   }
   
   /////////// GET PRIMARY KEY FROM PREVIOUS QUERY /////////
   $title_id = mysql_insert_id();   
   ////////// SECOND SQL QUERY //////////
   if (isset($_POST['update'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";
      if (!mysql_query($sql2)) {
         echo "<p>Error updating. ".mysql_error()." $sql2</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
   
   ////////// QUERY DATABASE //////////
   $queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
   if (!$queryresult) {
      exit ('<p>Error in query.'. mysql_error().'</p>');
   } 
      
   
   ///////// MAKE A TABLE CONTAINING FORMS FOR USER TO ADD NEW TITLES //////////
   ?>
   
    <p>
    <h2>Add Titles</h2>
    <table>
    <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <tr>
       <td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="update" type="submit" value="update"></td>
    </tr>
    </form>
    </table>
    </p>
   
    <p>
    <h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
   
   echo "<pre>",print_r($_POST),"</pre>";
   /////////// QUERY ////////////////////
   if (isset($_POST['subsend'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
      if (!mysql_query($sql3)) {
         echo "<p>Error updating. ".mysql_error()." $sql3</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
   
   $updated_echo = @mysql_query('SELECT * FROM stock');
   
       while ($row = mysql_fetch_array($updated_echo))
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>
    <tr><td colspan="4"><input type="submit" value="Submit" name="subsend" /></td></tr>
    </form>
    </table>
    </p>
   
   
  </div><!-- mainContent -->
   
    <div id="footer">
       <p>Coded and designed by: <a href="mailto:[email protected]">Steve Stoveld</a></p>
    </div><!-- footer -->
</div><!-- wrapper -->

</body>
</html>

trie this please always update info

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">
   <div id="banner">
       <h1><center><a href="index.php">Bookstore Administration</a></center></h1>
    </div><!-- banner -->
   
  <div id="mainContent">
     
   
    <?php   
   ////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', '*******') or die ('I cannot connect to the database because: ' .mysql_error());

   ///////// CONNECT TO DATABASE ////////////
   mysql_select_db('dbase18', $dbcnx);
   if (!@mysql_select_db('dbase18')) {
      exit ('<p>Error in query.'. mysql_error(). '<p>');
   }
   
   /////////// FIRST SQL QUERY ///////////
   
   if (isset($_POST['update'])){
      $books_id=$_POST['books_id'];
      $title = htmlentities($_POST['title']);
      $sql1 = "INSERT INTO books SET title='$title'";
      if (!mysql_query($sql1)){
         echo "<p>Error updating. ".mysql_error()."</p>";
      }
   }
   
   /////////// GET PRIMARY KEY FROM PREVIOUS QUERY /////////
   $title_id = mysql_insert_id();   
   ////////// SECOND SQL QUERY //////////
   if (isset($_POST['update'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";
      if (!mysql_query($sql2)) {
         echo "<p>Error updating. ".mysql_error()." $sql2</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
   
   ////////// QUERY DATABASE //////////
   $queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
   if (!$queryresult) {
      exit ('<p>Error in query.'. mysql_error().'</p>');
   } 
      
   
   ///////// MAKE A TABLE CONTAINING FORMS FOR USER TO ADD NEW TITLES //////////
   ?>
   
    <p>
    <h2>Add Titles</h2>
    <table>
    <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <tr>
       <td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="update" type="submit" value="update"></td>
    </tr>
    </form>
    </table>
    </p>
   
    <p>
    <h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
   
   echo "<pre>",print_r($_POST),"</pre>";
   /////////// QUERY ////////////////////
   if (isset($_POST['subsend'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
      if (!mysql_query($sql3)) {
         echo "<p>Error updating. ".mysql_error()." $sql3</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
   
   $updated_echo = @mysql_query('SELECT * FROM stock');
   
       while ($row = mysql_fetch_array($updated_echo))
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>
    <tr><td colspan="4"><input type="submit" value="Submit" name="subsend" /></td></tr>
    </form>
    </table>
    </p>
   
   
  </div><!-- mainContent -->
   
    <div id="footer">
       <p>Coded and designed by: <a href="mailto:[email protected]">Steve Stoveld</a></p>
    </div><!-- footer -->
</div><!-- wrapper -->

</body>
</html>

 

nah it didnt work :(

when you look at the stock table from the  database is it getting the correct info yes or no.

 

dont look at the script or browser look at php myadmin .....

 

when you update are the write fields getting updated correctly please.

 

ive looked at your code and the update must be going in correctly.

 

when you look at the stock table from the  database is it getting the correct info yes or no.

 

dont look at the script or browser look at php myadmin .....

 

when you update are the write fields getting updated correctly please.

 

ive looked at your code and the update must be going in correctly.

 

the stock table from the database is reflecting the same info as the browser is showing

 

what all the ids for in hidden form

       <td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="update" type="submit" value="update"></td>

 

what all the ids for in hidden form

       <td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="update" type="submit" value="update"></td>

 

im actually not sure lol, i re-used some old code and might have overlooked that. should i get rid of all of that? or not? im afraid to break this lol

 

update and post what comes on the browser.

 

remember the values you just updated please...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">
   <div id="banner">
       <h1><center><a href="index.php">Bookstore Administration</a></center></h1>
    </div><!-- banner -->
   
  <div id="mainContent">
     
   
    <?php   
   ////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', '*******') or die ('I cannot connect to the database because: ' .mysql_error());

   ///////// CONNECT TO DATABASE ////////////
   mysql_select_db('dbase18', $dbcnx);
   if (!@mysql_select_db('dbase18')) {
      exit ('<p>Error in query.'. mysql_error(). '<p>');
   }
   
   /////////// FIRST SQL QUERY ///////////
   
   if (isset($_POST['update'])){
      $books_id=$_POST['books_id'];
      $title = htmlentities($_POST['title']);
      $sql1 = "INSERT INTO books SET title='$title'";
      if (!mysql_query($sql1)){
         echo "<p>Error updating. ".mysql_error()."</p>";
      }
   }
   
   /////////// GET PRIMARY KEY FROM PREVIOUS QUERY /////////
   $title_id = mysql_insert_id();   
   ////////// SECOND SQL QUERY //////////
   if (isset($_POST['update'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";
      if (!mysql_query($sql2)) {
         echo "<p>Error updating. ".mysql_error()." $sql2</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
   
   ////////// QUERY DATABASE //////////
   $queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
   if (!$queryresult) {
      exit ('<p>Error in query.'. mysql_error().'</p>');
   } 
      
   
   ///////// MAKE A TABLE CONTAINING FORMS FOR USER TO ADD NEW TITLES //////////
   ?>
   
    <p>
    <h2>Add Titles</h2>
    <table>
    <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <tr>
       <td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="update" type="submit" value="update"></td>
    </tr>
    </form>
    </table>
    </p>
   
    <p>
    <h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
   
   echo "<pre>",print_r($_POST),"</pre>";
   /////////// QUERY ////////////////////
   if (isset($_POST['subsend'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
      if (!mysql_query($sql3)) {
         echo "<p>Error updating. ".mysql_error()." $sql3</php>";
      }else{
         echo "<p>Updated successfully.</p>";
         
         echo $sql3;
         
        exit;
      }
   }
   

   
       while ($row = mysql_fetch_array( $queryresult))
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>
    <tr><td colspan="4"><input type="submit" value="Submit" name="subsend" /></td></tr>
    </form>
    </table>
    </p>
   
   
  </div><!-- mainContent -->
   
    <div id="footer">
       <p>Coded and designed by: <a href="mailto:[email protected]">Steve Stoveld</a></p>
    </div><!-- footer -->
</div><!-- wrapper -->

</body>
</html>

 

update and post what comes on the browser.

 

remember the values you just updated please...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administration</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div id="wrapper">
   <div id="banner">
       <h1><center><a href="index.php">Bookstore Administration</a></center></h1>
    </div><!-- banner -->
   
  <div id="mainContent">
     
   
    <?php   
   ////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', '*******') or die ('I cannot connect to the database because: ' .mysql_error());

   ///////// CONNECT TO DATABASE ////////////
   mysql_select_db('dbase18', $dbcnx);
   if (!@mysql_select_db('dbase18')) {
      exit ('<p>Error in query.'. mysql_error(). '<p>');
   }
   
   /////////// FIRST SQL QUERY ///////////
   
   if (isset($_POST['update'])){
      $books_id=$_POST['books_id'];
      $title = htmlentities($_POST['title']);
      $sql1 = "INSERT INTO books SET title='$title'";
      if (!mysql_query($sql1)){
         echo "<p>Error updating. ".mysql_error()."</p>";
      }
   }
   
   /////////// GET PRIMARY KEY FROM PREVIOUS QUERY /////////
   $title_id = mysql_insert_id();   
   ////////// SECOND SQL QUERY //////////
   if (isset($_POST['update'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql2 = "INSERT INTO stock SET title_id='$title_id', quantity='$quantity', price='$price';";
      if (!mysql_query($sql2)) {
         echo "<p>Error updating. ".mysql_error()." $sql2</php>";
      }else{
         echo "<p>Updated successfully.</p>";
      }
   }
   
   ////////// QUERY DATABASE //////////
   $queryresult = @mysql_query('SELECT stock_id, title, quantity, price FROM stock LEFT JOIN books ON stock.title_id = books.books_id ORDER BY title;');
   if (!$queryresult) {
      exit ('<p>Error in query.'. mysql_error().'</p>');
   } 
      
   
   ///////// MAKE A TABLE CONTAINING FORMS FOR USER TO ADD NEW TITLES //////////
   ?>
   
    <p>
    <h2>Add Titles</h2>
    <table>
    <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <tr>
       <td><input name="title" type="text" value="<?php echo $title; ?>" size="30" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="4" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="price" type="text" value="<?php echo $price; ?>" size="6" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
       <td><input name="update" type="submit" value="update"></td>
    </tr>
    </form>
    </table>
    </p>
   
    <p>
    <h2>Update quantities and prices</h2>
    <form name="submit" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="post">
    <table>
        <tr>
       <td>Title</td>
        <td>Stock</td>
        <td>Price</td>
        <td>Update</td>
    </tr>
    <?php
   
   echo "<pre>",print_r($_POST),"</pre>";
   /////////// QUERY ////////////////////
   if (isset($_POST['subsend'])){
      $title=$_POST['title'];
      $stock_id=$_POST['stock_id'];
      $quantity = htmlentities($_POST['quantity']);
      $price = htmlentities($_POST['price']);
      $sql3 = "UPDATE stock SET quantity='$quantity', price='$price' where stock_id='$stock_id';";
      if (!mysql_query($sql3)) {
         echo "<p>Error updating. ".mysql_error()." $sql3</php>";
      }else{
         echo "<p>Updated successfully.</p>";
         
         echo $sql3;
         
        exit;
      }
   }
   

   
       while ($row = mysql_fetch_array( $queryresult))
           {
            echo('<tr><td>' . $row['title'] . '</td><td><input type="text" name="quantity" size="4" maxlength="4" value="' . $row['quantity'] . '"/></td><td><label>$<input type="text" name="price" size="6" maxlength="6" value="' . $row['price'] . '" /></label></td><td><input type="radio" name="stock_id" value="' . $row['stock_id'] . '"></td></tr>');
            }
   ?>
    <tr><td colspan="4"><input type="submit" value="Submit" name="subsend" /></td></tr>
    </form>
    </table>
    </p>
   
   
  </div><!-- mainContent -->
   
    <div id="footer">
       <p>Coded and designed by: <a href="mailto:[email protected]">Steve Stoveld</a></p>
    </div><!-- footer -->
</div><!-- wrapper -->

</body>
</html>

 

its still just using the values for the last row for some reason

 

i really appreciate the help, but the deadline has already passed so i had to turn in my assignment without it being fully functional

 

ive gotta get some sleep.

 

thanks again

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.