php - 如何用PHP连接MySQL(WAMP)数据库,并处理错误?

标签 php mysql html wamp

最近我接到了一个创建数据查询系统的任务。系统要求用户填写表格。提交后,数据将由管理员验证并连接到数据库。

这是我的form.html:

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>data</title>
    <link rel="stylesheet" media="screen" href="styles.css" >
</head>
<body bgcolor="#13b4ff">

<div id="header" style="background-color: #4169E1;"><hr>

<form class="form" action="submit.php" method="post" name="form" >

<ul>
    <li>
         <span class="required_notification">* Denotes Required Field</span>
    </li>
    <li>
        <label for="name">Name:</label>
        <input type="text" name="name" required />
    </li>
    <li>
        <label for="position:">Jawatan:</label>
        <input type="text" name="position" />
    </li>
    <li>
        <label for="unit">Unit:</label>
        <input type="text" name="unit" required />
    </li>
    <li>
        <label for="institute">Institute:</label>
        <input type="text" name="institute" required />
    </li>
    <li>
        <label for="telefon">No. Telefon:</label>
        <input type="number" name="telefon" placeholder="eg: 012-345-6789" required />
    </li>
    <li>
        <label for="faks">No. Faks:</label>
        <input type="number" name="faks" placeholder="eg: 03-12345678" />
    </li>
    <li>
        <label for="email">E-mail:</label>
        <input type="email" name="name" placeholder="name@something.com"/ required>
    <span class="form_hint">proper format<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script></span>
</li>
    <li>
        <label for="data">Data Required:</label>
        <input type="text" name="data" required/>
    </li>
    <li>
        <label for="purpose">Purpose:</label>
        <input type="text" name="purpose" required/>
    </li>
    <li>
    <button class="submit" type="submit">Submit</button>
    </li> 
</ul>
</form>
</body>
</html>

这是表单的 styles.css:

.form { 
background-color:#ffffff;
margin: 0 auto; 
width:750px;
height: 825px;
}

