im trying to send the email and redirect it, here is my code so far.
<?php
if (isset($_POST['formName'], $_POST['formEmail'], $_POST['formSubject'], $_POST['formMessage'])) {
$errors = array();
$formName = sanitize($_POST['formName']);
$formEmail = sanitize($_POST['formEmail']);
$formSubject = sanitize($_POST['formSubject']);
$formMessage = sanitize($_POST['formMessage']);
$required_fields = array('formName', 'formEmail', 'formSubject', 'formMessage');
foreach($_POST as $key=>$value){
if(empty($value) && in_array($key, $_required_fields) === true){
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
if(empty($errors)){
$to = 'pwnd999#msn.com';
$subject = $formSubject;
$body = $formMessage;
$header = 'From: ' . $formEmail;
wp_mail($to, $suject, $body, $header);
wp_redirect( get_permalink() . '?success' );
exit;
} else {
$errors[] = 'Something when wrong!';
}
function sanitize($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
if (isset($_GET[ 'success'])==true) { ?>
<div class="messageSent pannel">
<h1>Message sent successfully!</h1>
<p>I will try to reply as fast as possible however, feel free to send me a text message!</p>
</div>
<?php } else{ ?>
<form id="contactForm" method="POST" autocomplete="off" action="">
<?php if(!empty($errors)): ?>
<div class="errors pannel row">
<div class="col-md-8">
<ul>
<h3><li><?php echo implode('</li><li>', $errors);?></li><h3> </ul>
</div>
<div class="col-md-4">
<?php include(TEMPLATEPATH ."/panels/SVG/sadeFace.php"); ?>
</div>
</div>
<?php endif; ?>
<div class="form-group">
<label for="name"><h3>Name <span class="star">*</span></h3></label>
<span class="error"><?php echo $nameErr;?></span>
<div class="inputWrap">
<input type="text" required autocomplete="off" class="form-control" id="name" name="name" placeholder="Danny Devito" <?php echo isset($formName) ? 'value="' . $formName . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/name.php"); ?>
</div>
</div>
<div class="form-group">
<label for="email">
<h3>Email <span class="star">*</span></h3></label>
<div class="inputWrap">
<input type="email" required autocomplete="off" class="form-control" name="email" id="email" placeholder="devito#hollywood.com" <?php echo isset($formEmail) ? 'value="' . $formEmail . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/email.php"); ?>
</div>
</div>
<div class="form-group">
<label for="subject">
<h3>Subject <span class="star">*</span></h3></label>
<div class="inputWrap">
<input type="text" required autocomplete="off" class="form-control" name="subject" id="subject" placeholder="Design me a website!" <?php echo isset($formSubject) ? 'value="' . $formSubject . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/idea.php"); ?>
</div>
</div>
<div class="form-group">
<label for="message">
<h3>What can I help you with? <span class="star">*</span></h3></label>
<div class="inputWrap">
<textarea required autocomplete="off" name="message" class="form-control" id="message" placeholder="Hello Carl," value="poo">
<?php echo isset($formMessage) ? $formMessage : '' ?>
</textarea>
<?php include(TEMPLATEPATH ."/panels/SVG/message.php"); ?>
</div>
</div>
<button type="submit" name="submitted" class="btn main">Submit</button>
</form>
<?php }
?>
This is caused by the name of your field Name (name="name") that conflicts with the name parameter of WP_Query.
If you change that into name="formName" it should work.
Also you need to change the names of your other fields, not because of the previous reason, but because they don't match the names at the top of your code.
Related
I am working on a basic e-commerce website using PHP/MYSQL. I just need to know how I can upload multiple images for a product and then display them on the product details page in this format Example of what I'm trying to implement
I've been trying to figure out how I can display these images (multiple images for 1 product). I really don't understand how it should work! so any advice on simple terms would be appreciated.
Currently, I can only upload 1 image per product.
Here is what I have so far (1 Image per product),
For the Product Class Page
<?php
class Product{
private $db;
private $fm;
public function __construct(){
$this->db = new Database();
$this->fm = new Format();
}
public function productInsert($data, $file){
$productName = mysqli_real_escape_string($this->db->link, $data['productName']);
$catId = mysqli_real_escape_string($this->db->link, $data['catId']);
$body = mysqli_real_escape_string($this->db->link, $data['body']);
$price = mysqli_real_escape_string($this->db->link, $data['price']);
$type = mysqli_real_escape_string($this->db->link, $data['type']);
$town = mysqli_real_escape_string($this->db->link, $data['town']);
$quantity = mysqli_real_escape_string($this->db->link, $data['quantity']);
$email = mysqli_real_escape_string($this->db->link, $data['email']);
$phone = mysqli_real_escape_string($this->db->link, $data['phone']);
$contactName = mysqli_real_escape_string($this->db->link, $data['contactName']);
$permited = array('jpg', 'jpeg', 'png', 'gif');
$file_name = $file['image']['name'];
$file_size = $file['image']['size'];
$file_temp = $file['image']['tmp_name'];
$div = explode('.', $file_name);
$file_ext = strtolower(end($div));
$unique_image = substr(md5(time()), 0, 10).'.'.$file_ext;
$uploaded_image = "../uploads/".$unique_image;
if($productName == "" || $catId == "" || $price == "" || $file_name == "" || $town == "" || $quantity == "" || $email == "" || $phone == "" || $contactName == ""){
$msg = "<span class='error'>Fields must not be empty!</span>";
return $msg;
} elseif ($file_size >1048567) {
echo "<span class='error'>Image Size should be less then 1MB!
</span>";
} elseif (in_array($file_ext, $permited) === false) {
echo "<span class='error'>You can upload only:-".implode(', ', $permited)."</span>";
} else{
move_uploaded_file($file_temp, $uploaded_image);
$query = "INSERT INTO products(productName, catId, body, price, image, type, town, quantity, email, phone, contactName) VALUES('$productName','$catId','$body','$price','$uploaded_image', '$type','$town','$quantity','$email','$phone','$contactName')";
$inserted_row = $this->db->insert($query);
if($inserted_row){
$msg = "<span class='success'> Your Offer is Added Successfully. </span>";
return $msg;
} else{
$msg = "<span class='error'> Sorry! Your offer is not added! Try again later. </span>";
return $msg;
}
}
}
For the Add Product Page
<?php
$product = new Product();
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){
$insertProduct = $product->productInsert($_POST, $_FILES);
}
?>
<form action="" method="post" enctype='multipart/form-data'>
<!-- Add Product start -->
<div class="location">
<!-- Product select start -->
<div class="styled-select-car">
<select name="catId" id="my_selection" style ="font-size:18px;">
<option value="">Select the Offer Category* (Required)</option>
<?php
$cat = new Category();
$getCat = $cat->getAllCat();
if($getCat){
while($result = $getCat->fetch_assoc()){
?>
<option value="<?php echo $result['catId'];?>"><?php echo $result['catName'];?></option>
<?php } } ?>
<br>
</select>
</div>
<!-- Product select end -->
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Offer*</span>
<input type="text" name="productName" id="pick-up-location" class="form-control " placeholder="Enter Your Product or Service Title (Required)">
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Price* </span>
<input type="text" name="price" id="pick-up-location" class="form-control " placeholder="Enter Your Product or Service Price (Required)">
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Location* </span>
<input type="text" name="town" id="pick-up-location" class="form-control " <?php if(Session::get("customerTown")) {?>value="<?php echo Session::get("customerTown");?>" <?php } else { ?> placeholder="Enter Your Location Description (Required)" <?php } ?>>
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Quantity* </span>
<input type="text" name="quantity" id="pick-up-location" class="form-control " placeholder="Enter Your Product's or Service Quantity* (Required)">
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Email*</span>
<input type="text" name="email" id="pick-up-location" class="form-control " <?php if(Session::get("customerEmail")) {?>value="<?php echo Session::get("customerEmail");?>" <?php } else { ?> placeholder="Enter Your Email Address (Required)" <?php } ?>>
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Phone Number* </span>
<input type="text" name="phone" id="pick-up-location" class="form-control " <?php if(Session::get("customerPhone")) {?>value="<?php echo Session::get("customerPhone");?>" <?php } else { ?> placeholder="Enter Your Phone Number (Required)" <?php } ?>>
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Contact Name* </span>
<input type="text" name="contactName" id="pick-up-location" class="form-control " <?php if(Session::get("customerName")) {?>value="<?php echo Session::get("customerName");?>" <?php } else { ?> placeholder="Enter Your Contact Name (Required)" <?php } ?> >
</div>
<br>
<div class="input-group pick-up">
<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span> Product Image* </span>
<input type="file" name="image" class="form-control" placeholder="">
</div>
<br>
<p><b>Give a detailed description of your product or service(Required):</b></p>
<textarea name="body" ></textarea>
</div>
<input style ="font-size:18px;" type="submit" class="submit" name="submit" value="Add Offer" >
</form>
For the Product Details Page
<?php
if(!isset($_GET['productid']) || $_GET['productid'] == NULL){
echo "<script>window.location = '404.php'; </script>";
} else {
$id = preg_replace('/[^-a-zA-Z0-9_]/', '', $_GET['productid']);
}
?>
<div class="main">
<div class="content">
<div class="section group">
<div class="cont-desc span_1_of_2">
<?php
$getProduct = $product->getSingleProduct($id);
if($getProduct){
while($result = $getProduct->fetch_assoc()){
?>
<div class="grid images_3_of_2">
<img src="<?php echo $result['image']; ?>" alt="" />
</div>
<div class="desc span_3_of_2">
<div class="product-information"><!--/product-information-->
<h2><?php echo $result['productName']; ?></h2>
<p>Contact Name: <?php if($result['contactName'] == NULL){ ?>
Guest
<?php } else { echo $result['contactName'];}
?> </p>
<img src="images/rating.png" alt="" /></br>
<span>
<span>FCFA <?php echo number_format($result['price']); ?></span>
<label>Quantity:</label>
<input type="text" value="<?php echo $result['quantity']; ?>" />
</span>
<p><b>Availability:</b> In Stock</p>
<p><b>Category:</b> <?php echo $result['catName']; ?></p>
<p><b>Town:</b> <?php echo $result['town']; ?></p>
<?php
$contact = $result['phone'];
$contact = substr_replace($contact,"*******",2,6);
?>
<p><b>Telephone:</b> <?php echo $contact; ?></p>
<p><b>Email:</b> <?php echo mask_email($result['email']); ?></p>
<img src="images/share.png" class="share img-responsive" alt="" />
<div class="add-cart">
<form action="" method="post">
<input type="submit" class="buysubmit" name="submit" value="View Contact Details"/>
</form>
</div>
<span style="color: red; font-size: 18px;">
<?php
if(isset($addCart)){
echo $addCart;
}
?>
</span>
</div><!--/product-information-->
</div>
<div class="product-desc">
<h2>Offer Details</h2>
<pre> <?php echo $result['body']; ?> </pre>
</div>
<?php } } ?>
Can anyone help me on how I can modify this code to be able to upload multiple images per product and to be able to retrieve these image on the product details page?
To upload multiple images you can use
<input type="file" name="image[]" multiple>
and to retrieve images just loop through with the mysql_fetch_array() method
$sql="SELECT *
FROM tbl_image
WHERE productId= 1 ";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row ['photo'];
echo '<img src="path/'.$image.'" width="360" height="150">';
}
Here below is my code, i get strange results, for example the POST is not being caught by the PHP. If i add ($_SERVER['REQUEST_METHOD'] == 'POST') it is still not caught. It hould be a simple POST, what did i do wrong? The best i get out of this is the error message:
$url_message = 'Something went wro, please try again.';
Please help
if(isset($_POST["submit"])) {
$btc = $_POST['bitcoin'];
$eur = $_POST['euro'];
echo '1';
echo $btc;
echo '2';
echo $eur;
if($btc != "" && $eur != ""){
$user_idr = $_GET['id'];
$user_idreal = $LS->get_CU();
echo $LS->get_CU();
echo $user_idr;
if($user_idr == $user_idreal){
echo 'jemoder';
$cur = $btc;
$eur = $eur;
$user = $user_id;
$datetime = date('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
if($LS->store_val($cur, $eur, $user, $datetime, $ip) != false){
$url_message = 'Gelukt';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}else{
$url_message = 'niet gelukt';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}
}else{
$header_n = $LS->get_CU();
$url_message = 'Something went wro, please try again.';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}
}else{
$header_n = $LS->get_CU();
$url_message = 'Some fields were left blank.';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}
}else{
echo 'hallo';
}
?>
<div class="col-md-9">
<div class="span9">
<div class="space-6"></div>
<div class="row-fluid">
<div class="span12">
<form class="form-horizontal formproperties" action="<?php echo $_SERVER['PHP_SELF'] ?>#form-textfield" method="POST">
<fieldset>
<div class="container">
<div class="row-fluid">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="col-xs-12 col-sm-12 col-lg-6 logintextsupport text-center">
<h3 class="text-left">Buy <?php echo $form_titel;?></h3>
<hr />
<div class="form-group">
<label class="col-md-1 control-label" for="password"></label>
<div class="col-md-9">
<input id="<?php echo $form_id;?>" name="<?php echo $form_name;?>" type="text" placeholder="<?php echo $form_placeholder;?> *" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-1 control-label" for="password"></label>
<div class="col-md-9">
<input id="euro" name="euro" type="text" placeholder=" *" class="form-control input-md" required="">
</div>
</div>
<legend>Payment Method</legend>
<div class="form-group">
<label class="col-md-1 control-label" for="password"></label>
<div class="col-md-5">
<input type="radio" name="radio" id="radio1" /> <label for="radio1" class="inline">iDeal</label><br />
</div>
<div class="col-md-4">
<input type="radio" name="radio" id="radio3" /> <label for="radio3" class="inline">Ban Contact</label>
</div>
</div>
<hr />
<div class="form-group loginboxsupport">
<label class="col-md-12 col-lg-12" for="proceed"></label>
<div class="col-md-12 col-lg-12">
<button id="proceed" type="submit" name="submit" class="btn btn-success buttonali">SUBMIT</button>
</div>
</div>
<div style="height:0.5vw;"></div>
</div>
</div>
</div>
</div>
</fieldset>
</form>
The problem was in the HTML code, at the action there is the following code
action="<?php echo $_SERVER['PHP_SELF'] ?>#form-textfield"
So the action would be a link to itself, nothing on it. Had to add a user ID:
?id=<?php echo $user_id ?>
The complete action became:
action="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $user_id ?>#form-textfield"
The total start of the form:
<form class="form-horizontal formproperties" action="home.php?id=<?php echo $user_id ?>#form-textfield" method="POST">
I have a form that I want to add ReCaptcha to, however, somewhere along the line something isnt working correctly because my form will still submit whether or not reCaptcha is verified.
This is the form:
<?php if(isset($_GET[ 'CaptchaPass'])){ ?>
<div>Thank you! Your Form was Successfully Submitted</div>
<?php } ?>
<?php if(isset($_GET[ 'CaptchaFail'])){ ?>
<div>Captcha Error. Please verify that you are human!</div>
<?php } ?>
<form action="http://vmobileautoglass.com/php/func_contact.php">
<label>Name</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="name">
</div>
</div>
<label>Email
<span class="color-red">*</span>
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="email">
</div>
</div>
<label>Phone
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="phone">
</div>
</div>
<label>Message</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-8 col-md-offset-0">
<textarea rows="8" class="form-control" name="message"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="MYSITEKEYFROMGOOGLE"></div>
<p>
<button type="submit" class="btn btn-primary" name="ContactButton">Send Message</button>
</p>
</form>
This goes at the very top of the page where the form is located:
<?php
if (isset($_POST['ContactButoon'])) {
$url = 'https://google.com/recaptcha/api/siteverify';
$privatekey = "MYPRIVATEKEYFROMGOOGLE";
$response = file_get_contents($url . "?secret=" . $privatekey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$date = json_decode($response);
if (isset($data->success) AND $data->success == true) {
header('Location: contact.php?CaptchaPasss=True');
} else {
header('Location: contact.php?CaptchaFail=True');
}
}
?>
And this is the forms functionality:
// Receiving variables
#$pfw_ip = $_SERVER['REMOTE_ADDR'];
#$name = addslashes($_GET['name']);
#$email = addslashes($_GET['email']);
#$phone = addslashes($_GET['phone']);
#$message = addslashes($_GET['message']);
// Validation
if (strlen($name) == 0) {
header("Location: http://vmobileautoglass.com/php/err_name.php");
exit;
}
if (strlen($email) == 0) {
header("Location: http://vmobileautoglass.com/php/err_email.php");
exit;
}
if (strlen($message) == 0) {
header("Location: http://vmobileautoglass.com/php/err_message.php");
exit;
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "vMobile Contact Form";
$pfw_email_to = "vmobileag#gmail.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "name: $name\n"
. "email: $email\n"
. "phone: $phone\n"
. "message: $message\n";
#mail($pfw_email_to, $pfw_subject, $pfw_message, $pfw_header);
header("Location: http://vmobileautoglass.com/php/successform.php");
You have a typo:
$date = json_decode($response);
if(isset($data->success) AND $data->success==true){
$date should be $data.
PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
header('location: no_acces.php');
exit();
} else {
$id_user = $_SESSION['userid'];
$q_user = mysqli_query($conn, "SELECT * FROM users WHERE id = $id_user");
if (mysqli_num_rows($q_user) === 1) {
$r_user = mysqli_fetch_assoc($q_user);
} else {
unset($_SESSION['login']);
unset($_SESSION['userid']);
header('location: no_acces.php');
exit();
}
}
$error = "";
$userQuery = mysqli_query($conn, "SELECT username FROM users");
$user = mysqli_fetch_assoc($userQuery);
$id = $_GET['id'];
if (isset($_POST['edit_contact'])) {
$roepnaam = $_POST['roepnaam'];
$naam = $_POST['naam'];
$land = $_POST['land'];
$bedrijf = $_POST['bedrijf'];
$adres1 = $_POST['adres1'];
$adres2 = $_POST['adres2'];
$stad = $_POST['stad'];
$postcode = $_POST['postcode'];
$provincie = $_POST['provincie'];
$telefoon = $_POST['telefoon'];
$email = $_POST['email'];
$captcha= $_POST['g-recaptcha-response'];
if(!$captcha){
$error = "Er is een fout opgetreden";
}
if ($error == "") {
$insertUser = ("UPDATE address SET
roepnaam = '$roepnaam', naam = '$naam', bedrijf = '$bedrijf', telefoon = '$telefoon', email = '$email', adres1 = '$adres1', adres2 = '$adres2', stad = '$stad', postcode = '$postcode', provincie = '$provincie', land = '$land' WHERE id = $id");
if (mysqli_query($conn, $insertUser)) {
$_SESSION['edit_contact'] = true;
header('location: address_book.php');
} else {
$error = "Er is een fout opgetreden";
}
}
}
?>
HTML Code
<!DOCTYPE html>
<html lang="en">
<body>
<form action="" method="post">
<?php if ($error !== "") { ?>
<div class="row">
<div class="col-md-12 error">
<?php echo $error; ?>
</div>
</div>
<?php } ?>
<label for="firstName" class="control-label">Naam:</label>
<div class="row ">
<div class="col-md-6">
<input type="text" class="form-control" id="firstName" placeholder="Roepnaam" name="roepnaam" value="<?php if (isset($_POST['roepnaam'])) { echo $_POST['roepnaam']; } ?>" required/>
</div>
<div class="col-md-6">
<input type="text" class="form-control" id="lastName" placeholder="Naam" name="naam" value="<?php if (isset($_POST['naam'])) { echo $_POST['naam']; } ?>" required/>
</div>
</div>
<label for="username" class="control-label">Bedrijf:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="username" placeholder="Bedrijf" name="bedrijf" value="<?php if (isset($_POST['bedrijf'])) { echo $_POST['bedrijf']; } ?>" required/>
</div>
</div>
<label for="password" class="control-label">Telefoonnummer:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Telefoonnummer" name="telefoon" value="<?php if (isset($_POST['telefoon'])) { echo $_POST['telefoon']; } ?>" required/>
</div>
</div>
<label for="email" class="control-label">Email:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="email" placeholder="E-mailadres" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" required/>
</div>
</div>
<label for="adres1" class="control-label">Adres:</label>
<div class="row">
<div class="col-md-12">
<input type="text" class="form-control" id="adres1" placeholder="Adres 1" name="adres1" value="<?php if (isset($_POST['adres1'])) { echo $_POST['adres1']; } ?>" required/>
</div>
</div>
<div class="row padding-top-10">
<div class="col-md-12">
<input type="text" class="form-control" id="adres2" placeholder="Adres 2" name="adres2" value="<?php if (isset($_POST['adres2'])) { echo $_POST['adres2']; } ?>"/>
</div>
</div>
<div class="row">
<div class="col-md-3">
<label for="postcode" class="control-label">Postcode:</label>
</div>
<div class="col-md-5">
<label for="city" class="control-label">Stad:</label>
</div>
<div class="col-md-4">
<label for="regio" class="control-label">Regio:</label>
</div>
</div>
<div class="row ">
<div class="col-md-3">
<input type="text" class="form-control" id="postcode" placeholder="Postcode" name="postcode" value="<?php if (isset($_POST['postcode'])) { echo $_POST['postcode']; } ?>" required/>
</div>
<div class="col-md-5">
<input type="text" class="form-control" id="city" placeholder="Stad" name="stad" value="<?php if (isset($_POST['stad'])) { echo $_POST['stad']; } ?>" required/>
</div>
<div class="col-md-4">
<input type="text" class="form-control" id="regio" placeholder="Provincie" name="provincie" value="<?php if (isset($_POST['provincie'])) { echo $_POST['provincie']; } ?>" required/>
</div>
</div>
<label for="land" class="control-label">Land:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Land" name="land" value="<?php if (isset($_POST['land'])) { echo $_POST['land']; } ?>" required/>
</div>
</div>
<div class="row">
<div class="col-md-8 padding-top-10 ">
<div class="g-recaptcha " data-sitekey="6LcCsBoTAAAAAK72uzyJSrgWwD8xuF6jFIfgFaHX"></div>
</div>
</div>
<div class="row">
<div class="col-md-2 padding-top-10">
<input type="submit" name="edit_contact" class="btn btn-succes" value="Wijzigen">
</div>
<div class="col-md-2 padding-top-10">
<input type="text" name="delete_contact" action="delete_contact.php" class="btn btn-succes" value="Contact verwijderen">
</div>
</div>
</form>
</body>
</html>
PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true || !isset($_SESSION['userid']) || $_SESSION['userid'] == "") {
header('location: login.php');
exit();
} else {
session_regenerate_id();
}
$id = $_GET['id'];
$query = "DELETE FROM address WHERE id= $id";
mysqli_query ($query);
if (mysql_affected_rows() == 1) {
header('location: addressbook.php');
} else {
echo "Verwijderen mislukt";
}
?>
I'm trying to make a delete button for my contacts within the addressbook. but everytime I click "Contact verwijderen" the webpage resets it self and the contact won't be deleted. Could anyone help me to fix this?
You input is a text input and you don't have a form asociated with it,create one and change the type of submit to submit
<form action="delete_contact.php" method="post">
//other inputs
<input type="submit" name="delete_contact" class="btn btn-succes" value="Contact verwijderen">
</form>
You are mixing MySQL and MySQLi functions:
mysqli_query ($query);
if (mysql_affected_rows() == 1)
You cannot mix MySQL with MySQLi, your code should be:
mysqli_query ($query);
if (mysqli_affected_rows($conn) == 1)
Add a normal link to delete the contact, you don't need a form.
<a href="delete_contact.php?id=<?php echo $id ?>">
Contact verwijderen
</a>
I would like to ask your help. I wrote a code which checks if all the fields are filled in and if the file is not larger than 1 MB. If everything is correct the file successfuly uploads to MySQL database. But if the file size is larger than 1 MB the code stops working. The error doesnt show up and all the fields become empty. Here`s the code (some of it is in Lithuanian, sorry):
<?php
error_reporting(E_ERROR);
session_start();
if (isset($_SESSION['login'])){
include 'config.php';
$username = $_SESSION['login'];
$result = mysqli_query($db,"SELECT * FROM users WHERE username='$username'");
$rws = mysqli_fetch_array($result);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$autorius = mysqli_real_escape_string($db,$_POST['autorius']);$pavadinimas = mysqli_real_escape_string($db,$_POST['pavadinimas']);$puslapiai = mysqli_real_escape_string($db,$_POST['puslapiai']);$tema = mysqli_real_escape_string($db,$_POST['tema']);$pmintis = mysqli_real_escape_string($db,$_POST['pmintis']);$pveikejai = mysqli_real_escape_string($db,$_POST['pveikejai']);$aveikejai = mysqli_real_escape_string($db,$_POST['aveikejai']);$epizodas = mysqli_real_escape_string($db,$_POST['epizodas']);$nuomone = mysqli_real_escape_string($db,$_POST['nuomone']);$apie = mysqli_real_escape_string($db,$_POST['apie']);$foto = mysqli_real_escape_string($db,$_POST['foto']);$user_id = $rws['id'];
if (!empty($autorius) && !empty($pavadinimas) && !empty($puslapiai) && !empty($tema) && !empty($pmintis) && !empty($pveikejai) && !empty($aveikejai) && !empty($epizodas) && !empty($nuomone) && !empty($apie) && isset($foto) && $_FILES['foto']['size'] > 0 && $_FILES['foto']['size'] < 1000001) {
$fileName = $_FILES['foto']['name'];$tmpName = $_FILES['foto']['tmp_name'];$fileSize = $_FILES['foto']['size'];$fileType = $_FILES['foto']['type'];
$fp = fopen($tmpName, 'r');
$foto = fread($fp, filesize($tmpName));
$foto = addslashes($foto);
fclose($fp);
$query = "INSERT INTO books (id_user, autorius, pavadinimas, puslapiai, tema, pmintis, pveikejai, aveikejai, epizodas, nuomone, apie, foto, name, type, size) ".
"VALUES ('$user_id', '$autorius', '$pavadinimas', '$puslapiai', '$tema', '$pmintis', '$pveikejai', '$aveikejai', '$epizodas', '$nuomone', '$apie', '$foto', '$fileName', '$fileType', '$fileSize')";
$result = mysqli_query($db,$query);
$success = "Knygos aprašymas įkeltas";
echo $_FILES['foto']['size'];
}
else if (empty($autorius) || empty($pavadinimas) || empty($puslapiai) || empty($tema) || empty($pmintis) || empty($pveikejai) || empty($aveikejai) || empty($epizodas) || empty($nuomone) || empty($apie)) {
$error = "Užpildykite visus laukelius!";
}
else if (empty($foto)){
$error = "Pasirinkite viršelio nuotrauką!";
}
else if ($_FILES['foto']['size'] > 1000001){
$error = "Viršelio nuotraukos dydid neturi viršyti 1 MB!";
}
}
?>
<?php include 'bin/includes/header.html'; ?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Įkelti knygos aprašymą
<small>Ikelkite savo knygos aprašymą</small>
</h1>
<ol class="breadcrumb">
<li>Pagrindinis
</li>
<li class="active">Įkelti knygos aprašymą</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-md-8">
<form name="upload" id="upload" method="post" enctype="multipart/form-data">
<div class="control-group form-group">
<?php
if(isset($success)) {
echo '<label style="color: #44FF00;">';
echo $success;
echo '</label>';
}
else {
echo '<label style="color: #FF3700;">';
echo $error;
echo '</label>';
} ?>
<div class="controls">
<input type="text" class="form-control" name="autorius" maxlength="60" placeholder="Autorius" value="<?php if(!empty($_POST['autorius'])) echo $_POST['autorius'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="pavadinimas" maxlength="255" placeholder="Pavadinimas" value="<?php if(!empty($_POST['pavadinimas'])) echo $_POST['pavadinimas'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="puslapiai" placeholder="Puslapių skaičius" value="<?php if(!empty($_POST['puslapiai'])) echo $_POST['puslapiai'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="pveikejai" maxlength="999" placeholder="Pagrindiniai veikėjai" value="<?php if(!empty($_POST['pveikejai'])) echo $_POST['pveikejai'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="aveikejai" maxlength="999" placeholder="Antraeiliai veikėjai" value="<?php if(!empty($_POST['aveikejai'])) echo $_POST['aveikejai'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Tema" rows="2" class="form-control" name="tema" maxlength="999" style="resize:none"><?php if(!empty($_POST['tema'])) echo $_POST['tema'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Pagrindinė mintis" rows="2" class="form-control" name="pmintis" maxlength="999" style="resize:none"><?php if(!empty($_POST['pmintis'])) echo $_POST['pmintis'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Siužetas" rows="5" class="form-control" name="apie" maxlength="2999" style="resize:none"><?php if(!empty($_POST['apie'])) echo $_POST['apie'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Įsimintiniausias epizodas" rows="3" class="form-control" name="epizodas" maxlength="2999" style="resize:none"><?php if(!empty($_POST['epizodas'])) echo $_POST['epizodas'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Nuomonė apie knygą" rows="3" class="form-control" name="nuomone" maxlength="999" style="resize:none"><?php if(!empty($_POST['nuomone'])) echo $_POST['nuomone'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<label>Įkelti viršelį (max 1MB)</label>
<input name="foto" type="file" id="foto">
<p class="help-block"></p>
</div>
<button type="submit" class="btn btn-primary">Įkelti</button>
</div>
</form>
</div>
</div>
<?php include 'bin/includes/footer.html'; ?>
<?php } else {
header("location: index.php");
}
Thanks for your help!
You should use this to get the file size:
$size = filesize($_FILES['foto']['tmp_name']);
This returns the filesize in byte. One MB are 1048576 bytes. You should check for the function not returning false.
In general: You should use exceptions for this purpose, it is much simplier to check for a condition and then check again. If you just want to use if-else-statements, you should rearrange it (check for errors first and then upload the thing in the else-case).
Why are you not saving the files in filesystem? This is a lot easier...