0

I am trying to create the form to add data into the SQL database. It fetches first the data for me that already exist in the other table and then I them to be added into database along with form i have created. But despite no errors the data is not inserted it comes back with 0 ROWS. I tried to fix it for hours and i can't think of anything that would fix it.

Here is my code that fetches the data from existing table:

    <form action="" method = "POST" class="form-horizontal">
                                <fieldset>
                                    <legend>Action Taker:</legend>
<label class="control-label col-lg-4">Employee Name: </label>
<input type="text" name="employ_first" value ="<?php echo (isset($row['first_name'])&&!empty($row['first_name'])) ? $row['first_name'] : ''; ?>" disabled>
<input type="text" name="employ_second" value ="<?php echo (isset($row['second_name'])&&!empty($row['second_name'])) ? $row['second_name'] : ''; ?>" disabled>
<label class="control-label col-lg-4">Staff Comment: </label>
<input type="text" name="reason_emp" value ="<?php echo (isset($row['reason'])&&!empty($row['reason'])) ? $row['reason'] : ''; ?>" size="100" disabled>
 <div>
                                                    &nbsp;  &nbsp;  &nbsp;
                                            </div>
                                        <div>
                                            <label class="control-label col-lg-4">Add Respond Comment:</label>
                                            <div class="col-lg-4">
                                                <input type="text" name="admin_respond" class="form-control" size="50px"/>
                                            </div>
                                            <div>
                                                    &nbsp;  &nbsp;  &nbsp;
                                            </div>
                                        </div>
    <input type="submit" name="submit" class="form-control"  value="Add" />
    </form>

Here is the FORM code:

<form action="meeting_schema.php" method = "POST" class="form-horizontal">
<h3>Fill The Form For Investigation:</h3>
<div> 
<label>Type of Investigation:</label>
<input name="type" type="text">
<label>Set Date:</label>
<input name="set_on_date" type="date">
<label>Set Time:</label>
<input name="set_on_time" type="time">
<label>Assigned to:</label>
<input name="assigned_to" type="text">
<input type="submit" name="add_meeting" value="Create Meeting" />
</div>
</form>

And here is my action php file meeting_schema:

<?php
 $hostnames = "localhost";
    $usernames = "u3253997...";
    $passwords = "Ws.....";
    $databaseNames = "u3253997...";
    $conns= mysqli_connect($hostnames, $usernames, $passwords, $databaseNames);
 if (!$conns) {

die('Connection failed: ' . mysqli_connect_error());

        }
if (!$conns) {

die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['add_meeting'])){

$stmt = $conns->prepare("INSERT INTO `emp_inv` (`meeting.id`,`employ_first`,`employ_second`,`reason`,`type`,`set_on_date`, `set_on_time`, `set_by`, `assigned_to`) VALUES (NULL,?,?,?,?,?,?,?,?)");


if (!$stmt) {
        echo "There is an error in the source code, please contact support to fix the problem.";
    }
    else {

$stmt->bind_param('ssssssss', $_POST['employ_first'], $_POST['employ_second'], $_POST['reason_emp'], $_POST['type'], $_POST['set_on_date'], $_POST['set_on_time'], $_SESSION['set_by'], $_POST['assigned_to']);
$stmt->execute();

echo "Meeting has been created";
header("location:meetings.php");
} 
}   

?>
XJL
  • 31
  • 2
  • 1
    You aren't checking the return value of `bind_param()` **or** `execute()`. See [How to get MySQLi error information in different environments](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments) for an easier way – Phil Jan 14 '19 at 04:54
  • 1
    Also see [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Jan 14 '19 at 04:57
  • `bind_param` can return **NULL** on error, you don't check for it. – rsm Jan 14 '19 at 04:58
  • _"`meeting.id`"_ I don't recommend having dots in column names. It would make your code confusing to read and will probably lead you to making mistakes at some point – Phil Jan 14 '19 at 04:59
  • Fatal error: Uncaught mysqli_sql_exception: Column 'employ_first' cannot be null in /home/u325399794/domains/mcdempolog.hostingerapp.com/public_html/admin/meeting_schema.php:28 Stack trace: #0 /home/u325399794/domains/mcdempolog.hostingerapp.com/public_html/admin/meeting_schema.php(28): mysqli_stmt->execute() #1 {main} thrown in /home/u325399794/domains/mcdempolog.hostingerapp.com/public_html/admin/meeting_schema.php on line 28 thats what is is showing now – XJL Jan 14 '19 at 05:00
  • Did you debug the code to see if the data is coming in the $_POST? – Navid Jan 14 '19 at 05:09
  • The column is `NOT NULL`. Do not put as `NULL` just leave as empty string `''` If you want to leave it as empty – BadPiggie Jan 14 '19 at 05:09
  • You say you have no errors, you do. You're not checking for them, that's why. – Funk Forty Niner Jan 14 '19 at 06:29
  • **Note:** The duplicate(s) that was/were used to closed the question with, are **all** to be consulted and tried in order for you to debug your code. This helps to avoid probable countless comments asking for clarification/more code and possibly you never answering to them or expecting someone to spend time putting something together to test it; that isn't their job to do this, as noble as it may be. If you followed the steps **"to the letter"** and you still don't know how to fix your code or how to interpret the errors, you can update your post by including what those errors were. – Funk Forty Niner Jan 14 '19 at 06:30
  • Hello, sorry for no reply i got it fixed now, like i know whats going on. I do not expect people to fix it for me as i just started with code and i want to learn on my own. Thanks guys for the help – XJL Jan 14 '19 at 06:35

1 Answers1

-1

Use below code...

$stmt->bind_param('ssssssss',$employ_first,$employ_second, $reason_emp,$type,$set_on_date,$set_on_time,$set_by,$assigned_to);

$employ_first = $_POST['employ_first'];
$employ_second = $_POST['employ_second']; 
$reason_emp = $_POST['reason_emp'];
$type = $_POST['type']; 
$set_on_date = $_POST['set_on_date']; 
$set_on_time = $_POST['set_on_time']; 
$set_by = $_SESSION['set_by']; 
$assigned_to = $_POST['assigned_to'];
$stmt->execute();
echo "Meeting has been created";

$stmt->close();
PHP_only
  • 88
  • 9