在phpmyadmin中将表格中的数据写入表格

时间:2016-05-02 15:36:53

标签: php mysql phpmyadmin

我是初学者,请耐心等待我!!!

我正在将表格中的数据写入我的phpmyadmin表格中。此外,它会发送一封确认电子邮件,让用户知道您的信息已成功提交。

这是表单页面:

enter image description here

一旦你填写完它就说:

enter image description here

但它没有发送电子邮件确认,数据没有传递到phpmyadmin中的表。

这不是执行财产,我正在撞墙。请帮帮我

以下是代码:

<?php 
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

$link = mysqli_connect("localhost","warren","*******","warren");//This is the login creditial 

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());//Error check for execution 
}

$contactname = mysqli_real_escape_string($link, $_POST['contactname']);//lastname variable    
$age =  mysqli_real_escape_string($link, $_POST['age']);//age variable  
$gender =  mysqli_real_escape_string($link, $_POST['sex']);//gender variable
$weight =  mysqli_real_escape_string($link, $_POST['weight']);//gender variable 
$height =  mysqli_real_escape_string($link, $_POST['height']);//gender variable 
$membership =  mysqli_real_escape_string($link, $_POST['membership']);//gender variable 
$email =  mysqli_real_escape_string($link, $_POST['email']);//gender variable 


//If the form is submitted
if(isset($_POST['Submit'])) {

    // require a name from user
    if(mysqli_real_escape_string($link, $_POST['contactName']) === '') {
        $nameError =  'Forgot your name!'; 
        $hasError = true;
    } else {
        $contactname = mysqli_real_escape_string($link, $_POST['contactName']);
    }


    // need valid email
    if(mysqli_real_escape_string($link, $_POST['email']) === '')  {
        $nameError = 'Forgot your e-mail address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", mysqli_real_escape_string($link, $_POST['email']))) {
        $emailError = 'Invalid email address!';
        $hasError = true;
    } else {
        $email = mysqli_real_escape_string($link, $_POST['email']);
    }


if (!empty($contactname) && !empty($age) && !empty($gender) && !empty($weight) && !empty($height) && !empty($membership) && !empty($email)){ 

$sql = "INSERT INTO customer(customer_id, contactname, age, gender, weight, height, memebership, email) 
VALUES ('','$contactname', '$age', '$gender' ,'$weight', '$height', '$membership', '$email')";
if(mysqli_query($link,$sql)){
   echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}





mysqli_close($link);
}


    // upon no failure errors let's email now!
    if(!isset($hasError)) {

        $emailTo = 'youremail@email.com'; // ADD YOUR EMAIL ADDRESS HERE FOR CONTACT FORM!
        $subject = 'Submitted message from '.$name; // ADD YOUR EMAIL SUBJECT LINE HERE FOR CONTACT FORM!
        $sendCopy = mysqli_real_escape_string($link, $_POST['sendCopy']);
        $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
        $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);

        // set our boolean completion value to TRUE
        $emailSent = true;
    }
}
?>
<!DOCTYPE html> 
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>The Gym - Personal Training</title>
      <link rel="stylesheet" type="text/css" href="http://thing.cs.usm.maine.edu/~warren/mysql2/css/style.css" />   
      <link href="http://thing.cs.usm.maine.edu/~warren/mysql2/css/font-awesome.min.css" rel="stylesheet"/>
    <meta name="description" content="">
</head>
<body>
    <header>
        <div id="header-inner">
        <a href="index.html" id="logo" title=""></a>
        <nav>
            <a href="#" id="menu-icon"></a>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="about.html">About</a></li>
                <li><a href="facility.html">Facility</a></li>
                <li><a href="pricing.html">Pricing</a></li>
                <li><a href="contact.php" class="current">Contact</a></li>
            </ul>
        </nav>
        </div>
    </header>
<!--- Start Bread Crumbs -->
    <div id="bread-banner">
        <div id="bread-banner-inner">
            <h3>Contact Us</h3>
            <h5 class="bread-here">You are here: HOME / CONTACT</h5>
            <h5 class="bread-phone">555-525-5005</h5>
        </div>
    </div>
<!--- End Bread Crumbs -->
    <div class="clearfix"></div>
        <div id="inner-wrapper">
            <h3 class="text-welcome">We'd be happy to hear from you</h3>
            <div class="line-rule"></div>
