Php form submit is not inserting into database

That's right, it should be very simple php, however I can't figure out for life why this doesn't work.

I have a form that, on submission, has to grab the fields and then insert the data into the database. I have a section on my index.php that actually drops information from the database and works fine, so I can't see that it is a connection issue.

db_connection.php I hashed the information here as it is for my database, but everything is correct

<?php
define("DB_SERVER","**********");
define("DB_USER","*************");
define("DB_PASS","*********");
define("DB_NAME","reviews");

$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

if(mysqli_connect_errno()){
    die("database connection failed");
}


?>

      

functions.php

<?php
function redirect_to($new_location) {
      header("Location: " . $new_location);
      exit;
    }    
function confirm_query($result){
    if(!$result){
        die("database query failed");
    }
}
function find_reviews($connection){
    $query  = "SELECT * ";
    $query .= "FROM reviews";
    $result = mysqli_query($connection,$query);
    confirm_query($result);
    while($reviews = mysqli_fetch_assoc($result)){
        $output = "<li class=\"first\">";
        $output .= $reviews["name"];
        $output .= "</li>";
        $output .= "<li class=\"second\">";
        $output .= $reviews["company"];
        $output .= "</li>";
        $output .= "<li class=\"third\">";
        $output .= $reviews["comment"];
        $output .= "</li>";
        $output .= "<li class=\"line\"></li>";     
        echo $output;
    }
    mysqli_free_result($result);

    return $reviews;
}
?>

      

index.php Note that the find_reviews () function works and grabs information from the database. Also, the above "db_connection.php" and "functions.php" are required in index.php

<?php
require("includes/header.php");
$page_title = "All County Road Markings";
$description = "Specialising in Road Marking & Car Park Lining. We are a professional established road marking service with over 20 years experience";?>
<?php require("includes/db_connection.php"); ?>
<?php require("includes/functions.php"); ?>

<?php if(isset($_POST["submit"])){
    $name = $_POST["name"];
    $company = $_POST["company"];
    $comment = $_POST["comment"];
    $query = "INSERT INTO `reviews` (name,company,comment) VALUES ('$name','$company','$comment')";

    $result=mysqli_query($connection,$query);
    confirm_query($result);
    redirect_to("index.php");
}
?>
<div id="banner"></div>
<div id="paragraph">
    <h2>All County Road Markings are a professional established road marking<br>service with over 20 years experience within the industry</h2>
    <hr style="width: 1050px;">
</div>
<div id="content">
    <div id="left">
        <div class="slot">
            <div class="top carpark"> </div>
            <div class="linkbar">
                <h1>Car Parks</h1>
            </div><div class="linktext">
                <ul class="comments display">
                    <li>- Car Parking Bays</li>
                    <li>- Disabled Parking Bays</li>
                    <li>- Parent and Child Bays</li>
                    <li>- Lettering</li>
                    <li>- Hatchings</li>
                    <li>- Arrows</li>
                    <li>- Customised Lettering</li>
                </ul>
            </div>
        </div>
        <div class="slot">
            <div class="top roadmark"> </div>
            <div class="linkbar">
                <h1>Road Markings</h1>
            </div>
            <div class="linktext">
                <ul class="comments display">
                    <li>- Hatchings</li>
                    <li>- Centre Lines</li>
                    <li>- Double Yellow lines</li>
                    <li>- Give way junctions</li>
                    <li>- Reinstatement of existing markings</li>
                </ul>
            </div>
        </div>
        <div class="slot">
            <div class="top sportcourt">
            </div>
            <div class="linkbar">
                <h1>Sports/Playground Court</h1>
            </div>
            <div class="linktext">
                <ul class="comments display">
                    <li>- Tennis Court</li>
                    <li>- Basketball Court</li>
                    <li>- 5 A side Court</li>
                    <li>- Netball Court </li>
                    <li>- Reinstatement of existing markings</li>
                    <li>- Custom designs available</li>
                </ul>
            </div>
        </div>
    </div>
    <div id="right">
        <div id="rightbar">
            <h2 style="color: white;">Testimonials</h2>
        </div>
        <div id="comment">
            <ul class="comments">
                <?php echo find_reviews($connection); ?>
            </ul>
        </div>
        <div id="write">
            <p style="margin: 0px; padding-top: 5px;color: grey; font-size: 1.25em;">Click to write a review...<p>
        </div>
    </div>
    <a href="contact.php"><div id="quote"><div class="link-text">contact us</div></div></a>
</div>
<div id="add">
    <div id="close"></div>
    <form action="index.php" method="POST">
        <div id="name"> Name:<br/><span>Please Enter Full Name</span>
            <input type="text" name="name" id="textbox">
        </div>
        <div id="company"> Company<br/><span>Please Enter Company Name</span>
            <input type="text" name="company" id="textbox1">
        </div>
        <div id="review"> Review<br/><span>Please Enter Review</span>
            <textarea name="comment" id="reviewComment"></textarea>
        </div>
        <div id="save">
            <input type="submit" name="submit">
        </div>
    </form>
</div>
</body>
</html>

      

ok, can anyone see anything i am doing wrong? like when the form is submitted, it just isn't inserted into the database.

EDIT ::

when i try echo mysqli-> error; I am getting the following error:

 Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /hermes/bosoraweb140/b484/ipg.allcountyroadmarking/index.php on line 17 

      

So, I changed it to

if(!results){
     echo "hello";
}

      

and I don't see the "hello" so it indicates that I didn't even get into the part that deals with post fields

SELF RESOLVED :::

Ive fixed this, I changed action = "index.php" to action = ""

I don't understand, it should have worked, but for some reason the action worked. both must be acceptable!

+3


source to share


3 answers


Form action that was set to

 <form action="index.php" method="POST">

      

doesn't work, however I don't understand why not as it links to the current page.



However, changing that to this will resolve the issue.

 <form action="" method="POST">

      

0


source


The if statement for $ _POST ['submint'] can be false on every view. Maybe try it! Empty ($ _ POST)



0


source


Good thing you got php in the same file so maybe why action = "works if the default is the same page .. action =" index.php "should add the current web address. Maybe try putting the full web address in action = "url for index.php" to see if it works, if it does, then action = "index.php" should not go to the correct file.

0


source







All Articles