Jump to content

Recommended Posts

The code works no problem but its soo slow..

 

any suggestions?

 

session_start();

$Busername = session_id();

 

 

$item = $_GET["item"];

$action = '2';

 

// Connect to database

$dbc = mysql_connect ('localhost','username','pasword') OR die('Could not connect to mySql : ' . mysql_error());

mysql_select_db (databasename) OR die('Could not connect to mySql : ' . mysql_error());

 

/*

Update item quantity in shopping cart

*/

$yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));

  $sql = "DELETE FROM TblBasket WHERE CartDate < '$yesterday'";

mysql_query($sql) or die(mysql_error());

 

 

// SQL query to select the rows

$query = "SELECT * FROM TblBasket";

                $result = mysql_query ($query);

               

                while($row = mysql_fetch_array($result,MYSQL_ASSOC))

                {

                $itemIdInDatabase = $row[itemId];

                $UsernameOfUser = $row[userEmail];

                if ($item ==  $itemIdInDatabase && $Busername == $UsernameOfUser){

                    $qunt = $row[qun] + 1;

                    $query2 = "UPDATE TblBasket SET qun = $qunt WHERE itemId = '$item' && userEmail = '$Busername'";

                    $result2 = mysql_query($query2) or die(mysql_error());

                    $action = '1';

                }

                }

If ($action == '2'){

                $query2 = "INSERT INTO TblBasket (id, userEmail, itemId, qun, CartDate) VALUES ('NULL','$Busername','$item', '1', NOW())";

                $result2 =  mysql_query ($query2);

                }

 

 

mysql_close();

 

$ref = $_SERVER['HTTP_REFERER'];

header ("Location: $ref");

Link to comment
https://forums.phpfreaks.com/topic/122852-isit-my-code-or-isit-the-server/
Share on other sites

Your code is looping through all the rows in the TblBasket table, testing each one against $item and $Busername. That is definitely not how to use a database. You should only operate on the rows that match what you are interested in.

 

All of these lines of code -

            // SQL query to select the rows
            $query = "SELECT * FROM TblBasket";
                $result = mysql_query ($query);
                
                while($row = mysql_fetch_array($result,MYSQL_ASSOC))
                {
                $itemIdInDatabase = $row[itemId];
                $UsernameOfUser = $row[userEmail];
                if ($item ==  $itemIdInDatabase && $Busername == $UsernameOfUser){
                    $qunt = $row[qun] + 1; 
                    $query2 = "UPDATE TblBasket SET qun = $qunt WHERE itemId = '$item' && userEmail = '$Busername'";
                    $result2 = mysql_query($query2) or die(mysql_error());
                    $action = '1';
                }
                }

 

Can be replaced with this -

 

            // SQL query to select the rows
            $query2 = "UPDATE TblBasket SET qun = qun + 1 WHERE itemId = '$item' && userEmail = '$Busername'";
            $result2 = mysql_query($query2) or die(mysql_error());
            if(mysql_affected_rows() > 0) {
                    $action = '1';
            }

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.