Jump to content

Else Statement


Xtremer360

Recommended Posts

I'm just trying something new out here and wanted to know how it looked AND I'm having a hard time figuring out how to accomplish this with the else statement. Obviously on the else it would mean that there was already a template with the same name existing in the database table so I want the user to be sent back to the original form and I'll implement a jquery dialog box telling the user that there is already an existing template with that name.

 

if (isset($_POST['edittemplate'])) {
    $templatename = mysqli_real_escape_string($dbc, $_POST['templatename']);
    $headercode = mysqli_real_escape_string($dbc, $_POST['headercode']);
    $footercode = mysqli_real_escape_string($dbc, $_POST['footercode']);
    $templateID = (int)$_POST['templateID'];

    $query = "SELECT * FROM `templates` WHERE `templatename` = $templatename";
    $result = mysqli_query ( $dbc, $query ); // Run The Query
    $rows = mysqli_num_rows($result);
    
    if ($rows == 0) {
        $query = "UPDATE `templates` 
            SET `templatename` = '$templatename', `headercode`='".$headercode."', `footercode`='".$footercode."' WHERE `id` = '$templateID'";  
    
    mysqli_query($dbc,$query);
    
    } else {
        
    }
    
    
}

Link to comment
Share on other sites

$query = "SELECT * FROM `templates` WHERE `templatename` = $templatename";
if ($result = mysqli_query ( $dbc, $query )) {
  if (!mysqli_num_rows($result)) {
    $query = "
      UPDATE `templates`
      SET `templatename` = '$templatename', `headercode`='".$headercode."', `footercode`='".$footercode."'
      WHERE `id` = '$templateID'
    ";  
    
    if (mysqli_query($dbc,$query)) {
      if (mysqli_affected_rows($dbc)) {
        // update successful.
      } else {
        trigger_error("UPDATE Failed - $query - " . mysqli_error());
      }
    } else {
      trigger_error("Update Failed - $query - " . mysqli_error());
    }
  } else {
    // template already exists
  }
} else {
  trigger_error("SELECT Failed - $query - " . mysqli_error());
}

Link to comment
Share on other sites

Okay maybe I didn't phrase it in the form of a question.

 

Obviously on the else it would mean that there was already a template with the same name existing in the database table so I want the user to be sent back to the original form and I'll implement a jquery dialog box telling the user that there is already an existing template with that name.

Link to comment
Share on other sites

So, redirect the user back to the form with a flag set that will trigger a pop-up.

 

header("Location: http://yourdomain.com/form.php?m=template-exists");

 

If you need to repopulate the form with the data they have already submitted you would need to store that data in the $_SESSION array before redirecting, then retrieve it again on the form page.

 

As for triggering the pop-up, that's a Javascript question.

Link to comment
Share on other sites

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.