i actually trying to run 2 select queries and get user id from members3 table and productid, title,price, categoryname and img from product4 tables. i quite noob in php so not too sure whats went wrong. there is no error msg show just that my database data only have userid and productid with 0 value the rest are null...

need advice from u guys.. thanks

<?
$objConnect = mysql_connect("localhost","xxx","xxxx") or die(mysql_error());
$objDB = mysql_select_db("db-test");
$id=$_GET['id'];


$email = $_SESSION['email'];
$sql="SELECT userid FROM members3 where email='$email'";
$result2=mysql_query($sql);
$objQuery2 = mysql_query($sql) or die ("Error Query [".$sql."]");
//while($objResult2 = mysql_fetch_array($objQuery2))
//{
//$userid =$objResult["userid"];
//}


$strSQL = "SELECT * FROM products4 where productid='$id'";
$result=mysql_query($strSQL);
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery) && $objResult2 = mysql_fetch_array($objQuery2))
{

$userid =$objResult["userid"];
$p = $objResult["productid"];
$t = $objResult["title"];
$price = $objResult["price"];
$cat = $objResult["categoryname"];
$img = $objResult["img"];

$action ='';

if(isset($_GET['action']) ) {
$action = $_GET['action'];

}
switch ($action) {
	case 'addtowishlist':
$con = mysql_connect("localhost", "root", "boon84");
$db = mysql_select_db ("db-test", $con) or die ("Could not select database");
$query = mysql_query("INSERT INTO wishlist(userid,productid,title,price,categoryname,img) VALUES( '$userid', '$p', '$t', '$price', '$cat', '$img')") or die(mysql_error());
}
}



?>

Recommended Answers

All 3 Replies

I see 2 potentials points of failure in your code. The first thing is if the users email or the product ID returns no results (because the the email or product ID specified in the URL didn't exist in the database) then the condition in the while() loop will not evaluate to true.

The other point of failure is what if one query returned results but the other didnt? That would also cause the condition to evaluate to false.

I see 2 potentials points of failure in your code. The first thing is if the users email or the product ID returns no results (because the the email or product ID specified in the URL didn't exist in the database) then the condition in the while() loop will not evaluate to true.

The other point of failure is what if one query returned results but the other didnt? That would also cause the condition to evaluate to false.

For the user email , the top part of the code actually is

<?php 
session_start();
if (!(isset($_SESSION['email']) && $_SESSION['email'] != '')) {
header ("Location: login.php");
}

include ('includes\header.php');
?>

As for the product id , on my previous page eg productpage.php i already do a

<div class="product_add"><a href="wishlist.php?action=addtowishlist&id=<? echo $rows['productid']; ?>"><img src="styles/Images/add_icon.png" name="wishlist" alt="add icon"/>ADD TO WISHLIST</a></div>

to add on,

actually if i exclude the following code below for userid and manually insert userid fixed value as "1" , it able to insert my productid,title etc details

$email = $_SESSION['email'];$sql="SELECT userid FROM members3 where email='$email'";$result2=mysql_query($sql);$objQuery2 = mysql_query($sql) or die ("Error Query [".$sql."]");
&& $objResult2 = mysql_fetch_array($objQuery2)){ $userid =$objResult["userid"];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.