body {font: 14px/21px "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;}
.form h2, .form label {font-family:Georgia, Times, "Times New Roman", serif;}
.form_hint, .required_notification {font-size: 11px;}

.form ul {
    width:750px;
    list-style-type:none;
    list-style-position:outside;
    margin:0px;
    padding:0px;
}
.form li{
    padding:13px;
    border-bottom:1px solid #eee;
    position:relative;
}

.form li:first-child, .form li:last-child {
    border-bottom:2px solid #777;
}

/*header*/
.form h2 {
    margin:0;
    display: inline;
}
.required_notification {
    color:#d45252;
    margin:5px 0 0 0;
    display:inline;
    float:right;
}

.form label {
    width:200px;
    margin-top: 3px;
    display:inline-block;
    float:left;
    padding:3px;
}
.form input {
    height:20px;
    width:220px;
    padding:5px 8px;
}
.form textarea {padding:8px; width:300px;}
.form button {margin-left:156px;}

.form input, .form textarea {
    border:1px solid #aaa;
    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
    border-radius:2px;
    -moz-transition: padding .25s;
    -webkit-transition: padding .25s;
    -o-transition: padding .25s;
    transition: padding .25s;
    padding-right:30px;
}
.form input:focus, .form textarea:focus {
    padding-right:70px;
    background: #fff;
    border:1px solid #555;
    box-shadow: 0 0 3px #aaa;
}

button.submit { 
margin: 250 auto; 
width:100px;
}

button.submit {
    background-color: #2F68B1;
    background: -webkit-gradient(linear, left top, left bottom, from(#2F68B1), to(#10468B));
    background: -webkit-linear-gradient(top, #2F68B1, #10468B);
    background: -moz-linear-gradient(top, #2F68B1, #10468B);
    background: -ms-linear-gradient(top, #2F68B1, #10468B);
    background: -o-linear-gradient(top, #2F68B1, #10468B);
    background: linear-gradient(top, #2F68B1, #10468B);
    border: 1px solid #2C6BB8;
    border-bottom: 1px solid #5b992b;
    border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    -ms-border-radius: 3px;
    -o-border-radius: 3px;
    box-shadow: inset 0 1px 0 0 #9fd574;
    -webkit-box-shadow: 0 1px 0 0 #9fd574 inset ;
    -moz-box-shadow: 0 1px 0 0 #9fd574 inset;
    -ms-box-shadow: 0 1px 0 0 #9fd574 inset;
    -o-box-shadow: 0 1px 0 0 #9fd574 inset;
    color: white;
    font-weight: bold;
    padding: 7px 20px;
    text-align: center;
    text-shadow: 0 -1px 0 #396715;
}
button.submit:hover {
    opacity:.85;
    cursor: pointer;
}
button.submit:active {
    border: 1px solid #20911e;
    box-shadow: 0 0 10px 5px #356b0b inset;
    -webkit-box-shadow:0 0 10px 5px #356b0b inset ;
    -moz-box-shadow: 0 0 10px 5px #356b0b inset;
    -ms-box-shadow: 0 0 10px 5px #356b0b inset;
    -o-box-shadow: 0 0 10px 5px #356b0b inset;
}

input:required, textarea:required {
    background: #fff url(asterisk.jpg) no-repeat 98% center;
}

/*invalid, valid*/
.form input:focus:invalid, .form textarea:focus:invalid { /* when a field is considered invalid by the browser */
    background: #fff url(invalid.jpg) no-repeat 98% center;
    box-shadow: 0 0 5px #d45252;
    border-color: #b03535
}

.form input:required:valid, .form textarea:required:valid { /* when a field is considered valid by the browser */
    background: #fff url(valid.jpg) no-repeat 98% center;
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
}

.form_hint {
    background: #d45252;
    border-radius: 3px 3px 3px 3px;
    color: white;
    margin-left:8px;
    padding: 1px 6px;


}

表单将被传输到submit.php(我使用mysql做数据库)。我使用wampserver。

 <?php

    //debug mode
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');

    //to show some error is smthng went wrong
    $errors = array();

    function connect(){
        $connection = mysql_connect(localhost, root, "" ); //I have no idea what the password is. where could I set one?
        $db = mysql_select_db(permohonan_data,$connection);

    if (!$connection || !$db){
        return false;
        }
    else{
        return true;
        }
    }

echo "first stage";

    //will run if user did submit the form
    if (!empty($_POST)){

echo "second stage";

    //connect sql server:
    if (!connect()){
        $errors[] = "Can't establish link to MySql server";
        }

        $name = $_POST['name'];
        $position = $_POST['position'];
        $unit = $_POST['unit'];
        $institute = $_POST['institute'];
        $telefon = $_POST['telefon'];
        $faks = $_POST['faks'];
        $email = $_POST['email'];
        $data = $_POST['data'];
        $purpose = $_POST['purpose'];

echo "third stage";

    //no error til here
    if (empty($error)){

    //prevent SQL injection
    $name = mysql_real_escape_string($name);    
    $position = mysql_real_escape_string($position);
    $unit = mysql_real_escape_string($unit);
    $institute = mysql_real_escape_string($institute);
    $telefon = mysql_real_escape_string($telefon);
    $faks = mysql_real_escape_string($faks);
    $email = mysql_real_escape_string($email);
    $data = mysql_real_escape_string($data);
    $purpose = mysql_real_escape_string($purpose);

    }

echo "fourth stage";

    //try insert value
    $query = "INSERT INTO 'user'
        (name,position,unit,institute,telefon,faks,email,data,purpose)
        VALUES ('$name', '$position', '$unit', '$institute', '$telefon', '$faks', '$email', '$data', '$purpose')";

    if (!mysql_query($query)){
        //
        //die(mysql_error());
        $errors[] = "Can't insert the values";
        }
    else {
        //on success
        header('location:C:\wamp\www\FORM\thankyou.php');

    }

    }   

    ?>

结果没有给出错误,我已经输入了报告错误语法,但屏幕上也没有显示任何内容。不过,该脚本在页面源中仍然可见。当我放置 echo 部分时,脚本只会运行到“第一阶段”。它没有重定向到thankyou.php。我做错了什么?我怎样才能将其连接到数据库?我做了 $connect 部分,但它对我来说仍然很模糊。请帮忙。

这是thankyou.php:

<html>
<head>
    <meta charset="utf-8">
    <title>thank you!</title>
    <link rel="stylesheet" media="screen" href="stylesphp.css" >
</head>
<body bgcolor="#13b4ff">
        <div class="boxed";>
        <div id="thankyou" style= "text-align:center; font-size:50px;">THANK YOU</div><br>
        &nbsp;Your inquiry has been submitted. Please wait 24 hours. <br>
        &nbsp;You will be notify by email.
        <p>&nbsp;return to <a class="a" href="http://www.moe.gov.my/en/home" target="_self">main page.</a>
        </div>
</body>
</html>

编辑,感谢 Fred -ii- 和 Joni Salmi,我已经修复了错误。

但它显示的是这个:

Notice: Undefined index: email in C:\wamp\www\FORM\submit.php on line 39.

这是第 39 行

$email = $_POST['email'];

最佳答案

根据您的original question

<小时/>

$_POST"superglobal"并且必须为大写。

你有:

if (!empty($_post)){

将其更改为:

if (!empty($_POST)){

这似乎是问题最有可能的部分。

据我所知,除了您使用已弃用的 mysql_ 函数这一事实之外,您的其余代码对我来说似乎没问题。

编辑

正如 Joni Salmi 指出的 in a comment ,

您正在使用msql_connect,它应该是mysql_connect

归功于 Joni (Salmi)

更改:

$connection = msql_connect(localhost, root, "" );
missing "y" => ^

至:

$connection = mysql_connect(localhost, root, "" );

另外,根据您对 Undefined index: email 错误消息的评论。

更改此行:

<input type="email" name="name" placeholder="name@something.com"/ required>

至:

<input type="email" name="email" placeholder="name@something.com"/ required>

你有:

<label for="name">Name:</label>
<input type="text" name="name" required />

使用与您的电子邮件输入相同的命名输入字段name="name"

编辑#2

在表名中使用反引号而不是引号。

你有:

$query = "INSERT INTO 'user'

使用(反引号):

$query = "INSERT INTO `user`

您甚至可以删除引号:(但反引号更好)

$query = "INSERT INTO user

关于php - 如何用PHP连接MySQL(WAMP)数据库,并处理错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20602472/

相关文章:

php - 从 mysqli 结果中读取值

php - 如何组织数据库

html - CSS 保持第一个 child 的填充 : 0px

html - CSS3 动画和过渡一起

javascript - 如何检索使用 setCustomValidity 设置的错误消息?

php 时间没有给出正确的日期

php - 不使用表单中的空字段更新数据库

php - 如何在 Slim 3.1 中触发 Eloquent Manager 依赖注入(inject)

mysql - 跨连接表求和

php - 如何修改表格内容的字体大小