Making sure an insert works

I have the following code to execute my query in mysql/php

$stmt->execute(array(
    ':name' => $name,
    ':email' => $email,
    ':phone' => $phone,
    ':c_method' => $c_method,
    ':rate' => $rate,
    ':age' => $age,
    ':feet' => $feet,
    ':inches' => $inches,
    ':weight' => $weight,
    ':waist' => $waist,
    ':cup' => $cup,
    ':ethnicity' => $ethnicity,
    ':intro' => $intro,
    ':thumb' => $thumb,
    ':display' => $display,
    ':date_created' => $date_created
  ));

but if I want to see if it actually works, do I do this?

if($stmt->execute(array(
    ':name' => $name,
    ':email' => $email,
    ':phone' => $phone,
    ':c_method' => $c_method,
    ':rate' => $rate,
    ':age' => $age,
    ':feet' => $feet,
    ':inches' => $inches,
    ':weight' => $weight,
    ':waist' => $waist,
    ':cup' => $cup,
    ':ethnicity' => $ethnicity,
    ':intro' => $intro,
    ':thumb' => $thumb,
    ':display' => $display,
    ':date_created' => $date_created
  ))) {
echo "success";
}

I guess it depends on what you mean by “works”

PDOStatement::execute

Returns TRUE on success or FALSE on failure.

mysqli_stmt::execute

Returns TRUE on success or FALSE on failure.

ok, then I got a question about my try block…

try {
	
$sql = "INSERT INTO providers (
  name,
  email,
  ...
  ) VALUES (
  :name,
  :email,
   ...)";
  
$stmt = $dbh->prepare($sql);

$stmt->execute(array(
		':name' => $name,
		':email' => $email,
		...
	  ));
	  
$primaryKey = $dbh->lastInsertId();
	
mkdir("providers/{$primaryKey}"); 
mkdir("providers/{$primaryKey}/bad"); 
mkdir("providers/{$primaryKey}/thumbs");
  
mail("insert@houston-asp.com","Provider (".date( 'm/d/y', strtotime($date_created)).")",$message,$headers);

} catch(PDOException $e) {
	echo '<div class="panel panel-default"style="margin:75px auto; width:960px">';	
  	echo '<div class="panel-body">';
 	echo '<h4 class="text-danger">';
 	echo '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> The email address ('.$email.') is being used by a provider.</h4>';
	echo "<br><a href='logout.php'>Try again (logout)?</a>";
	echo '</div></div>';
    echo 'Error: ' . $e->getMessage();
}

I notice the INSERT statement only works when a unique email address is used (thats good as that field has a unique constraint in the mysql table). As a test I input the same email address ( so I thought the code in the catch block would be executed) and this is the result…

Warning: mkdir(): File exists in /home/luke69/public_html/thanks_provider.php on line 237

Warning: mkdir(): File exists in /home/luke69/public_html/thanks_provider.php on line 238

Warning: mkdir(): File exists in /home/luke69/public_html/thanks_provider.php on line 239

how can I make sure the three directories and the mail is sent only when a $primaryKey is set?
and the Catch block will run if it isn’t?

Do you have PDO configured to throw an exception for that kind of error?

http://php.net/manual/en/pdo.error-handling.php

I thought I did but guess I dont (this try/catch doesn’t display any error if the INSERT doesn’t go through)

try {
	
$sql = "INSERT INTO providers (
  name,
  email,
  ...
  ) VALUES (
  :name,
  :email,
  ...)";
  
$stmt = $dbh->prepare($sql);

$stmt->execute(array(
		':name' => $name,
		':email' => $email,
                ...
	  ));


} catch(PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}

how do I do what you suggest?

I guess all I had to do is

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

That seemed to be what the article I linked to was suggesting.

2 Likes

I want to see if it actually works

The question is why do you want it. The proper answer to your question depends on this one.

Assuming you want to show an ornate error message, as in your other question, let me warn you against it. Mind you’re [hopefully] not a sole user of your site. And this error message you’re putting so much effort on will be shown to a user who will have no idea what to do with it. while you will have no idea there was an error or what caused it.

how can I make sure the three directories and the mail is sent only when a $primaryKey is set?

That’s really easy. Set PDO in exception mode as explained here and get rid of the try..catch stuff in your code. And you will never see an mkdir command executed if insert fails.

thanks droopsnot, after putting that line of code in, if I try to enter a duplicate email the catch block is run, i commented out the error code cause its really only for me. Can I put a mail() in it so the error get emailed to me instead? how

catch(PDOException $e) {
	echo '<div class="panel panel-default"style="margin:75px auto; width:960px">';	
  	echo '<div class="panel-body">';
 	echo '<h4 class="text-danger">';
 	echo '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> The email address ('.$email.') is being used by a provider.</h4>';
	echo '<center><img src="images/fail.jpg" class="img-thumbnail img-responsive" /></center>';
	echo "<br><a href='logout.php'>Try again (logout)?</a>";
	echo '</div></div>';
   // echo 'Error: ' . $e->getMessage();
}

Sorry, colshrapnel but I really dont want to get in over my head…
so, are you saying to change

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {
	
$sql = "INSERT INTO providers (
  name,
  email,
  ...
  ) VALUES (
  :name,
  :email,
  ...)";
  
$stmt = $dbh->prepare($sql);

$stmt->execute(array(
		':name' => $name,
		':email' => $email,
		...
	  ));
	  
$primaryKey = $dbh->lastInsertId();
	
mkdir("providers/{$primaryKey}"); 
mkdir("providers/{$primaryKey}/bad"); 
mkdir("providers/{$primaryKey}/thumbs");
...
...

} catch(PDOException $e) {
	echo '<div class="panel panel-default"style="margin:75px auto; width:960px">';	
  	echo '<div class="panel-body">';
 	echo '<h4 class="text-danger">';
 	echo '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span> The email address ('.$email.') is being used by a provider.</h4>';
	echo '<center><img src="images/fail.jpg" class="img-thumbnail img-responsive" /></center>';
	echo "<br><a href='logout.php'>Try again (logout)?</a>";
	echo '</div></div>';
   // echo 'Error: ' . $e->getMessage();
}

to

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$sql = "INSERT INTO providers (
  name,
  email,
  ...
  ) VALUES (
  :name,
  :email,
  ...)";
  
$stmt = $dbh->prepare($sql);

$stmt->execute(array(
		':name' => $name,
		':email' => $email,
		...
	  ));
	  
$primaryKey = $dbh->lastInsertId();
	
mkdir("providers/{$primaryKey}"); 
mkdir("providers/{$primaryKey}/bad"); 
mkdir("providers/{$primaryKey}/thumbs");

Thanks…
which way would be to best?

errors should be written to the error log and not displayed in the web page

2 Likes

so thats using the try/catch thing? Do I add this to the catch block?

...
error_log("Error message\n", 3, "/mypath/php.log");

or something?

which way would be to best?

Surely the latter.

Do I add this to the catch block?

No.

Just add this single line once somewhere at the top of your code:

ini_set("log_errors", 1);

and have all errors logged, even ones you never had an idea to wrap in a try, catch or any other block.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.