houdriwiokug houdriwiokug - 3 months ago 9
MySQL Question

PHP Does Not Insert a New Data Into The Table

I have made a simple php application which can retrieve data from database and show it on table. But the problem is that ,it does not work at all! I mean whenever I submit the form ,it does not insert a new query to database for some reasons. Also there's no error appearing on the screen so I don't know what is the problem. Here's the full code of my page:

<?php
error_reporting(E_ALL);
require 'connect.php';
require 'security.php';
$records = array();
if(!empty($_POST)){
if(isset($_POST['site_name'], $_POST['site_admin'], $_POST['site_desc'], $_POST['site_url'], $_POST['made_by'])){
$sitename = trim($_POST['site_name']);
$siteadmin = trim($_POST['site_admin']);
$sitedesc = trim($_POST['site_desc']);
$siteurl = trim($_POST['site_url']);
$madeyby= trim($_POST['made_by']);

if(!empty($sitename) && !empty($siteadmin) && !empty($sitedesc) && !empty($siteurl) && !empty($madeyby)){
$insert = $db->prepare("INSERT INTO list_of_sites (site_name, site_admin, site_desc, site_url, made_by, date_created) VALUES (?, ?, ?, ?, ?, NOW())");
$insert->bind_param('sssss',$sitename,$siteadmin,$sitedesc,$siteurl,$madeby);

if($insert->execute()){
header('Location: index.php');
die();
}
}
}
}
if($results = $db->query("SELECT * FROM list_of_sites")){
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Daygotar Application</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
/**********************************/
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}

input[type=text]:focus {
width: 100%;
}
#searchForm{
float:right;
top:0;
margin-bottom:20px;
}
</style>
</head>
<body>
<form id="searchForm">
<input type="text" name="search" placeholder="Search..">
</form>
<h3 style="font-size:30px;"><u>List Of Sites</u></h3>
<?php
if(!count($records)){
echo 'No records';
}else{

}
?>
<table>
<tr>
<th>Site Name</th>
<th>Site Admin</th>
<th>Site Description</th>
<th>Site URL</th>
<th>Made By</th>
<th>Date Created</th>
</tr>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo escape($r->site_name); ?></td>
<td><?php echo escape($r->site_admin); ?></td>
<td><?php echo escape($r->site_desc); ?></td>
<td><a title="goto this site" href="<?php echo escape($r->site_url); ?>" target="_blank"><?php echo escape($r->site_url); ?></a></td>
<td><?php echo escape($r->made_by); ?></td>
<td><?php echo escape($r->date_created); ?></td>
</tr>
<?php
}
?>
</table>
</hr style="margin-top:20px;">
<form action="" method="POST">
<div class="field">
<label for="site_name"><h4>Site name</h4></label>
<input type="text" name="site_name" id="site_name" autocomplete="off"></input>
</div>
<div class="field">
<label for="site_admin"><h4>Site admin</h4></label>
<input type="text" name="site_admin" id="site_name" autocomplete="off"></input>
</div>
<div class="field">
<label for="site_description"><h4>Site description</h4></label>
<input type="text" name="site_desc" id="site_desc" autocomplete="off"></input>
</div>
<div class="field">
<label for="site_url"><h4>Site url</h4></label>
<input type="text" name="site_url" id="site_name" autocomplete="off"></input>
</div>
<div class="field">
<label for="made_by"><h4>Made by</h4></label>
<input type="text" name="made_by" id="site_name" autocomplete="off"></input>
</div></br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>


Please if you know how to solve this question ,please let me know ... thanks.

Answer

So I've tweaked it slightly just to improve it somewhat for you to tidy it up a little and add conditions to each if !empty statement.

Within the insert query your values need to be added which I have done by using '$variable' under each.

<?php
error_reporting(E_ALL);
require 'connect.php';
require 'security.php';
$records = array();
    if(isset($_POST['send'])) {
        $sitename = trim($_POST['site_name']);
        $siteadmin = trim($_POST['site_admin']);
        $sitedesc = trim($_POST['site_desc']);
        $siteurl = trim($_POST['site_url']);
        $madeyby = trim($_POST['made_by']);

        if (!empty($sitename)) {
            if (!empty($siteadmin)) {
                if (!empty($sitedesc)) {
                    if (!empty($siteurl)) {
                        if (!empty($madeyby)) {

                            $insert = $db->prepare("INSERT INTO list_of_sites (site_name, site_admin, site_desc, site_url, made_by, date_created) VALUES ('$sitename','$siteadmin','$sitedesc','$siteurl','$madeby', NOW())");
                            $insert->execute();

                        } else {
                            echo "Please fill in the Made by field.";
                        }
                    } else {
                        echo "Please add a site url.";
                    }
                } else {
                    echo "Please add a site description.";
                }
            } else {
                echo "Please add a site admin name.";
            }
        } else {
            echo "Please add a site name.";
        }
    }

Then within the HTML section change the name="" to

<input type="submit" name="send" value="Submit">

Comments