Jump to content

lookee

Members
  • Posts

    28
  • Joined

  • Last visited

    Never

Everything posted by lookee

  1. I have this shopping cart almost running, but it doesn't display the actual names of the contents within the database. I've indicated below where I'm having problems ... What code do I insert to display the database contents of the related Ids? I have tried a variety of things, but none are working. Any help would be greatly appreciated. function connectToDb () -- specifics removed.... // Select the Database $db_select=mysql_select_db($db_name); if (!$db_select) { die ("Couldn't select the database: <br/>". mysql_eror()); } function isError () { if ( $this->connectError ) return true; $error=mysql_error ($this->dbConn); if ( empty ($error) ) return false; else return true; } /** * Returns an instance of MySQLResult to fetch rows with * @param $sql string the database query to run * @return MySQLResult * @access public */ function query($sql) { if (!$queryResource=mysql_query($sql,$this->dbConn)) trigger_error ('Query failed: '.mysql_error($this->dbConn). ' SQL: '.$sql); return new MySQLResult($this,$queryResource); } } /** * MySQLResult Data Fetching Class * @access public * @package SPLIB */ class MySQLResult { /** * Instance of MySQL providing database connection * @access private * @var MySQL */ var $mysql; /** * Query resource * @access private * @var resource */ var $query; /** * MySQLResult constructor * @param object mysql (instance of MySQL class) * @param resource query (MySQL query resource) * @access public */ function MySQLResult(& $mysql,$query) { $this->mysql=& $mysql; $this->query=$query; } /** * Returns the number of rows selected * @return int * @access public */ function size () { return mysql_num_rows($this->query); } /** * Returns the ID of the last row inserted * @return int * @access public */ function insertID () { return mysql_insert_id($this->mysql->dbConn); } /** * Checks for MySQL errors * @return boolean * @access public */ function isError () { return $this->mysql->isError(); } } /** * Fetches a row from the result * @return array * @access public */ function fetch () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) { return $row; } else if ( $this->size() > 0 ) { mysql_data_seek($this->query,0); return false; } else { return false; } } $result = mysql_query($sql); // $result = mysql_query($query); function showCart() { $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } NOT SURE WHAT TO PUT HERE TO SHOW CONTENTS.... $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM packages WHERE id = '.$id; $result = mysql_query($sql); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$size.' by '.$width.'</td>'; $output[] = '<td>£'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } echo showCart(); ?> Thanks!
  2. Okay, thanks... I now defined $result in the line above, and I call the function fetch several lines above, but I still get the error.. Fatal error: Call to a member function fetch() on a non-object Is the non-object $row, or is the non-object something in the database? My head hurts. Thanks for any help. <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>REVIEW QUANTITY</h1> <?php * Checks for MySQL errors * @return boolean * @access public */ function isError () { if ( $this->connectError ) return true; $error=mysql_error ($this->dbConn); if ( empty ($error) ) return false; else return true; } /** * Returns an instance of MySQLResult to fetch rows with * @param $sql string the database query to run * @return MySQLResult * @access public */ function query($sql) { if (!$queryResource=mysql_query($sql,$this->dbConn)) trigger_error ('Query failed: '.mysql_error($this->dbConn). ' SQL: '.$sql); return new MySQLResult($this,$queryResource); } } /** * MySQLResult Data Fetching Class * @access public * @package SPLIB */ class MySQLResult { /** * Instance of MySQL providing database connection * @access private * @var MySQL */ var $mysql; /** * Query resource * @access private * @var resource */ var $query; /** * MySQLResult constructor * @param object mysql (instance of MySQL class) * @param resource query (MySQL query resource) * @access public */ function MySQLResult(& $mysql,$query) { $this->mysql=& $mysql; $this->query=$query; } /** * Returns the number of rows selected * @return int * @access public */ function size () { return mysql_num_rows($this->query); } /** * Returns the ID of the last row inserted * @return int * @access public */ function insertID () { return mysql_insert_id($this->mysql->dbConn); } /** * Checks for MySQL errors * @return boolean * @access public */ function isError () { return $this->mysql->isError(); } } /** * Fetches a row from the result * @return array * @access public */ function fetch () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) { return $row; } else if ( $this->size() > 0 ) { mysql_data_seek($this->query,0); return false; } else { return false; } } // $result = mysql_query($query); function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM packages WHERE id = '.$id; $result = mysql_query($sql); PROBLEM LINE ---> $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' by '.$author.'</td>'; $output[] = '<td>£'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } echo showCart(); ?> [\code]
  3. Below is my complete code (less the connection to the database) When I run the following code, I get the following error: Call to a member function fetch() on a non-object in line 138 If I comment out the PROBLEM LINES, the shopping cart partially displays onscreen, but the content of shopping cart passed from the previous .php screen is not passed in completion. What I need are the mysql commands or something to replace PROBLEM LINES that will access the information passed from the previous shopping cart page. Any help would be greatly appreciated. Thanks! // Select the Database $db_select=mysql_select_db($db_name); if (!$db_select) { die ("Couldn't select the database: <br/>". mysql_eror()); } // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Shopper Shopper</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shopper"> <h1>Deliva's Shopping Cart</h1> <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Review Quantityh1> <?php function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { PROBLEM LINE ----> $row = $result->fetch(); PROBLEM LINE ----> extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' by '.$author.'</td>'; $output[] = '<td>£'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } echo showCart(); ?>
  4. lookee

    mysql error

    For the following code, line 1 if (!$queryResource=mysql_query($sql,$this->dbConn)) line 2 trigger_error ('Query failed: '.mysql_error($this->dbConn). line 3 ' SQL: '.$sql); I receive Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in Line 1 Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource ine Line 2 Notice: Query failed: SQL: SELECT * FROM packages WHERE id = 2 in Line 3 After much research, I discovered that // SET PASSWORD FOR 'user'@'localhost' = OLD_PASSWORD('pass'); might fix the problem, but I keep getting the error Parse error: syntax error, unexpected T_STRING Any help would be greatly appreciated!
  5. lookee

    Syntax

    With the following code, I am receiving the following error SET PASSWORD FOR 'jumpodin_jmpdng1'@'localhost' = OLD_PASSWORD('dvv88'); Parse error: syntax error, unexpected T_STRING in
  6. Thanks.... The crappy tutorial I read gave two quote marks... <?php echo "Hello World";
  7. And when I try to echo it... echo "<div id="leaderboard_bridge"></div><script src="http://xs.mochiads.com/static/pub/swf/leaderboard.js" type="text/javascript"></script><script type="text/javascript">Mochi.addLeaderboardIntegration({partnerID: "x4455839574839567674"});</script>"; ?> The closing php tag doesn't turn red again... something is missing, and I can't figure out what... something in the embed code seems to be throwing off the syntax
  8. I can't link it or href it because it has to be embedded in the actual file. The echo didn't show up inbetween the code tags....
  9. I'm attempting to embed the following code into a .php file, but every time I do, I get a syntax error. <div id="leaderboard_bridge"></div><script src="http://xs.mochiads.com/static/pub/swf/leaderboard.js" type="text/javascript"></script><script type="text/javascript">Mochi.addLeaderboardIntegration({partnerID: "x34551194587393"});</script> The error I get is Parse error: syntax error, unexpected '<' in /home/jumpodin/public_html/components/com_puarcade/puarcade.php on line 170 Basically, I don't know how to integrate this bit of code into my pre-existing .php script. I can't remove the <. I tried putting a ; at the end, but that doesn't work either. Any help would be greatly apprecaited.
  10. Solved... if (mysql_num_rows($result) > 0)
  11. Everything works except the verification of whether a row is already in table. Basicially if 1 row exists, code should quit running and echo "already purchased" message. Code ---------------------------- // Select the Database $db_select=mysql_select_db($db_name); if (!$db_select) { die ("Couldn't select the database: <br/>". mysql_eror()); } // Assign the Query $query = "SELECT * FROM shoes WHERE username = '$username' AND size > 0"; <---------- If size greater than 0 already placed an order) // Execute the Query $result = mysql_query($query); if (!$result) { die ("Line 117 Could not query the database: <br />". mysql_error()); } if ($query == 1) <----- if 1 row exists - order for shoes already in database, so exit { die ("You already made a request!"); } mysql_query("INSERT INTO shoes VALUES('purchase_number','$username','$first_name','$last_name','$address','$city','$state','$zip','$email','$size','date') ") or die(mysql_error()); Header("Location: successful_purchase.php"); include 'close_db.php'; ?> Any help with my logic would greatly help!
  12. I have them in an include. Both tables are within the same database, so I figured the same connection information should work.
  13. How do I verify data obtained via an HTML form with one db table, and if everything matches, write data to a new table? Currently, I'm able to either validate form data in one table OR write data to ANOTHER TABLE, but when I merge all the code, I get a Warning: mysql_query() [function.mysql-query]: Access denied for user 'earlier'@'localhost' (using password: NO) in /home/earlier/public_html/now/request.php on line 93 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/earlier/public_html/now/request.php on line 93 Could not query the database: Access denied for user 'earlier'@'localhost' (using password: NO) My code: ------------ <?php session_start(); // Assign the Query $sql="SELECT * FROM shoe WHERE username='$username'"; //Execute the Query $result=mysql_query($sql); <-------------LINE 93 in Error Message if (!$result){ die ("Could not query the database: <br />". mysql_error()); //Saves Type Request to a MYSQL Database //Insert a row of information into the table "shoepay" / mysql_query("INSERT INTO shoepay VALUES('request_number','$username','$first_name','$last_name','$email','$user_id','$shoe_size','$paid_shoes','date') ") or die(mysql_error()); Header("Location: successful_payment.php"); } mysql_close(); include 'close_db.php'; ?> ---------------------------- It seems I'm opening one query but not finishing it then starting another, and I'm mixing up my "mysql_query" so that login information is getting crossed. Or something. Any help would be greatly appreciated.
  14. Thanks! I just noticed the PHP tutorial by Mustafa on VTC.com was released 2002. I have requested my refund.
  15. Thanks... I got it to work whe I use $_SESSION['loggedin'] = $username; session_register("loggedin"); but I was wondering why I couldn't get it to work using the format in the tutorial, which is... // captures username to be passed on to other pages $loggedin = $username; session_register("loggedin"); thanks! [attachment deleted by admin]
  16. I just purchased a tutorial that gave an example of the session string, and the tutorial is not formatted as $_SESSION['thing'] = 'formstring'; It's formatted $loggedin="username"; session_register("loggedin"); Please see attachment for actual screen capture of example code... Is this not correct? [attachment deleted by admin]
  17. Certainly... the login form and the receiving successful page are below... --------------------- Login Form <div id="dispanel"> <table width="750" border="0" background="../images/graybk.jpg"> <tr> <td width="736" colspan="3"><form name="form2" method="post" action="member.php"> <table width="736" height="221" border="0"> <tr> <td height="21"> </td> <td colspan="3" align="left" valign="top"> </td> <td> </td> </tr> <tr> <td width="13" height="22"> </td> <td colspan="3" align="left" valign="top"><p class="style13 style23">Registered User Login</p> </td> <td width="12"> </td> </tr> <tr> <td> </td> <td width="163"> </td> <td width="167"> </td> <td width="359"> </td> <td> </td> </tr> <tr> <td> </td> <td><span class="style3 style30">Username</span></td> <td><label> <input name="username" type="text" id="username"> </label></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><span class="style3 style30">Password</span></td> <td><input name="password" type="text" id="password"></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td><label> <input name="submit" type="submit" id="submit" value="Login" /> </label></td> <td> </td> <td> </td> </tr> <tr> <td height="21"> </td> <td colspan="3"> </td> <td> </td> </tr> <tr> <td height="21"> </td> <td colspan="3"><hr /></td> <td> </td> </tr> </table> ------------ Successful page that should receive the session strings... <?php session_start(); include 'config.php'; echo "Welcome,".$username; ?> -------------------------- Thanks in advance! (edited by kenrbnsn to add tags)
  18. Nevermind... I accidentally posted this under Mysql... I moved it to the .php forum.
  19. I've almost got my login script working, but for some reason the username string is not passing to my successful.php page. Code -------------------------------------------- <?php session_start(); include 'config.php'; // Connect to server and select databse. //mysql_connect("$host", "$username", "$password")or die("cannot connect"); //mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $username=$_POST['username']; $password=$_POST['password']; // captures username & passwords, so they can be passed on to other pages session_register("username"); $sql="SELECT * FROM nixon WHERE username='$username' and password='$password'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matches table row must be 1 row if($count==1){ header("location: successful.php"); } else { header ("Location: again.html"); } ?> ------------------------ Any assistance would be of great help! Thanks! (edited by kenrbnsn to add tags)
  20. I've almost got my login script working, but for some reason the username string is not passing to my successful.php page. Code -------------------------------------------- <?php session_start(); include 'config.php'; // Connect to server and select databse. //mysql_connect("$host", "$username", "$password")or die("cannot connect"); //mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $username=$_POST['username']; $password=$_POST['password']; // captures username & passwords, so they can be passed on to other pages session_register("username"); $sql="SELECT * FROM nixon WHERE username='$username' and password='$password'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matches table row must be 1 row if($count==1){ header("location: successful.php"); } else { header ("Location: again.html"); } ?> ------------------------ Any assistance would be of great help! Thanks!
  21. Thanks for the response. I'm sitting in front of O'Reilly's Learning PHP 5, O'Reilly's Learning PHP & Mysql, and Gosselin's PHP with MySQL, and not one of them show an example of this, so I'm trying to learn the basics ... but.... even as you explain it all and it sounds clear and everything... which is great... and I am brushing up on the basics... it still doesn't work. Which is the problem... It's not the concepts that don't make sense... it's actually plugging the code in and actually having it work that is the problem. I don't mean to start some sort of insane thread ... people seem to be very helpful, but plain and simple... it "still does not work." ------------------------------------------------- // Create a MySQL table in the selected database mysql_query("ALTER TABLE jos_divvy ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30);"); or die(mysql_error()); <---- line 26 echo "Table Created!"; include 'close_db.php'; ?> Error Message Parse error: syntax error, unexpected ';' in /home/silliso2/public_html/divvy/add columns.php on line 26 ---------------------------------
  22. *sighs* I removed the ( ) from around the alter table. Nothing is working. Everyone thinks they're saying what they mean, but they aren't. For example, the syntax for the Alter table one the Mysql site doesn't have any ( ), but I also noticed it doesn't have and " " around it either... but I am finding tutorials with both both. Nothing is working. I can get one line to work all day long. I've cut and pasted code from other tutorials and built a sample database around the silly code... still nothing... // Create a MySQL table in the selected database mysql_query "ALTER TABLE jos_divvy ADD COLUMN title VARCHAR (30), ADD COLUMN books VARCHAR (30), ADD COLUMN publisher VARCHAR (30), ADD COLUMN ISBN VARCHAR (30), ADD COLUMN contact VARCHAR (30);" or die(mysql_error()); echo "Table Created!"; include 'close_db.php'; ?> Error Code Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/silliso2/public_html/divvy/add columns.php on line 24
  23. What do you mean (...) ? There is no ... after the table name?
  24. I am attempting to add a quantity to an exisiting field in a Mysql table. For instance. Current_Inventory 8 I would like to increase the amount of 8 by 4 to equal 12. Code $query = "UPDATE nixon SET quantity=$books WHERE group_id=9"; mysql_query($query); Right now, the above code simply replaces 8 with 4. How would I write the code to add the amounts together? Would I have to do something like - fetch the current inventory and assign a $current_string to that amount - assign a $new_string to the addition amount - add the two amounts together by assigning the sum of both to an $updated_string? Or is there a simple Mysql command that will do this? Thanks!
×
×
  • 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.