Jump to content

data is not being inserted i get query failed?


jrphplover

Recommended Posts

please see what im doing wrong and how do i insert date into datetime field in database?

 

 

$qry = "INSERT INTO leads ('agentID, customerName, customerAddress, homePhone, altPhone, loanAmount, monthlyPayment, paymentPeriod,
ssn, dl, sid, monthlyIncome, lastPayday, nextPayday, dob, transferNumber, applicationNumber, comments, date_time) VALUES ('".$agentID."', '".$customerName."', '".$customerAddress."', '".$homePhone."', '".$altPhone."', '".$loanAmount."', '".$monthlyPayment."', '".$repaymentPeriod."',
'".$ssn."', '".$dl."', '".$sid."', '".$monthlyIncome."', '".$lastPayday."', '".$nextPayday."', '".$dob."', '".$transferNumber."', '".$applicationNumber."', '".$comments."', 'CURRENT_TIMESTAMP')";
    $result = @mysql_query($qry);
    
    print_r($qry);
    
    //Check whether the query was successful or not
    if($result) {
        header("location: record-success.php");
        exit();
    }else {
        die("Query failed");

HINT: There's another function in the PHP mysql (deprecated, BTW) library. It's called mysql_error(), and it prints out the last error mysql encountered. Might come in handy here, as well as not suppressing errors/warnings with the @ sign.

If you want to insert current date and time to database, you can use mysql NOW() fuction with your insert query.

 

Try this.

$qry = "INSERT INTO leads ( agentID, customerName, customerAddress, homePhone, altPhone, loanAmount, 
			    monthlyPayment, paymentPeriod, ssn, dl, sid, monthlyIncome, lastPayday, 
			    nextPayday, dob, transferNumber, applicationNumber, comments, date_time
			  ) VALUES 
			  ('$agentID', '$customerName', '$customerAddress', '$homePhone', '$altPhone', 
			   '$loanAmount', '$monthlyPayment', '$repaymentPeriod', '$ssn', '$dl', '$sid', 
			   '$monthlyIncome', '$lastPayday', '$nextPayday', '$dob', '$transferNumber', 
			   '$applicationNumber', '$comments', now())";

Side note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated, read this . Learn about prepared statements  instead, and use PDO or MySQLi  - this article  will help you decide which.
 

still its not inserting eh what is wrong here? here is the entire coding

 

<?php
    //Start session
    session_start();
    
    //Include database connection details
    require_once('config.php');
    
    //Array to store validation errors
    $errmsg_arr = array();
    
    //Validation error flag
    $errflag = false;
    
        //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
    
    //Sanitize the POST values
    $agent = clean($_SESSION['SESS_LOGIN_NAME']);
    $customerName = clean($_POST['customerName']);
    $customerAddress = clean($_POST['customerAddress']);
    $homePhone = clean($_POST['homePhone']);
    $altPhone = clean($_POST['altPhone']);
    $loanAmount = clean($_POST['loanAmount']);
    $monthlyPayment = clean($_POST['monthlyPayment']);
    $repaymentPeriod = clean($_POST['repaymentPeriod']);
    $ssn = clean($_POST['ssn']);
    $dl = clean($_POST['dl']);
    $sid = clean($_POST['sid']);
    $monthlyIncome = clean($_POST['monthlyIncome']);
    $lastPayday = clean($_POST['lastPayday']);
    $nextPayday = clean($_POST['nextPayday']);
    $dob = clean($_POST['dob']);
    $transferNumber = clean($_POST['transferNumber']);
    $applicationNumber = clean($_POST['applicationNumber']);
    $comments = clean($_POST['comments']);

        
    //Input Validations
    if($customerName == '') {
        $errmsg_arr[] = 'Customer name is missing';
        $errflag = true;
    }
    if($customerAddress == '') {
        $errmsg_arr[] = 'Customer address is missing';
        $errflag = true;
    }
    if($loanAmount == '') {
        $errmsg_arr[] = 'Loan amount is missing';
        $errflag = true;
    }
    
    //Check for duplicate login ID
    if($homePhone != '') {
        $qry = "SELECT * FROM leads WHERE homePhone='$homePhone'";
        $result = mysql_query($qry);
        if($result) {
            if(mysql_num_rows($result) > 0) {
                $errmsg_arr[] = 'Customer with this number already exist';
                $errflag = true;
            }
            @mysql_free_result($result);
        }
        else {
            die("Query failed");
        }
    }
    
    //If there are input validations, redirect back to the registration form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: add-record.php");
        exit();
    }

    //Create INSERT query
    $qry = "INSERT INTO leads ( agent, customerName, customerAddress, homePhone, altPhone, loanAmount,
                monthlyPayment, paymentPeriod, ssn, dl, sid, monthlyIncome, lastPayday,
                nextPayday, dob, transferNumber, applicationNumber, comments, date_time
              ) VALUES
              ('$agent', '$customerName', '$customerAddress', '$homePhone', '$altPhone',
               '$loanAmount', '$monthlyPayment', '$repaymentPeriod', '$ssn', '$dl', '$sid',
               '$monthlyIncome', '$lastPayday', '$nextPayday', '$dob', '$transferNumber',
               '$applicationNumber', '$comments', now())";
    $result = @mysql_query($qry);
    
    print_r($qry);
    
    //Check whether the query was successful or not
    if($result) {
        header("location: record-success.php");
        exit();
    }else {
        die("Query failed");
    }
?>

Change this line:

 

$result = @mysql_query($qry);

To:

$result = mysql_query($qry) or trigger_error(mysql_error());

 

Make sure your error reporting is on, if it isn't then use:

 

$result = mysql_query($qry) or exit(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.