hi

i have a contact form that i want to submit to a dater base and here is my code to do that

<?php require_once('Connections/mywigan.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO email_messages (id, email, subject, message) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['subject'], "text"),
                       GetSQLValueString($_POST['message'], "text"));

  mysql_select_db($database_mywigan, $mywigan);
  $Result1 = mysql_query($insertSQL, $mywigan) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Email:</td>
      <td><input type="text" name="email" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Subject:</td>
      <td><input type="text" name="subject" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right" valign="top">Message:</td>
      <td><textarea name="message" cols="50" rows="5"></textarea>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="id" value="">
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>

but when the form submits and entered into my daterbase i want a email to be sent to me saying that a entery has been an enter into my dater base. What is the best way to get around this.


thanks for your help and time

Dani AI

Generated

posted a working quick fix (calling PHP mail() after the insert). That gets the job done, but for reliability and safety there are a few improvements worth making: only send the notification after the database insert is confirmed, stop execution after sending a redirect, validate and sanitize user input, and move away from the old mysql_* pattern to prepared statements so the code works on modern PHP. See the PHP docs for prepared statements PDO prepared statements and for how header() behaves header().

Recommended checklist:

  • Validate the submitter email with filter_var before inserting: filter_var.
  • Use PDO or mysqli prepared statements to avoid SQL injection and to support current PHP versions.
  • Send notifications with an SMTP-capable library (PHPMailer) rather than relying on the basic mail() API for better deliverability: PHPMailer.
  • Only redirect after the insert and email attempts succeed, and call exit; immediately after header() to stop further execution.
  • Strip CR/LF from any header fields or avoid placing raw user input in headers to prevent header injection.

Minimal flow example (illustrative, not a drop-in):

$email = $_POST['email'] ?? '';
$subject = $_POST['subject'] ?? '';
$message = $_POST['message'] ?? '';

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { /* handle invalid */ }

$pdo = new PDO($dsn, $user, $pass, $opts);
$stmt = $pdo->prepare('INSERT INTO email_messages (email,subject,message) VALUES (:email,:subject,:message)');
if ($stmt->execute([':email'=>$email, ':subject'=>$subject, ':message'=>$message])) {
  // send notification via PHPMailer (SMTP) and check its result
  // then redirect
  header('Location: index.php');
  exit;
}

If mail does not arrive, check your server mail logs and SPF/DKIM settings and test sending via authenticated SMTP. Also log failures from your mail library so you can diagnose delivery problems.

i have solved it i have done this

<?php require_once('Connections/mywigan.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO email_messages (id, email, subject, message) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['subject'], "text"),
                       GetSQLValueString($_POST['message'], "text"));

  mysql_select_db($database_mywigan, $mywigan);
  $Result1 = mysql_query($insertSQL, $mywigan) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

?>
<?php

$to      = 'emailadress';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: emailadress@shooting.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Email:</td>
      <td><input type="text" name="email" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Subject:</td>
      <td><input type="text" name="subject" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right" valign="top">Message:</td>
      <td><textarea name="message" cols="50" rows="5"></textarea>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="id" value="">
  <input type="hidden" name="MM_insert" value="form1">
</form>

<p>&nbsp;</p>

thanks for you help
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.