Jump to content

[SOLVED] New to PHP and have a Database that needs accessing!!


Recommended Posts

I just started PHP about 10 days ago, and I am stuck on a db operation I want to perform. PLEASE HELP!!  I'm desperate!  :(

 

I'm having two problems:

 

1.  My page reads in a DB containing movie information, essentially it's a DVD purchasing website.

I can access my DB just fine, returning rows from a SELECT statement I have.  But when the rows are returned to the webpage, I need to be able to click on them to see additional information: a DVD cover screenshot, Title, Cast, etc.  i have coded an 'onclick' handler to take me to 'details.php'-the link works (I'm redirected) but I don't know how to pass the information of just the one row to the new web page.

 

2.  On the left side of my page I have listed a couple movie genres to filter the db search results.  They are <div>s that are clickable.  If I click on the "Comedy" div, I need to have it show me all movies listed under the 'comedy' genre.

 

ANY help would be MOST appreciated....I'll donate to you my spleen if I have to!  :)

 

Let me know if I need to better explain my problems.  Thanks!

 

 

Here is my code for the main page:

 

<?php

 

if ($_POST[submit] == "Go!")

{

 

//Collect Form Data

 

$string = $_POST['SearchName'];

//echo $string;

//Establish data connection

 

$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\wamp\www\3350dvd\DVD_shoppingCart.mdb','','');

 

//Issue SQL SELECT Statement

 

$sql = "SELECT * FROM MovieInventory WHERE Title LIKE '%$string%'";

 

$rs = odbc_exec($conn, $sql);

}

 

?>

 

 

<!DOCTYPE html PUBLIC "-//W3C/DTD/XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

 

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Assignment 3-5</title>

 

 

<script type="text/javascript" src="3350dvd\jscript.js"></script>

 

 

 

<style>

 

body {margin:15px;font:10pt Verdana}

 

input,textarea{border:0px}

 

</style>

 

</head>

 

<body>

 

<form action="Assignment3-5.php" method="post">

<div id="top" style="background-color:Gray; color:White; width:100%; height:60px;"><h1><center>Assignment 3-5 - DVD shopping</center></h1>

</div>

 

 

<div style="background-color:Gray; color:White; width:140px; height:300px">

<table>

<tr>

  <td colspan="2"><b>Movie Categories:</b></td>

</tr>

<tr>

  <td><div name="Comedy" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="genre(this)">Comedy<div></td><td></td>

</tr>

<tr>

  <td><div name="Cult" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="genre(this)">Cult<div></td>

</tr>

  <tr>

  <td><div name="Horror" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="genre(this)">Horror</div></td>

</tr>

  <tr>

  <td><div name="Romance" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="genre(this)">Romance</div></td>

</tr>

  <tr>

  <td><div name="Sci-Fi" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="genre(this)">Sci-Fi</div></td>

</tr>

  <tr>

  <td> </td>

</tr>

<tr>

  <td><b>Search For:</b></td>

</tr>

<tr>

  <td><input type="text" size="15" name="SearchName"/></td>

</tr>

<tr>

  <td><input type="submit" value="Go!" name="submit"/></td>

</tr>

</table>

</div>

 

 

 

<div style="position:relative; top:-290px; left:150px;">

 

<?php

if(!empty($_POST))

{

$results = 0;

 

echo "<table><tr style='background-color:Gray; color:White'>";

echo "<td>Item Number</td><td>Title</td><td>Actors</td><td>Genre</td><td>Year</td><td>Price</td></tr>";

 

while($row = odbc_fetch_array($rs))

{

echo "<tr onclick=\"location.href='details.php'\" onmouseover='rowMouse(this)' onmouseout='rowMouseOut(this)'>";

echo "<td>".$row['ID']."</td>";

echo "<td>".$row['Title']."</td>";

echo "<td>".$row['Actors']."</td>";

echo "<td>".$row['Genre']."</td>";

echo "<td>".$row['YearReleased']."</td>";

echo "<td>$".number_format($row['Price'],2)."</td>";

echo "</tr>";

$results += 1;

}

echo "</table>";

 

if ($results == 0)

{

echo "No records found!";

}

 

odbc_close($conn);

}

 

?>

 

</div>

</form>

</body>

</html>

Pass the records ID (a unique ID) to the details.php like this

details.php?id=123

the 123 is the ID, in the details.php file use

$ID = $_GET['id']
//$ID will equal 123 now

to get that id, then use that to search the database again for the rest of the details :)

Ok, so I have another question.  I'm using the ___.php?id"" tool you told me about, but...

