-3
<?php
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("index.php");
exit;
}

include 'header.php';
include './include/sql/connect.php';

if(isset($_POST['submit']))
{
$insert = "INSERT INTO `customers`(`bb_id`, `name`, `email`, `phone`, `circle`, `ssa`, `sdca`) VALUES (?,?,?,?,?,?,?)";

    $stmt = mysqli_prepare($connect, $insert);
    $stmt->bind_param('sssisss', $_POST['bb_id'],$_POST['name'],$_POST['email'],$_POST['phone'],$_POST['circle'],$_POST['ssa'],$_POST['sdca']);
    $stmt->execute();

    if (!$stmt)
    {
        printf("Error: %s\n", mysqli_error($connect));
        exit();
    }
}


$cust = "SELECT * FROM `customers`";
$qry = mysqli_query($connect,$cust);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Customers</title>
    <!--link rel="stylesheet" type="text/css" href="style/fg_membersite.css"-->
    <link rel="stylesheet" type="text/css" href="style/tableview.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<br>
<div id='tableview'>
    <div class='ext-box'>
        <form action="" method="post">
            <input type="text" name="bb_id" placeholder="Broadband ID">&nbsp;
            <input type="text" name="name" placeholder="Name">&nbsp;
            <input type="text" name="email" placeholder="Email">&nbsp;
            <input type="text" name="phone" placeholder="Phone">&nbsp;
            <input type="text" name="circle" placeholder="Circle">&nbsp;
            <input type="text" name="ssa" placeholder="SSA">&nbsp;
            <input type="text" name="sdca" placeholder="SDCA"><br><br>
            <input type="submit" name="searchsubmit" value="Search Records">
            <input type="submit" name="submit" value="Update Records">
        </form>
    </div>
    <br><br><br>
</div>

<table class="blueTable">
<thead>
    <tr>
        <th>Broadband ID</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Circle</th>
        <th>SSA</th>
        <th>SDCA</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <td colspan="7">
            <div class="links"><a href="#">&laquo;</a> <a class="active" href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">&raquo;</a></div>
        </td>
    </tr>
</tfoot>
<tbody>
    <?php while($row = mysqli_fetch_array($qry)):?>
    <tr>
        <td><?php echo $row['bb_id'];?></td>
        <td><?php echo $row['name'];?></td>
        <td><?php echo $row['email'];?></td>
        <td><?php echo $row['phone'];?></td>
        <td><?php echo $row['circle'];?></td>
        <td><?php echo $row['ssa'];?></td>
        <td><?php echo $row['sdca'];?></td>
    </tr>
    <?php endwhile;?>
</tbody>
</table>            


</body>
</html>

The above code is supposed to insert data filled in the form to a DB. And it does that, but what is weird is only sometimes.

Like I am trying to insert data since 2 hours using chrome but it would not do so. Suddenly I did the same thing with IE and it took the insert. I thought maybe browser compatibility, but when I tried again with IE it stopped inserting after doing it once.

  • 1) Eliminate the membersite codes, please. 2) Show us the code from connect.php. 3) Add `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top to display eventual errors. 4) Use ` `. Then give us your feedback again. – PajuranCodes Aug 13 '18 at 21:37

1 Answers1

1

You're not checking whether the execution was successful correctly.

Change:

$stmt->execute();
if (!$stmt) {
    ...
}

to:

if (!$stmt->execute()) {
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks alot. So, it seems I was trying to insert a BIGINT in place of INT. Now I can insert the queries properly, kinda. Another error is coming up now , Error: Duplicate entry for key 'PRIMARY'. Even though the string I am entering is not present. the query gives this error but also inserts the data. – Mohammad Ozair Akhlaq Aug 13 '18 at 21:52
  • Then you have a PRIMARY KEY conflict. If you want to be able to store duplicate rows, you'll need to remove the primary key. Right now, the first is inserted, and subsequent inserts are denied. – mickmackusa Aug 13 '18 at 21:55
  • @MohammadOzairAkhlaq Make sure you're only calling `$stmt->execute()` once – Barmar Aug 13 '18 at 22:00
  • 1
    @MohammadOzairAkhlaq I've seen lots of mistakes like `$stmt->execute(); if ($stmt->execute()) ...` – Barmar Aug 13 '18 at 22:00
  • @mickmackusa but why would it through a duplicate error in the first place if the string is not present already? And no, I don't want to store duplicates that's why I made it a PK. – Mohammad Ozair Akhlaq Aug 13 '18 at 22:01
  • @MohammadOzairAkhlaq What do you mean by _"Even though the string I am entering is not present."_? Which string? Be more explicit, please. And, if that _"Duplicate error"_ is raised, then definitely no data is inserted. So you saw something else or wrong. – PajuranCodes Aug 13 '18 at 22:02
  • @MohammadOzairAkhlaq If `bb_id` is your PK column, then make it `autoincrement` and eliminate it from the sql statement. As a result, no _"Duplicate error"_ will be raised anymore. – PajuranCodes Aug 13 '18 at 22:06
  • @Barmar Thanks alot. I was doing this `$stmt->execute(); if ($stmt->execute()) ...`. – Mohammad Ozair Akhlaq Aug 13 '18 at 22:08