如何将MySQL(WAMP)数据库与PHP连接,以及如何处理错误?
最近我给了一个任务,创建一个数据查询系统。系统要求用户填写表格。提交时,管理员将验证数据并将其连接到数据库 这是my form.html:如何将MySQL(WAMP)数据库与PHP连接,以及如何处理错误?,php,mysql,html,wamp,Php,Mysql,Html,Wamp,最近我给了一个任务,创建一个数据查询系统。系统要求用户填写表格。提交时,管理员将验证数据并将其连接到数据库 这是my form.html: <!DOCTYPE html> <head> <meta charset="utf-8"> <title>data</title> <link rel="stylesheet" media="screen" href="styles.css" > </hea
<!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>
表单将被传输到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');
}
}
?>
这是第39行
$email = $_POST['email'];
按照你的要求
$\u POST
是大写字母,且必须为大写字母
你有:
if (!empty($_post)){
$query = "INSERT INTO 'user'
将其更改为:
if (!empty($_POST)){
这似乎是问题最可能的部分
据我所知,除了您使用的是一个不推荐使用的mysql\uu
函数之外,您的其余代码对我来说似乎还行
编辑
正如乔尼·萨尔米所指出的
您使用的是msql\u connect
,应该是mysql\u connect
归功于
更改:
$connection = msql_connect(localhost, root, "" );
missing "y" => ^
致:
另请参见您关于未定义索引:电子邮件错误消息的评论
更改此行:
<input type="email" name="name" placeholder="name@something.com"/ required>
使用(反勾号):
您甚至可以删除引号:(但倒勾更好)
回复:未定义索引:
您错误地命名了用于在HTML中保存电子邮件地址的输入
<input type="email" name="name" placeholder="name@something.com"/ required>
换成
<input type="email" name="email" placeholder="name@something.com"/ required>
注意name=“email”
您在HTML中为字段指定的名称控制$\u POST数组中使用的字段名。首先,$\u POST
是一个字符,必须为大写。您有if(!empty($\u post)){
将其更改为if(!empty($\u post)){
这很可能是“a”或者是实际的问题。我不能测试你的全部代码,但一定要从那开始。据我所知,你的其他代码对我来说似乎没问题,除了你使用的是一个不推荐的mysql\uuuu
function.msql\u connect to mysql\u connection我还没有注意到@JoniSalmi很好。我注意到你用发布的c编辑了你的问题注意/回答(刚才)。现在有什么问题?@Fred ii-工作很好!非常感谢你帮助我:)
<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 />
$query = "INSERT INTO 'user'
$query = "INSERT INTO `user`
$query = "INSERT INTO user
<input type="email" name="name" placeholder="name@something.com"/ required>
<input type="email" name="email" placeholder="name@something.com"/ required>