I have a form that submits business information. We dont always have all the info so I want to have it so that only the Business Name and Contact are required. The issue I have now is if the email is empty the page errors out. Give me a 500 error. I am using the !emtpy and trim in the script.
add/index.php
session_start();
if (!isset($_SESSION['logged_in'])) {
header('Location:https://portal.site.net/');
}
require 'dbconnect.php';
if(isset($_POST['addsmb'])){
$businessName = !empty($_POST['businessName']) ? trim($_POST['businessName']) : null;
$contactName = !empty($_POST['contactName']) ? trim($_POST['contactName']) : null;
$title = !empty($_POST['title']) ? trim($_POST['title']) : null;
$phone = !empty($_POST['phone']) ? trim($_POST['phone']) : null;
$email = !empty($_POST['email']) ? trim($_POST['email']) : null;
$url = !empty($_POST['url']) ? trim($_POST['url']) : null;
$city = !empty($_POST['city']) ? trim($_POST['city']) : null;
$state = !empty($_POST['state']) ? trim($_POST['state']) : null;
$outletid = !empty($_POST['outletid']) ? trim($_POST['outletid']) : null;
$user = !empty($_POST['user']) ? trim($_POST['user']) : null;
$sql = "SELECT COUNT(Phone) AS num FROM smb_leads WHERE Phone = :phone";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':phone', $phone);
//.
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row['num'] > 0){
$_SESSION['error'] = "1";
header("Location: ../error.php".$qstring);
exit;
}
if(empty($_POST['email'])){
$email = "No Email";
}
$sql = "INSERT INTO smb_leads (Outlet_ID, URL, State, City, DUNS_Name, Contact_Name, Title, Phone, created_by, email) VALUES (:outletid, :url, :state, :city, :businessName, :contactName, :title, :phone, :user, :email)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':businessName', $businessName);
$stmt->bindValue(':contactName', $contactName);
$stmt->bindValue(':title', $title);
$stmt->bindValue(':phone', $phone);
$stmt->bindValue(':url', $url);
$stmt->bindValue(':city', $city);
$stmt->bindValue(':state', $state);
$stmt->bindValue(':outletid', $outletid);
$stmt->bindValue(':user', $user);
$stmt->bindValue(':email', $email);
$result = $stmt->execute();
}
// Redirect to the listing page
header("Location: ../index.php".$qstring);
form
<form action="add/index.php" method="post">
<span style="color: gray; font-size: 12px;" >Fields marked with a * are required </span>
<div class="form-row">
<div class=".form-group.required col-md-4">
<input type="text" class="form-control" id="businessName" name="businessName" placeholder="* Business Name">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="contactName" name="contactName" placeholder="* Contact Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="title" name="title" placeholder="Contact Title">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="phone" name="phone" placeholder="* Contact Phone #">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="email" name="email" placeholder="Email Address">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="url" name="url" placeholder="Company Website">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="city" name="city" placeholder="City">
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control" id="state" name="state" placeholder="State">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="outletid" name="outletid" placeholder="Outlet ID" value="<?php echo $_SESSION['outlet_id']; ?>">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="user" name="user" placeholder="* Your Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-1">
</div>
<button type="submit" name="addsmb" value="Register" class="btn btn-primary">Submit</button>
</form>
Im guessing my error is somewhere in here.
$phone = !empty($_POST['phone']) ? trim($_POST['phone']) : null;
$email = !empty($_POST['email']) ? trim($_POST['email']) : null;
The script works just fine if i leave everything else blank but the name, business and email.
My Error
This page isn’t working portal.site.net is currently unable to handle this request.
HTTP ERROR 500