<!-- Start Contact Form -->
    <section class="two-third">
    <div id="contact-area">
    <div id="contact" class="section">
        <div class="container content">
            <?php if(isset($emailSent) && $emailSent == true) { ?>
                <p class="info">Your email was sent. HooHaaa!!!</p>
            <?php } else { ?>       
                </div>  
                <div id="contact-form">
                    <?php if(isset($hasError) || isset($captchaError) ) { ?>
                        <p class="alert">Error submitting the form</p>
                    <?php } ?>

                    <form  action="contact.php" method="POST">
                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Name</h5></label>
                            <input type="text" name="contactName" id="contactName" value=" " class="txt requiredField" placeholder="Name:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                            <?php } ?>
                        </div>

                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Age</h5></label>
                            <input type="num" name="age" id="age" value=" " class="txt requiredField" placeholder="Age:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                            <?php } ?>
                        </div>

                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Gender</h5></label>
                            <input type="text" name="sex" id="sex" value=" " class="txt requiredField" placeholder="Gender:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                            <?php } ?>
                        </div>

                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Weight</h5></label>
                            <input type="num" name="" id="weight" value=" " class="txt requiredField" placeholder="Weight:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                            <?php } ?>
                        </div>

                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Height</h5></label>
                            <input type="num" name="" id="height" value=" " class="txt requiredField" placeholder="Height:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                            <?php } ?>
                        </div>

                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Membership</h5></label>
                            <input type="num" name="" id="membership" value=" " class="txt requiredField" placeholder="membership:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                            <?php } ?>
                        </div>

                        <div class="clearfix"></div>
                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Email</h5></label>
                            <input type="text" name="email" id="email" value=" " class="txt requiredField email" placeholder="Email:" />
                            <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span>
                            <?php } ?>
                        </div>
                        <div class="clearfix"></div>
                        <div class="clearfix"></div>  
                            <button name="Submit" type="Submit" class="subbutton"><h5>Submit</h5></button>
                            <input type="hidden" name="Submit" id="Submit" value="true" />
                    </form>         
            <?php } ?>
        </div>
    </div>
            </div>
        </section>
<!-- End Contact Form -->
    </div> 
<!--- End Inner Wrapper -->
    <div class="clearfix"></div>
<!--- Start Footer -->
        <footer>
        <div id="footer-inner">
            <section class="one-third" id="footer-third">
                <h3>Contact</h3>
                <p class="footercontact">The Gym<br>
                <b class="phone">555-525-5005</b><br><br>
                500 Washington Road<br>
                Seattle, WA 98101<br></p>
        </section>
        <section class="one-third" id="footer-third">
            <h3>Social</h3>
            <br>
                <ul class="social">
                    <li><a href="https://www.facebook.com/w3newbie" target="_blank"title=""><i class="fa fa-facebook" id="facebook"></i></a></li>
                    <li><a href="https://plus.google.com/+DrewRyan_w3/posts" target="_blank"title=""><i class="fa fa-google-plus" id="google-plus"></i></a></li>
                    <li><a href="https://twitter.com/DrewOnCue" target="_blank"title=""><i class="fa fa-twitter" id="twitter"></i></a></li>
                    <li><a href="https://youtube.com/user/DrewOnCue" target="_blank"title=""><i class="fa fa-youtube" id="youtube"></i></a></li>
                </ul>
        </section>
        <section class="one-third" id="footer-third-last">
                <h3>Pages</h3>
                <br>
                <h5><a href="index.html">home</a> - <a href="about.html">about</a> - <a href="facility.html">facility</a> - <a href="pricing.html">pricing</a> - <a href="contact.php">contact</a></h5>
        </section>
    </div>
    </footer>
    <footer class="second">
        <p>© The Gym, 2016.</p>
    </footer>
<!--- End Footer -->
<!--- Top Scroll Start -->
    <a href="#0" class="cd-top">Top</a>
        <script src="js/top.js"></script> <!-- Gem jQuery -->
        <script src="js/modernizr.js"></script>
<!--- Top Scroll End --> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

假设您的customer_id列有自动增量值。

$sql = "INSERT INTO customer(contactname, age, gender, weight, height, memebership, email) 
VALUES ('$contactname', '$age', '$gender' ,  '$weight', '$height', '$membership', '$email')";
  

没有为AUTO_INCREMENT列指定值,因此MySQL   自动分配序列号。你也可以明确地说   为列分配NULL或0以生成序列号。