2

As i'm trying to insert the value in database through php code but it didn't work and also it didn't give any error. Here is the code that i'd tried to execute but not succeeded.

 <?php
$mysqli = new mysqli("localhost", "admin", "password", "project") or die("couldn't connect to the database");
error_reporting(0);
session_start();
if (!isset($_SESSION["sess_user"])) {
    header("location:index.php");
} else {
    $username = $_SESSION['sess_user'];
    if (isset($_GET['submit'])) {
        if ($_GET['e1'] == "E-LN3465") {
            $productname = $mysqli->real_escape_string($_GET['e1']);
            if ($insert = $db->query("INSERT INTO `cart`(`ID`, `pid`) ((Select `ID` from `users` where `Username`='$username'),(Select `pid` from `product` where `pname`='$productname'))")) {
                $checkQuery = $mysqli->query("INSERT INTO `cart`(`ID`, `pid`) ((Select `ID` from `users` where `Username`='$username'),(Select `pid` from `product` where `pname`='$productname'))");
            }
        }
    }
}
?>
Saty
  • 22,443
  • 7
  • 33
  • 51
Mishal Baig
  • 175
  • 1
  • 2
  • 10
  • Execute query two time and `$db` is nothing in your code just remove it form your code – Saty Nov 23 '15 at 11:37
  • 1
    @Calimero no need of `values` in INSERT SELECT statement http://dev.mysql.com/doc/refman/5.7/en/insert-select.html – Saty Nov 23 '15 at 11:44
  • @Saty you're right, my bad, thanks for your comment. – Calimero Nov 23 '15 at 11:45
  • @Calimero you'r welcome! we all learner here !! – Saty Nov 23 '15 at 11:46
  • @Saty but it still didn't work. – Mishal Baig Nov 23 '15 at 11:50
  • echo and Run your in phpmyadmin and check the erroe – Saty Nov 23 '15 at 11:51
  • No error.Also, it is not inserting. – Mishal Baig Nov 23 '15 at 11:54
  • $mysqli = new mysqli ("localhost","admin","password","project") or die("couldn't connect to the database"); error_reporting(0); session_start(); if(!isset($_SESSION["sess_user"])){ header("location:index.php"); } else { $username=$_SESSION['sess_user']; if(isset($_GET['submit'])) { if( $_GET['e1']== "E-LN3465") {$productname=$mysqli->real_escape_string($_GET['e1']); if ($mysqli->query("INSERT INTO `cart`(`ID`, `pid`) ((Select `ID` from `users` where `Username`='$username'),(Select `pid` from `product` where `pname`='$productname'))")) { echo "INSERT SUCESSFULLY"; } } }} – Mishal Baig Nov 23 '15 at 11:59
  • @Saty Now it is working. Thanks a lot – Mishal Baig Nov 23 '15 at 12:05

1 Answers1

2

In your code $mysqli variable store your connection object and you are executing your query two time. $db is nothing in your code just remove it

if ($mysqli->query("INSERT INTO `cart`(`ID`, `pid`) ((Select `ID` from `users` where `Username`='$username'),(Select `pid` from `product` where `pname`='$productname'))")) {
         echo "INSERT SUCESSFULLY";
    }

UPDATED

Change your query to

INSERT INTO `cart`(`ID`, `pid`)
SELECT users.ID, product.pid 
     FROM users, product
     WHERE users.Username='$username'
     AND product.pname='$productname';
Saty
  • 22,443
  • 7
  • 33
  • 51