Now, from detailsl.php, I am using the 'cart.php?id=""; to pass information to my shopping cart.  But I appended the end of 'id' with ADD, so that when the "add to cart" button is clicked, the movie ID number along with ADD is passed to cart.php.  Then in cart.php I search the id string to make sure that ADD is in there, if so, I add the movie to the shopping cart and database. But everytime I refresh the shopping cart page, another copy of the movie is added to the shopping cart.  I know that this is because the page posts back and the id that was passed still has "ADD" on the end, so it executes the add sequence. 

 

Question is how can I pass the movie id number to cart.php and have cart.php only add a new record to the shopping cart when the 'add to cart' button on details.php is clicked?

 

I've included the code for both details.php and cart.php below!

 

Thanks SOOOOOO much for any input/ideas you can help me with

 

 

Here is details.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>DVD Details</title>

<script type="text/javascript" src="3350dvd\jscript.js"></script>

<style>

body {margin:15px;font:10pt Verdana}

input,textarea{border:0px}

</style>

</head>
<body>

<div id="top" style="background-color:Gray; color:White; width:100%; height:60px;"><h1><center>Assignment 3-5 - DVD shopping</center></h1>
</div>

<div style="background-color:Gray; color:White; width:140px; height:300px">
<table style="color:White; font-size:10pt">
<tr>
  <td colspan="2"><b>Movie Categories:</b></td>
</tr>
<tr>
  <td><div name="Comedy" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Comedy'">Comedy<div></td><td></td>
</tr>
<tr>
  <td><div name="Cult" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Cult'">Cult<div></td>
</tr>
  <tr>
  <td><div name="Horror" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Horror'">Horror</div></td>
</tr>
  <tr>
  <td><div name="Romance" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Romance'">Romance</div></td>
</tr>
  <tr>
  <td><div name="Sci-Fi" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Sci-Fi'">Sci-Fi</div></td>
</tr>
  <tr>
  <td> </td>
</tr>
<tr>
  <td><b>Search For:</b></td>
</tr>
<tr>
  <td><input type="text" size="15" name="SearchName"/></td>
</tr>
<tr>
  <td><input type="submit" value="Go!" name="submit"/></td>
</tr>
</table>
</div>

<?php
$ID = $_GET['id'];

$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\wamp\www\3350dvd\DVD_shoppingCart.mdb','','');
$sql = "SELECT * FROM MovieInventory WHERE ID = '$ID'";
$rs = odbc_exec($conn, $sql);

$sqlGen = "SELECT * FROM MovieInventory WHERE Genre = '$ID'";
$genre = odbc_exec($conn, $sqlGen);

?>

<div style='position:relative; top:-290px; left:150px'">

<?php

