+ Reply to Thread
Results 1 to 5 of 5

Thread: MySql Page

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    MySql Page

    I am creating a special shopping cart script and I am having a hard time learning MySQL. I have a database with one table and 3 fields. I simply wish to display all entries with a delete link after each one. I started this code if someone could help me along it would be greatly appreciated. I got kinda lost. I am offering up 500 credits for a sample page.

    Code:
        $db_server = "localhost";
        $db_name = "database";
        $db_user = "username";
        $db_pass = "password";
        
        $con1 = mysql_connect($db_server, $db_user, $db_pass) or die(mysql_error());
        mysql_select_db($db_name, $con1);
        
        $query_Results = ("SELECT * FROM customers ORDER BY Field3");
        $SearchResults = mysql_query($query_Results) or die(mysql_error());
        $row_SearchResults = mysql_fetch_assoc($SearchResults);
        $totalRows_SearchResults = mysql_num_rows($SearchResults);
    Last edited by driveflexfuel; 02-18-2009 at 02:18 PM.

  2. #2
    sarvar's Avatar
    sarvar is offline x10Hosting Member sarvar is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    82

    Re: MySql Page

    I'm just throwing this outta my head, so if you have problems with this post it and improve it to your needs. Like name of the items, price, etc.

    PHP Code:

    DB
     
    *cart
       
    -ip
       
    -id

     
    *items
       
    -id
       
    -name
       
    -desc

    // Get user ip
    $ip $_SERVER['REMOTE_ADDR'];

    // Select customer items
    $select_items mysql_query("SELECT * FROM `cart` WHERE `ip` = '".$ip."'");

    $array_items = array();
    while(
    $row mysql_fetch_array($select_items) {

      
    $array_items[] = $row['id'];

    }

    foreach (
    $array_items AS $shop_items) {

    $get_item_info mysql_query("SELECT * FROM `items` WHERE `id` = '".$shop_items."'");
    $get_item_rst mysql_fetch_assoc($get_item_info);

    echo 
    '$get_item_rst['name'] -- $get_item_rst['desc']          <a href="remove.php?id=$shop_items">REMOVE</a>';


    Not tested. You may want to improve it.
    CLICK HERE to Thank Me.
    (You must be logged in into forums.)

  3. #3
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: MySql Page

    The table and data we are using for this example:

    Code:
    -- phpMyAdmin SQL Dump
    -- version 2.11.9.2
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Feb 21, 2009 at 12:17 PM
    -- Server version: 5.0.67
    -- PHP Version: 5.2.6
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    
    --
    -- Database: `help`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `scart`
    --
    
    CREATE TABLE IF NOT EXISTS `scart` (
      `id` mediumint(8) unsigned NOT NULL auto_increment,
      `item` varchar(10) NOT NULL,
      `Description` varchar(30) NOT NULL,
      `price` varchar(30) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
    
    --
    -- Dumping data for table `scart`
    --
    
    INSERT INTO `scart` (`id`, `item`, `description`, `price`) VALUES
    (10031, '64L-00003', 'Microsoft LifeCam VX-1000 Webc', '22.49'),
    (10032, 'D6600029', 'Microsoft Optical Wheel Mouse-', '14.49'),
    (10033, 'Q8061A#ABA', 'HP Officejet 6310 All-in-One p', '67.58'),
    (10034, 'SDSDRH-004', 'SanDisk 4GB Ultra II Secure Di', '14.99');
    The php file (show_cart.php) to show/remove items:
    PHP Code:
    <?php

    $db_server 
    "localhost";
    $db_name "database";
    $db_user "username";
    $db_pass "password";

    $con1 mysql_connect($db_server$db_user$db_pass) or die(mysql_error());
        
    mysql_select_db($db_name$con1);
        
    $query_Results = ("SELECT * FROM scart ORDER BY id");
    $SearchResults mysql_query($query_Results) or die(mysql_error());
    $row_SearchResults mysql_fetch_assoc($SearchResults);
    $totalRows_SearchResults mysql_num_rows($SearchResults);
    mysql_close();

    ?>

    <h2>Display Shoping Cart</h2>
    <table width='550' cellpadding='0' cellspacing='1'>

        <tr style='height: 20px;'>
                <th width='50'>&nbsp;</th>
                <th width='100'>Item Code</th>
                <th width='300'>Description</th>
                <th width='50'>Price</th>
                <th width='50'>Action</th>
      </tr>


    <?php

    // Loop results
    $i=0;
    while (
    $i $totalRows_SearchResults
    {
     
     
    $tb_id    mysql_result($SearchResults$i0);
     
    $tb_item  mysql_result($SearchResults$i1);
     
    $tb_desc  mysql_result($SearchResults$i2);
     
    $tb_price mysql_result($SearchResults$i3);
     
     
    $tb_orde $i 1
     
    ?>
    <tr>
     <td align='center'>&nbsp;<?php echo $tb_orde?></td>
     <td>&nbsp;<?php echo $tb_item?> </td>
     <td>&nbsp;<?php echo $tb_desc?> </td>
     <td>&nbsp;<?php echo $tb_price?> </td>
     <td>&nbsp;<a href="remove.php?rid=<?php echo $tb_id?>">Remove</a></td>
    </tr>

    <?php
     
    ++$i;
    }

    ?>

    </table>
    The remove.php file is
    PHP Code:
    <?php

    $record 
    $_GET["rid"];

    $db_server "localhost";
    $db_name "database";
    $db_user "username";
    $db_pass "password";

    $con1 mysql_connect($db_server$db_user$db_pass) or die(mysql_error());
        
    mysql_select_db($db_name$con1);
        
    $query_Results = ("DELETE FROM scart WHERE id = $record");
    $SearchResults mysql_query($query_Results) or die(mysql_error());
    mysql_close();

    //Go Back to cart
    include("show_cart.php");

    ?>
    Edit:
    Please note that the previous code is not intended to be open to the www, it’s just intended to be a demonstration. This code would be open to all kind of abuse and exploitation.

    Actually, it’s not a good idea for a user to be able to “delete” records from a table or database. It’s much better to mark the records for deletion and hide them so the user can’t see them anymore.
    Last edited by gomarc; 02-22-2009 at 01:03 PM. Reason: Automerged Doublepost

  4. #4
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: MySql Page

    works like a charm thanks a lot.

  5. #5
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: MySql Page

    Thanks for the credits driveflexfuel!

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. [REQ][$$$]1 page image flash redesign for choclate sales
    By tgkprog in forum The Marketplace
    Replies: 5
    Last Post: 11-17-2008, 09:53 AM
  3. Use PHP to add Content to page through Admin
    By goldy30 in forum Programming Help
    Replies: 1
    Last Post: 11-12-2008, 11:13 PM
  4. Replies: 4
    Last Post: 02-20-2008, 06:13 AM
  5. MySQL problem
    By pensoftware in forum Free Hosting
    Replies: 3
    Last Post: 10-30-2005, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers