Jump to content

Okay so im designing a site, the idea of this page is to click "Add to my PC " on a seperate page such as the CPU page, now i am transfering data via the URL, using ID's then comparing them to the data in the database. Therefor displaying the correct infomation. Clever eh. 

Now the problem i am facing is when a user clicks the tab "My PC" now this will have no Id variable. Now i am trying to use a session variable so the site can hold the data, while more data is getting added, but i cannot get this to work, as when i click it it just gives me a blank templete.

Here is the code (im using bootstrap too)

<?phperror_reporting(0);include 'sqllogin.php'; $id = $_GET['id']; $sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id";                                                  $result=mysqli_query($con, $sql);         while($row = mysqli_fetch_assoc($result)) {             $Make = $row['Make'];             $Model = $row['Model'];            $Clockspeed = $row['Clockspeed'];              $Apu = $row['Apu'];             $App1 = $row['App1'];             $Per1 = $row['Per1'];             $App2 = $row['App2'];             $Per2 = $row['Per2'];             $App3 = $row['App3'];             $Per3 = $row['Per3'];             $App4 = $row['App4'];             $Per4 = $row['Per4'];             $App5 = $row['App5'];             $Per5 = $row['Per5'];            $id = $row['Product_ID'];            $Comments = $row['Comments'];         }                                                                                                                  session_start();    $_SESSION["Model"] = $Model;    $_SESSION["Make"] = $Make;?><!DOCTYPE html><html lang="en"><head>  <meta charset="utf-8">  <title>Cpu Landing page</title>  <meta name="description" content="Hello World">  <!-- Latest compiled and minified CSS -->  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">  <!-- Optional theme -->  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">  <style><?phpinclude "css/stylesheet.css";?></style></head><body>       <header>    <nav class="navbar navbar-inverse " role="navigation">    <div class="navbar-header">        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">            <span class="sr-only">Toggle navigation</span>            <span class="icon-bar"></span>            <span class="icon-bar"></span>            <span class="icon-bar"></span>        </button>        <div class="span5 logo">          <a href=""><img src="img/specmytech.png" alt="" /></a>        </div>    </div>    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">        <ul class="nav navbar-nav">          <li class="active"><a href="#">Home</a></li>            <li><a href="#">My Pc</a></li>            <li class="dropdown">                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Browse Components<b class="caret"></b></a>                <ul class="dropdown-menu">                    <li><a href="cpu.php">CPU</a></li>                    <li><a href="gpu.php">GPU</a></li>                    <li><a href="#">Something else here</a></li>                    <li class="divider"></li>                    <li><a href="#">Separated link</a></li>                    <li class="divider"></li>                                    </ul>            </li>            <li class="dropdown">                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Add Data<b class="caret"></b></a>                <ul class="dropdown-menu">                    <li><a href="addata/cpu.php">CPU</a></li>                    <li><a href="addata/gpu.php">GPU</a></li>                    <li><a href="addata/hdd.php">HDD</a></li>                    <li class="divider"></li>                    <li><a href="addata/mobo.php">Motherboard</a></li>                    <li><a href="addata/cooler.php">CPU cooler</a></li>                    <li><a href="addata/ram.php">RAM</a></li>                    <li class="divider"></li>                    <li><a href="addata/psu.php">Power supply</a></li>                    <li><a href="addata/case.php">Case</a></li>                                    </ul>            </li>        </ul>        <div class="col-sm-3 col-md-3 pull-right">            <form class="navbar-form" role="search">                <div class="input-group">                    <input type="text" class="form-control" placeholder="Search" name="query">                    <div class="input-group-btn">                        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>                    </div>                </div>            </form>          </div>        </div>         </nav><hr> </header>  <div class="container">    <div class="titletext"> My Pc</div>    <h3>Currently </h3>     <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">CPU </div>       <?php echo $_SESSION["Make"]," ", $_SESSION["Model"]; ?></div>             </div>    </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">      <div class="headtext">GPU</div>        <?php echo $GPU; ?></div>    </div>    </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Motherboard</div>      <?php echo $mobo; ?></div>    </div>    <br>    </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Ram</div>     <?php echo $RAM; ?></div>    </div>     </div>           <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Hard drive</div>      <?php echo $HDD; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">CPU cooler</div>      <?php echo $CPU_C; ?></div>    </div>     <br>    </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Powersupply</div>      <?php echo $PSU; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Case</div>        <?php echo $case; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Application 3</div>      <?php echo $App3; ?></div>    </div>    <br>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">How does it perform?</div>      <?php echo $Per3; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Application 4</div>        <?php echo $App4; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">How does it perform?</div>      <?php echo $Per4; ?></div>    </div>    <br>    </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">Application 5</div>      <?php echo $App5; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">How does it perform?</div>      <?php echo $Per5; ?></div>    </div>     </div>         <div class="col-sm-4 col-md-4 col-lg-4">      <div class="outline">        <div class="center">        <div class="headtext">How does it perform?</div>      <?php echo $Per5; ?></div>    </div>    <br>     </div><div class="col-sm-12 col-md-12 col-lg-12">      <div class="outline">        <div class="center">        <div class="headtext">Comments</div>      <?php echo $Comments; ?></div>    </div>     <br>    </div>  </div>        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script></body> </html>

now this code assigns variables values, that are not used, do not worry about this! its for a later purpose :)

Thanks in advance james.

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/476828-php-help/
Share on other sites

Link to post
Share on other sites

Few pointers for you:

 

1)

$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id"; 

Add 'LIMIT 1' to the end of this query, assuming any product ID is going to be unique there is no point continuing to search the DB after its been found.

$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id LIMIT 1"; 

2)

            $App1 = $row['App1'];             $Per1 = $row['Per1'];             $App2 = $row['App2'];             $Per2 = $row['Per2'];             $App3 = $row['App3'];             $Per3 = $row['Per3'];             $App4 = $row['App4'];             $Per4 = $row['Per4'];             $App5 = $row['App5'];             $Per5 = $row['Per5'];

A better way to handle multiple applications would be to store them in a separate DB table and link them to a product using the product_id. You can then have an array with a varying number of applications and it makes the code a lot cleaner and shorter.

 

3)

<?php echo $Per5; ?>

When you just need to echo something use the sorter method instead:

<?= $Per5; ?>

4)

<style><?phpinclude "css/stylesheet.css";?></style>

Include your own stylesheets the same way you have with Bootstrap. There is no need for PHP here.

<link rel="stylesheet" href="css/stylesheet.css">

5)

<?php echo $_SESSION["Make"]," ", $_SESSION["Model"]; ?></div>

PHP uses a period for concatenation not ,

<?= $_SESSION["Make"]." ".$_SESSION["Model"]; ?></div>

6)

error_reporting(0);

Error reporting. Bad for a production environment, awesome when you are learning PHP.

error_reporting(E_ALL);

7)

$id = $_GET['id'];$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id"; 

Validate and clean any user input. By doing the above you are wide open to SQL injection and attack. At the very least make use of mysql_real_escape_string on any user input but carful consideration needs to be made with security.

 

8)

    $_SESSION["Model"] = $Model;    $_SESSION["Make"] = $Make;

This is likely your problem. $Make and $Model are only set in the while loop above yet this code is always executed. If a valid ID isn't passed in both $Make and $Model will be undefined at this point and wipe out anything stored in their respective $_SESSION variables.

 

One solution would be to move this code inside the while loop or you could check the state of $Make and $Model before using them:

    if (!empty($Make) && !empty($Model) {            $_SESSION["Model"] = $Model;        $_SESSION["Make"] = $Make;    }

Also please try and stick to a naming convention for your variables. Either camelCase or underscores are preferred and also correct indentation makes code a lot easier to read.

Link to comment
https://linustechtips.com/topic/476828-php-help/#findComment-6389772
Share on other sites

Link to post
Share on other sites

Few pointers for you:

 

1)

$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id"; 

Add 'LIMIT 1' to the end of this query, assuming any product ID is going to be unique there is no point continuing to search the DB after its been found.

$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id LIMIT 1"; 

3)

<?php echo $Per5; ?>

When you just need to echo something use the sorter method instead:

<?= $Per5; ?>
5)
<?php echo $_SESSION["Make"]," ", $_SESSION["Model"]; ?></div>

PHP uses a period for concatenation not ,

<?= $_SESSION["Make"]." ".$_SESSION["Model"]; ?></div>

Also please try and stick to a naming convention for your variables. Either camelCase or underscores are preferred and also correct indentation makes code a lot easier to read.

1. LIMIT 1 is only needed if the `Product_ID` isn't a primary key or unique, appears to be primary so this  would be unnecessary.

3. Awesome I didn't know about this :D and php7 will not remove it!

5. If you're echoing multiple values you can separate them with a comma. It is actually a bit faster since using periods concatenates them together then echos it whereas commas does not require the concat.

 

God I love php <3

Link to comment
https://linustechtips.com/topic/476828-php-help/#findComment-6391402
Share on other sites

Link to post
Share on other sites

1. LIMIT 1 is only needed if the `Product_ID` isn't a primary key or unique, appears to be primary so this  would be unnecessary.

3. Awesome I didn't know about this :D and php7 will not remove it!

5. If you're echoing multiple values you can separate them with a comma. It is actually a bit faster since using periods concatenates them together then echos it whereas commas does not require the concat.

 

God I love php <3

 

1) Correct but without seeing the Table structure no way to be sure. But even then the speed difference would be negligible at the scale here anyway.

 

3) Its a useful short-cut and makes the code a bit tidier as well.

 

5) Haven't come across this before, could be useful in the future though.

 

Really need to read up on PHP 7 more but no chance of upgrading at work with plenty of legacy code to support

Link to comment
https://linustechtips.com/topic/476828-php-help/#findComment-6391601
Share on other sites

Link to post
Share on other sites

1) Correct but without seeing the Table structure no way to be sure. But even then the speed difference would be negligible at the scale here anyway.

 

3) Its a useful short-cut and makes the code a bit tidier as well.

 

5) Haven't come across this before, could be useful in the future though.

 

Really need to read up on PHP 7 more but no chance of upgrading at work with plenty of legacy code to support

PHP7 should work for php 5.6 it's the earlier ones like 5.4 that you need to make changes I believe. There's a good boost in performance so it could be worth upgrading.

 

Ya I found out about this from phpbench.com which has a slew of comparisons that run when you load the page to see which one is faster.

Link to comment
https://linustechtips.com/topic/476828-php-help/#findComment-6391683
Share on other sites

Link to post
Share on other sites

Few pointers for you:

 

1)

$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id"; 

Add 'LIMIT 1' to the end of this query, assuming any product ID is going to be unique there is no point continuing to search the DB after its been found.

$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id LIMIT 1"; 

2)

            $App1 = $row['App1'];             $Per1 = $row['Per1'];             $App2 = $row['App2'];             $Per2 = $row['Per2'];             $App3 = $row['App3'];             $Per3 = $row['Per3'];             $App4 = $row['App4'];             $Per4 = $row['Per4'];             $App5 = $row['App5'];             $Per5 = $row['Per5'];

A better way to handle multiple applications would be to store them in a separate DB table and link them to a product using the product_id. You can then have an array with a varying number of applications and it makes the code a lot cleaner and shorter.

 

3)

<?php echo $Per5; ?>

When you just need to echo something use the sorter method instead:

<?= $Per5; ?>

4)

<style><?phpinclude "css/stylesheet.css";?></style>

Include your own stylesheets the same way you have with Bootstrap. There is no need for PHP here.

<link rel="stylesheet" href="css/stylesheet.css">

5)

<?php echo $_SESSION["Make"]," ", $_SESSION["Model"]; ?></div>

PHP uses a period for concatenation not ,

<?= $_SESSION["Make"]." ".$_SESSION["Model"]; ?></div>

6)

error_reporting(0);

Error reporting. Bad for a production environment, awesome when you are learning PHP.

error_reporting(E_ALL);

7)

$id = $_GET['id'];$sql= "SELECT * FROM `cpu_allow` WHERE `Product_ID`=$id"; 

Validate and clean any user input. By doing the above you are wide open to SQL injection and attack. At the very least make use of mysql_real_escape_string on any user input but carful consideration needs to be made with security.

 

8)

    $_SESSION["Model"] = $Model;    $_SESSION["Make"] = $Make;

This is likely your problem. $Make and $Model are only set in the while loop above yet this code is always executed. If a valid ID isn't passed in both $Make and $Model will be undefined at this point and wipe out anything stored in their respective $_SESSION variables.

 

One solution would be to move this code inside the while loop or you could check the state of $Make and $Model before using them:

    if (!empty($Make) && !empty($Model) {            $_SESSION["Model"] = $Model;        $_SESSION["Make"] = $Make;    }

Also please try and stick to a naming convention for your variables. Either camelCase or underscores are preferred and also correct indentation makes code a lot easier to read.

thankyou, i learnt alot, i managed to get it working late last night after i started this using isset(). and i plan to protect against injection when i have complete more of this project. 

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
https://linustechtips.com/topic/476828-php-help/#findComment-6393912
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×