插入SQL有时可能有效,有时却无效

时间:2019-05-31 16:03:07

标签: php

请帮助我解决这个奇怪的问题。我在下面提供了我的代码,它有时可以工作,有时会给出“未插入”输出。请帮助

许多专家建议我尝试各种插入方法,但这是有时有效但有时无效的唯一方法。

<?php

session_start();
// Check if the user is logged in, if not then redirect him to login page
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
    header("location: index.php");
    exit;
}
$con = mysqli_connect('localhost', 'root', ''); // Establishing Connection with Server
if (!$con) {
    echo "Not connected to the server";
}
if (!mysqli_select_db($con, 'cocms')) {
    echo "Database not found";
}
//$db = mysql_select_db("cocms", $con); // Selecting Database from Server
// Fetching variables of the form which travels in URL
$cstID = $_POST['cstID'];
$cname = $_POST['cname'];
$caddres = $_POST['caddres'];
$chip = $_POST['chip'];
$cmobilen = $_POST['cmobilen'];
$pkg = $_POST['pkg'];
$total = "";
$total = ($pkg + (($pkg * 18) / 100));
$strtdate = $_POST['strtdate'];
$enddate = "SELECT date_add($strtdate, INTERVAL 30 DAY))";
//$validity = $_POST['validity'];
$discount = $_POST['discount'];

//$sql = mysql_query("SELECT cstID FROM custinfo WHERE cstID = ?");

//Insert Query of SQL
$query = "INSERT INTO custinfo (`cstID`, `cname`, `caddres`, `chip`, 
    `cmobilen`, `pkg`, `total`, `strtdate`, `enddate`, `discount`) VALUES 
    ('$cstID', '$cname', '$caddres', '$chip', '$cmobilen', '$pkg', '$total', 
    '$strtdate', '$enddate', '$discount')";
if (!mysqli_query($con, $query)) {
    echo "Not inserted";
} else {
    echo "Inserted";
}
mysqli_close($con); // Closing Connection with Server

//header("location: dashboard.php");
}

0 个答案:

没有答案