if($ID == "Romance" || $ID == "Sci-Fi" || $ID == "Cult" || $ID == "Horror" || $ID == "Comedy")
{
echo "<table style='font-size:10pt'>"; 
echo "<tr style='background-color:Gray; color:White'><td>Item Number</td><td>Title</td><td>Actors</td><td>Genre</td><td>Year</td><td>Price</td></tr>";

while($row = odbc_fetch_array($genre))
{
echo "<tr onclick=\"location.href='details.php?id=".$row['ID']."'\" onmouseover='rowMouse(this)' onmouseout='rowMouseOut(this)'>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Title']."</td>";
echo "<td>".$row['Actors']."</td>";
echo "<td>".$row['Genre']."</td>";
echo "<td>".$row['YearReleased']."</td>";
echo "<td>$".number_format($row['Price'],2)."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "<table>";
  while($row = odbc_fetch_array($rs))
  {
$tempID = $row['ID'];
   echo "<tr><td rowspan='6'><img src=\"3350dvd\pictures\\".$row['ID'].".jpg\" alt=".$row['Title']."/></td></tr>"; 
   echo "<td style='padding-left:10px'>Title: <b style=\"font-size:14pt\">".$row['Title']."</b></td>";
   echo "<tr><td style='padding-left:10px'>Cast: ".$row['Actors']."</td></tr>";
   echo "<tr><td style='padding-left:10px'>Genre: ".$row['Genre']."</td></tr>";
   echo "<tr><td style='padding-left:10px'>Year: ".$row['YearReleased']."</td></tr>";
   if($row['Price'] < 10)
    echo "<tr><td style='padding-left:10px'>Price: <b style='font-size:12pt; font-weight:bold; color:red;'>$".number_format($row['Price'],2)."</b></td></tr>";
   else
    echo "<tr><td style='padding-left:10px'>Price: $".number_format($row['Price'],2)."</td></tr>";
  	echo "<tr><td colspan='2' style=\"font-size:8pt\">Inventory Number: ".$row['ID']."</td></tr>";
}
  echo "</table>";  
echo "<br/>    <input type='submit' value='Add to Cart' name='AddToCart' style='background-color:Gray; color:White' onclick=\"location.href='cart.php?id=".$tempID."ADD'\"/>";
}
?>

</div>

</body>
</html>

 

And here is cart.php:




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Shopping Cart</title>

<script language="JavaScript" type="text/javascript" src="3350dvd\jscript.js">
</script>


<style type="text/css">
<!--
body {margin:15px;font:10pt Verdana}

input,textarea{border:0px}
-->
</style>


</head>
<body>

<div id="top" style="background-color:Gray; color:White; width:100%; height:60px;"><h1><center>Assignment 3-5 - DVD shopping</center></h1>
</div>

<div style="background-color:Gray; color:White; width:140px; height:300px">
<table style="color:White; font:10pt Verdana">
<tr>
  <td colspan="2"><b>Movie Categories:</b></td>
</tr>
<tr>
  <td><div name="Comedy" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Comedy'">Comedy<div></td><td></td>
</tr>
<tr>
  <td><div name="Cult" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Cult'">Cult<div></td>
</tr>
  <tr>
  <td><div name="Horror" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Horror'">Horror</div></td>
</tr>
  <tr>
  <td><div name="Romance" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Romance'">Romance</div></td>
</tr>
  <tr>
  <td><div name="Sci-Fi" onmouseover="OverMouse(this)" onmouseout="OutMouse(this)" onclick="location.href='details.php?id=Sci-Fi'">Sci-Fi</div></td>
</tr>
  <tr>
  <td> </td>
</tr>
<tr>
  <td><b>Search For:</b></td>
</tr>
<tr>
  <td><input type="text" size="15" name="SearchName"/></td>
</tr>
<tr>
  <td><input type="submit" value="Go!" name="submit"/></td>
</tr>
</table>
</div>

<div style="position:relative; top:-280px; left:160px;">
<font style="font-size:14pt">Shopping Cart</font>
<?php

$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\wamp\www\3350dvd\DVD_shoppingCart.mdb','','');

$subTotal = 0;
echo $_POST['AddToCart'];
$ID = $_GET['id'];
session_start();
$sessID = session_id();

$pattern = "/([a-zA-Z0-9])+AD([a-zA-Z0-9_-])+/";

$add = preg_match($pattern,$ID);

$ID = ereg_replace("[A-Z]", "", $ID);


$date = date('m-d-Y');

if($add == 1)  //if adding to cart
{
  $sqlShow = "SELECT OrderNo, OrderItem, OrderDate, OrderQuantity, Title, Price, MovieInventory.ID FROM ShopCart, MovieInventory WHERE OrderNo = '$sessID' AND MovieInventory.ID = OrderItem";
$cartShow = odbc_exec($conn, $sqlShow);

if($_POST['Add to Cart'] == "Add to Cart")
{
  $sql = "INSERT INTO ShopCart (OrderNo, OrderItem, OrderDate, OrderQuantity) VALUES ('$sessID', '$ID', '$date', 1)";
$rs = odbc_exec($conn, $sql);
}



if (!$rs)
{
echo "An error has occurred. Please try again";
}

echo "<br/>Date: ".date('m-d-Y')."<br/><font style='font-size:12pt'>Order Number: ".$sessID."</font>";
echo "<table style='font-size:10pt; text-align:center'>"; 
echo "<tr style='background-color:Gray; color:White'><td>Item Number</td><td>Quantity</td><td>Title</td><td>Price</td>";

while($row = odbc_fetch_array($cartShow))
{
echo "<tr onclick=\"location.href='details.php?id=".$row['OrderItem']."'\" onmouseover='rowMouse(this)' onmouseout='rowMouseOut(this)'>";
echo "<td>".$row['OrderItem']."</td>";
echo "<td>".$row['OrderQuantity']."</td>";
echo "<td>".$row['Title']."</td>";
echo "<td>$ ".number_format($row['Price'],2)."</td>";
echo "</tr>";
$subTotal += $row['Price'];
}
echo "<tr><td colspan='3' align='right'>SubTotal: </td><td>$ ".$subTotal."</tr>";
echo "</table>";


}
else
{
}

odbc_close($conn);

?>
</div>


</body>
</html>

 

Thanks again!

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.