corbeeresearch Posted August 10, 2010 Share Posted August 10, 2010 Hi, I'm a newbie, and needed help fixing a part of a code Whenever someone is clicking add to cart, it triggers an error. The output is: Duplicate entry Warning: Cannot modify header information - headers already sent by (output started at C:\ampp\htdocs\ptc1\pages\insertTotempsql.php:9) in C:\ampp\htdocs\ptc1\pages\insertTotempsql.php on line 56 Is there some weakness in the code causing it? line 56 is the last line. The code is <?php if( !isset( $_SESSION )){ session_start(); } $ses_id = session_id(); require_once('../Connections/connect.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php if(isset($_POST['id'])){ $id=$_POST['id']; $uom=$_POST['uom']; $quantity=$_POST['QUANTITY']; $date=$_POST['dateToday']; $PRICE=$_POST['PRICE']; if(isset($_POST['MeatOrigin'])){ $prod=$_POST['prodname'] . " ( " . $_POST['MeatOrigin'] . " ) "; } else { $prod=$_POST['prodname']; } mysql_select_db($database_connect, $connect); //check database if item is existing $query_duplicate = sprintf("SELECT * FROM tbl_cart WHERE pd_id='$id' and ct_session_id='$ses_id' and ct_date='$date'"); $duplicate = mysql_query($query_duplicate, $connect) or die(mysql_error()); $row_duplicate = mysql_fetch_assoc($duplicate); $totalRows_duplicate = mysql_num_rows($duplicate); if($totalRows_duplicate > 0){ echo "Duplicate entry"; } else { $insertSQL = "INSERT INTO tbl_cart ( pd_id, pd_name, ct_qty, ct_session_id, ct_date, PRICE, uom) VALUES ('$id', '$prod', '$quantity', '$ses_id', '$date', '$PRICE', '$uom')"; $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error()); } mysql_close($connect); header("Location:".$_SERVER['HTTP_REFERER']); } ?> Thanks Link to comment https://forums.phpfreaks.com/topic/210295-header-error/ Share on other sites More sharing options...
kenrbnsn Posted August 10, 2010 Share Posted August 10, 2010 Remove the DOCTYPE line, since it's not needed as there is no HTML output. You can not send any output to the browser before you do a header() function and the DOCTYPE line is output. You will also can't echo the error message. What good is a error message when the header loads another page and the user wouldn't see it? Ken Link to comment https://forums.phpfreaks.com/topic/210295-header-error/#findComment-1097372 Share on other sites More sharing options...
corbeeresearch Posted August 10, 2010 Author Share Posted August 10, 2010 cool! It works! I had no idea that was causing it. Thanks! Link to comment https://forums.phpfreaks.com/topic/210295-header-error/#findComment-1097374 Share on other sites More sharing options...
kenrbnsn Posted August 10, 2010 Share Posted August 10, 2010 Read this sticky note Ken Link to comment https://forums.phpfreaks.com/topic/210295-header-error/#findComment-1097375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.