0

I am building a simple client management system that allows users to input client data into a form populated in a modal. The idea is that when the user completes the form in the modal by clicking Create New Client, new client information will be stored into the predefined mysql table named client. I am using a nearly identical php script throughout the site to create new users which works fine.

The primary difference with this script is that is called from a form that is enclosed within a modal that is enclosed within divs. Could that possibly be inhibiting my scripting from following through on the query?

There are no error messages other than my own defined if else calls. The data will echo on Submit but information will not actually insert into the data in mysql. I'm still new to anything beyond html and css, so any help will be greatly appreciated and reciprocated as I continue to get a handle on this journey. Reference code below

newclient.php

<?php
session_start();
include 'dbh.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

$comname = $_POST['comname'];
$primcon = $_POST['primcon'];
$addr = $_POST['addr'];
$city = $_POST['city']; 
$state = $_POST['state'];
$zip = $_POST['zip'];
$phn = $_POST['phn'];
$websi = $_POST['websi'];
$email = $_POST['email'];
$activ = $_POST['activ'];

$sql = "INSERT INTO client (comname, primcon, addr, city, state, zip, phn, 
websi, email, activ ) 
VALUES ('$comname', '$primcon', '$addr', '$city', '$state', '$zip', '$phn', 
'$websi', '$email', '$activ')";
$result = mysqli_query($conn, $sql);

echo $comname;
echo $primcon;
echo $addr;
echo $city;
echo $state;
echo $zip;
echo $phn;
echo $websi;
echo $email;
echo $activ;

//header("Location: login.html");

admin.html - where users actually create new clients (modal content only)

<div class="modal-content">
<div class="modal-header">
  <span class="close">&times;</span>
  <h2>New Client Registration</h2>
</div>

<div class="modal-body">
    <form align="center" 
action="http://localhost:1234/housenotes/newclient.php" method="POST">
            <input type="text" placeholder="Company Name" name="comname" 
required> 
            <input type="text" placeholder="Primary Contact" name="primcon" 
required> 
            <input type="text" placeholder="Street Address" name="addr" 
required> <br>
            <input type="text" placeholder="City" name="city" required>
            <input type="text" placeholder="State" name="state" required> 
            <input type="text" placeholder="Zip" name="zip" required> <br>
            <input type="text" placeholder="Phone Number" name="phn" 
required> 
            <input type="text" placeholder="Website" name="websi" required>
            <input type="text" placeholder="Email" name="email" required>
<br>  
            <input type="radio" name="activ" value="Potential" checked> 
Potential
            <input type="radio" name="activ" value="Engaged"> Engaged
            <input type="radio" name="activ" value="Active"> Active<br>  
            <button type="submit" class="button" style="width:15%;">Create 
New Client</button>

    </form>

</div>
<div class="modal-footer">
  <h3>housenotes</h3>
</div>

Again, any help would be greatly appreciated! Thanks in advance!

  • 3
    If it is not inserting, use simple debugging using `mysqli_error()` -> `$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));`. Most likely an error in your query. Also, sanatize your user data, as you are wide open to sql injection - [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Sean Oct 08 '17 at 03:53
  • If you're inserting from a modal, is the modal closing when you submit, sometimes opening up the developer tools and looking at the response to the submission in the developer tools can be a good way to see if you did in fact get a response (error response or otherwise) that will help you see where the issue is. – Jeff Richards Oct 08 '17 at 04:53
  • Also you would do well to move the error_reporting line and the display errors above the include in case the include is causing problems. And lastly, I'd suggest adjusting the include to be require since you really want this script to fail out right if the include fails. if include fails it will just return false and issue a warning. require when it fails returns a fatal error which would be better in this case, since you MUST have that db connection set up before you use it! – Jeff Richards Oct 08 '17 at 04:57
  • Thank you both! The problem ended up being with the sanitation. The code itself was error free, however the test contact information I was using included a name with an accent over the e (Darante') which was interfering. I was able to figure that out after running the mysqli_error() -> $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); which showed me that there was a problem with my sytax which I knew was not possible until I remember that my name has an accent, lol. Once I removed the accent, data inserted. Now I'll be working on sanitizing it. Lesson learned! . – Darante LaMar Oct 08 '17 at 20:28

0 Answers0