Jump to content

gueland

New Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by gueland

  1. I have been using a tutorial from udemy and have reached a point where we are adding to the cart using the IP address of the user. The scripts in the functions.php work EXCEPT for the cart() function. It just blinks the page but does not add the items to the database table cart. The instructor is not getting back to me in the feedback function of the course. Can you help? Here is the code written: function getIp() { $ip = $_SERVER['REMOTE_ADDR']; if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } return $ip; } function cart() { if(isset($_GET['add_cart'])) { global $con; $ip = getIp(); $pro_id = $_GET['add_cart']; $check_pro = "select * from cart where ip_add='$ip' and p_id='$pro_id'"; $run_check = mysqli_query($con, $check_pro); if(mysqli_num_rows($run_check)>0){ echo ""; } else { $insert_pro = "insert into cart (p_id,ip_add) values ('$pro_id','$ip')"; $run_pro = mysqli_query($con, $insert_pro); echo "<script>window.open('index.php','_self')</script>"; } } } The database connection is not written in this, but it does work for the other functions, like adding products for example. I am running Windows 10, with MySQL, Apache, PHP5. I cannot go any further until I resolve this. Please help. Thank you.
  2. I am also getting PHP Notice: Undefined variable: REQUEST_METHOD on line 20.
  3. I have changed the coding but still does not work (see below): <?php $usr = "dbuser"; #not real $pwd = "dbpassword"; #not real $db = "dbcontacts"; $host = "localhost"; // connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> <?php // this is processed when the form is submitted // back on to this page (POST METHOD) if ($REQUEST_METHOD=="POST") { // double-up apostrophes $email = $_POST['email']; $ip = gethostbyname($_SERVER['REMOTE_ADDR']); // setup SQL statement $SQL = " INSERT INTO contacts "; $SQL = $SQL . " (email, date, ip) VALUES "; $SQL = $SQL . " ('$email', 'NOW(), '$ip') "; //execute SQL statement $result = mysql_db_query($db,"$SQL",$cid); // check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } if($result) { //send the email $to = "email@domain.com"; #not real $subject = "New contact from the website"; //headers and subject $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: <".$email.">\r\n"; $body = "New contact<br />"; $body .= "Email: ".$email."<br />"; $body .= "IP: ".$ip."<br />"; mail($to, $subject, $body, $headers); //ok message echo "Your message has been sent"; } } ?> <div id="wrap"> <img src="images/top.jpg" id="top"> <div id="piece3"> <form method=post action="index.php" > <input type=hidden name=redirect value="thankyou.php" /> <input type=hidden name=errorredirect value="error.php" /> <input type=text name="email" id="email" title="Enter your email address"> <input name="Submit" type="submit" id="subscribe" value="" /> </form> </div> <div id="alerts"> <ul class="hoverbox"> <li><a href="#"><img src="images/Stock1.jpg"><img src="images/vnda.png" class="preview"></a></li> </ul> <ul class="hoverbox2"> <li><a href="#"><img src="images/Stock2.jpg"><img src="images/pir.png" class="preview"></a></li> </ul> <ul class="hoverbox3"> <li><a href="#"><img src="images/Stock3.jpg"><img src="images/gvbp.png" class="preview"></a></li> </ul> <ul class="hoverbox4"> <li><a href="#"><img src="images/Stock4.jpg"><img src="images/fre.png" class="preview"></a></li> </ul> </div> <div id="footer"> <h1>Grow your portfolio like you never have, subscribe free above</h1> </div> </div> <?php mysql_close($cid); ?>
  4. This is the form I was basing it on. from http://www.thetechlabs.com/tutorials/interfaces/a-simple-php-mail-contact-form-with-mysql It does not work on my server either.
  5. I have written this page to capture an email address, ip and date that will post to a database and then email, but it does not post Here is the code I have written for the page. <div id="wrap"> <img src="images/top.jpg" id="top"> <div id="piece3"> <form method=post action="index.php" > <input type=hidden name=redirect value="thankyou.php" /> <input type=hidden name=errorredirect value="error.php" /> <script type="text/javascript"> function verifyRequired() { if (document.icpsignup["fields_email"].value == "") { document.icpsignup["fields_email"].focus(); alert("The Email field is required."); return false; } return true; } </script> <input type=text name="fields_email" id="email" title="Enter your email address"> <input name="Submit" type="submit" id="subscribe" value="" /> </form> </div> <div id="alerts"> <ul class="hoverbox"> <li><a href="#"><img src="images/Stock1.jpg"><img src="images/vnda.png" class="preview"></a></li> </ul> <ul class="hoverbox2"> <li><a href="#"><img src="images/Stock2.jpg"><img src="images/pir.png" class="preview"></a></li> </ul> <ul class="hoverbox3"> <li><a href="#"><img src="images/Stock3.jpg"><img src="images/gvbp.png" class="preview"></a></li> </ul> <ul class="hoverbox4"> <li><a href="#"><img src="images/Stock4.jpg"><img src="images/fre.png" class="preview"></a></li> </ul> </div> <div id="footer"> <h1>Grow your portfolio like you never have, subscribe free above</h1> </div> </div> <?php //include the connection file require_once('connection.php'); //save the data on the DB and send the email if(isset($_POST['action']) && $_POST['action'] == 'submitform') { //recieve the variables $email = $_POST['email']; $ip = gethostbyname($_SERVER['REMOTE_ADDR']); //save the data on the DB mysql_select_db($database, $connection); $insert_query = sprintf("INSERT INTO contacts (email, date, ip) VALUES (%s, NOW(), %s)", sanitize($email, "text"), sanitize($ip, "text")); $result = mysql_query($insert_query, $connection) or die(mysql_error()); if($result) { //send the email $to = "info@thestockrage.com"; $subject = "New contact from the website"; //headers and subject $headers = "MIME-Version: 1.0rn"; $headers .= "Content-type: text/html; charset=iso-8859-1rn"; $headers .= "From: ".$name." <".$email.">rn"; $body = "New contact "; $body .= "Email: ".$email." "; $body .= "IP: ".$ip." "; mail($to, $subject, $body, $headers); //ok message echo "Your message has been sent"; } } function sanitize($value, $type) { $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value; switch ($type) { case "text": $value = ($value != "") ? "'" . $value . "'" : "NULL"; break; case "long": case "int": $value = ($value != "") ? intval($value) : "NULL"; break; case "double": $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL"; break; case "date": $value = ($value != "") ? "'" . $value . "'" : "NULL"; break; } return $value; } ?> Here is the structure of the database. describe contacts; +---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | NO | | NULL | | | email | varchar(100) | NO | | NULL | | | url | varchar(200) | YES | | NULL | | | comment | text | YES | | NULL | | | date | datetime | NO | | NULL | | | ip | varchar(255) | YES | | NULL | | +---------+--------------+------+-----+---------+----------------+ Can you help?
×
×
  • 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.