0

I'm trying to insert some data into a mysql table but the process doesnt go through and I get nothing printed for the error. I don't think I'm outputting the errors correctly. Looking at php.net it seems like I'm doing the error handling properly but maybe I'm missing something.

Here is the code

$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sqlInsert = $db->query("INSERT INTO transactions(`user_id`, `courses`, `invoice`, `start_date`, `end_date`, `mc_gross`, `email`) VALUES ('".$user_id."','".$trim."','".$invoice."','".$start_date."','".$end_date."', '".$email."')");


if($sqlInsert)
    {
    echo "Inserted successfully";
    }
    else
      {
       printf("Error: ", $db->error);

       }

The values for the variables are as follows

$custom = $_SESSION['custom'];
$user_id = $_POST['userID'];
$pwd = $_POST['password'];
$email = $_POST['email'];
$start_date = date('Y-m-d');
$end_date = date (('Y-m-d'), strtotime('+120 days'));
$invoice = date('Ymd'). mt_rand(1252,10000);
$invoice_check = $db->query("SELECT `invoice` FROM `transactions` WHERE `invoice` = $invoice");
while ($rows = $invoice_check->fetch_assoc())
{
    $invoice = date('Ymd'). mt_rand(1252,10000);   
}
$mc_gross = $_SESSION['subtotal'];
$trim = rtrim($custom, ",");

the var_dump for $trim is string(17) "bio_01,calculus_01" and the other variables just echo out normally as you'd expect.

Any ideas?

EDIT Updated the code with $db instead of $sqlInsert. Still no output for an error.

7
  • 1
    You have 7 INSERTs and 6 VALUES. Seems like you're missing the 7th VALUE for courses Commented Nov 5, 2013 at 1:10
  • Since $sqlInsert is false, you obviously can't use $sqlInsert->error, because false is not an object. You have to use $db->error. Commented Nov 5, 2013 at 1:11
  • I've tried $db->error as well, it's just blank without any output Commented Nov 5, 2013 at 1:12
  • @Fred-ii- would the error handling tell me that I'm missing a value? Also, I put the value in.. still not getting anything Commented Nov 5, 2013 at 1:12
  • @Fahad Yes, it would say Column count doesn't match value count. Commented Nov 5, 2013 at 1:13

1 Answer 1

1

Change:

printf("Error: ", $sqlInsert->error);

to:

printf("Error: %s", $db->error);

You can't read the error property of false, because it's not an object. And you're missing the %s in the printf format string to substitute the argument.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you! the %s was the problem. I didn't really notice it I guess because I didnt know what it was for
Why don't you just use echo?
I tried originally with echo but for some reason I wasn't getting an output either. I don't really know why. I saw php.net had printf so i tried using that instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.