如何正确地做一个mysqli准备

时间:2014-09-15 03:03:54

标签: php forms mysqli

<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):

if (isset($_POST['myname'])) { $myname = $_POST['myname']; } else { $myname = ''; }
if (isset($_POST['mypassword'])) { $mypassword = $_POST['mypassword']; } else { $mypassword = ''; }
if (isset($_POST['mypasswordconf'])) { $mypasswordconf = $_POST['mypasswordconf']; } else { $mypasswordconf = ''; }
if (isset($_POST['mycomments'])) {
    $mycomments = filter_var($_POST['mycomments'], FILTER_SANITIZE_STRING ); }
    else { $mycomments = ''; }
if (isset($_POST['reference'])) { $reference = $_POST['reference']; } else { $reference = ''; }
if (isset($_POST['favoritemusic'])) { $favoritemusic = $_POST['favoritemusic']; } else { $favoritemusic = array(); }
if (isset($_POST['requesttype'])) { $requesttype = $_POST['requesttype']; } else { $requesttype = ''; }
if (isset($_POST['ajaxrequest'])) { $ajaxrequest = $_POST['ajaxrequest']; } else { $ajaxrequest = ''; }

    $formerrors = false;

    if ($myname === '') :
        $err_myname = '<div class="error">Sorry, your name is a required field</div>';
        $formerrors = true;
    endif; // input field empty

    if (strlen($mypassword) <= 6):
        $err_passlength = '<div class="error">Sorry, the password must be at least six characters</div>';
        if ( $ajaxrequest ) { echo "<script>$('#mypassword').after('<div class=\"error\">Sorry, the password must be at least six characters</div>');</script>"; }
        $formerrors = true;
    endif; //password not long enough


    if ($mypassword !== $mypasswordconf) :
        $err_mypassconf = '<div class="error">Sorry, passwords must match</div>';
        if ( $ajaxrequest ) { echo "<script>$('#mypasswordconf').after('<div class=\"error\">Sorry, passwords must match</div>');</script>"; }
        $formerrors = true;
    endif; //passwords don't match


    if ( !(preg_match('/[A-Za-z]+, [A-Za-z]+/', $myname)) ) :
        $err_patternmatch = '<div class="error">Sorry, the name must be in the format: Last, First</div>';
        $formerrors = true;
    endif; // pattern doesn't match

  $favoritemusic1 = implode(",", $favoritemusic);
  $formid1 = " ";

  $formdata = array (
    'myname' => $myname,
    'mypassword' => $mypassword,
    'mypasswordconf' => $mypasswordconf,
    'mycomments' => $mycomments,
    'reference' => $reference,
    'favoritemusic' => $favoritemusic,
    'requesttype' => $requesttype
  );

  date_default_timezone_set('US/Eastern');
  $currtime = time();
  $datefordb = date('Y-m-d H:i:s', $currtime);
  $salty = dechex($currtime).$mypassword;
  $salted = hash('sha1', $salty);


    if (!($formerrors)) :
        include("log_formdb.php");

        $forminfolink = mysqli_connect($host, $user, $password, $dbname);
        /* check connection */

        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }

        /* create a prepared statement */
        if ($stmt = mysqli_prepare($forminfolink, "INSERT INTO table form_info (forminfo_id, forminfo_ts, myname, mypassword, mycomments, reference, favoritemusic, requesttype) VALUES (?,?,?,?,?,?,?,?")) {

        if ($forminforesult = $stmt):
          $msg = "We will get back with you in a timely manner. Thanks.";
          if ( $ajaxrequest ):
            echo "<script>$('#myform').before('<div id=\"formmessage\"><p>",$msg,"</p></div>'); $('#myform').hide();</script>";
          endif; // ajaxrequest
        else:
          $msg = "Sorry, there was a problem and your data has not been processed.  If this problem continues please send an email to our webmaster at webmaster@email.com.";
          if ( $ajaxrequest ):
            echo "<script>$('#myform').before('<div id=\"formmessage\"><p>",$msg,"</p></div>'); $('#myform').hide();</script>";
          endif; // ajaxrequest
        endif; //write to database

            /* bind parameters for markers */
            mysqli_stmt_bind_param($stmt, "isssssss", $formid1, $datefordb, $myname, $mypassword, $mycomments, $reference, $favoritemusic1, $requesttype);

            /* execute query */
            mysqli_stmt_execute($stmt);

            /* close statement */
            mysqli_stmt_close($stmt);
        }

    endif; // check for form errors

endif; //form submitted
?>

我正在尝试编写我的第一个mysqli prepare语句,将信息从表单发送到服务器。我想我已经挂断了验证......我已经查看了类似的其他问题,并没有看到任何有用的问题。有什么想法吗?

0 个答案:

没有答案