我有一个HTML5文档,用于请求用户输入表单(保存为employee.php)。
我还创建了一个php文档(SQLConnectionProcess.php),用于将表单连接到本地数据库。我正在使用XAMPP和PHPmyAdmin。
employee.php:
<html>
<body>
<form name="EmployeeDatabase" action="SQLConnectionProcess.php" method="post">
<link rel="stylesheet" href="css.css">
<h1>EMPLOYEE DATABASE</h1>
Employe Card NO: <input type="text" name="cardNO" ><br><br>
Employee NO: <input type="text" name="employeeNO" ><br><br>
Employee Name: <input type="text" name="employeename"><br><br>
Nationality: <input type="text" name="nationality"><br><br>
Profession: <input type="text" name="profession"><br><br>
DOB: <input type="text" name="DOB"><br><br>
DOJ: <input type="text" name="DOJ"><br><br>
DOA(VisitVisa): <input type="text" name="DOA"><br><br>
Company Code: <input type="text" name="companycode"><br><br>
Sponsor Code: <input type="text" name="sponsorcode"><br><br>
Visa Type: <input type="text" name="visatype"><br><br>
Status: <input type="text" name="status"><br><br>
<input type="submit" name="formSubmit" value="Submit">
<?php
?>
</form>
</body>
</html>
SQLConnectionProcess.php:
<?php
$con = mysql_connect('localhost','root','mysql');
mysql_select_db('employee_info',$con);
if(isset($_POST['formSubmit'])){
$cardNO= isset($_POST['cardNO']) ? $_POST['cardNO'] : 0;
$employeeNO= isset($_POST['employeeNO']) ? $_POST['employeeNO'] : 0;
$employeename= isset($_POST['employeename']) ? $_POST['employeename'] : "";
$nationality= isset($_POST['nationality']) ? $_POST['nationality'] : "";
$profession= isset($_POST['profession']) ? $_POST['profession'] : "";
$DOB= isset($_POST['DOB']) ? $_POST['DOB'] : "";
$DOJ= isset($_POST['DOJ']) ? $_POST['DOJ'] : "";
$DOA= isset($_POST['DOA']) ? $_POST['DOA'] : "";
$companycode = isset($_POST['companycode']) ? $_POST['companycode'] : 0;
$sponsorcode= isset($_POST['sponsorcode']) ? $_POST['sponsorcode'] : 0;
$visatype= isset($_POST['visatype']) ? $_POST['visatype'] : "";
$status= isset($_POST['status']) ? $_POST['status'] : "";
$sql = "INSERT INTO employee_info info(EmployeeCardNO,EmployeeNO,EmployeeName,Nationality,Profession,DOB,DOJ,DOA,CompanyCode,SponsorCode,VisaType,Status) VALUES ($cardNO,$employeeNO,$employeename,$nationality,$profession,$DOB,$DOJ,$DOA,$companycode,$sponsorcode,$visatype,$status)";
mysql_query($sql);
}
?>
PHPmyAdmin密码是&#34; mysql&#34;。
当我提交表单时,我收到以下错误:
&#34;致命错误:未捕获错误:在C:\ xampp \ htdocs \ test1 \ SQLConnectionProcess.php中调用未定义函数mysql_connect():3堆栈跟踪:#0 {main}抛出C:\ xampp \第3行&#34;
上的htdocs \ test1 \ SQLConnectionProcess.php请帮助我。谢谢....
答案 0 :(得分:2)
请使用PDO或MySQLi。 mysql已弃用,不应在新代码上使用
http://php.net/manual/en/function.mysql-query.php
试试这个链接,它给了我很多帮助:@ Your-Common-Sense的phpdelusions.net / pdo。
我会这样编码:
1:db连接文件:
<?php
$db = new PDO('mysql:host=yourhost;dbname=dbname', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
您的SQLConnectionProcess.php应如下所示:
<?php
require ('path/to/db/con.php');
if(isset($_POST['formSubmit'])){
$cardNO = isset($_POST['cardNO']) ? $_POST['cardNO'] : 0;
$employeeNO = isset($_POST['employeeNO']) ? $_POST['employeeNO'] : 0;
$employeename = isset($_POST['employeename']) ? $_POST['employeename'] : "";
$nationality = isset($_POST['nationality']) ? $_POST['nationality'] : "";
$profession = isset($_POST['profession']) ? $_POST['profession'] : "";
$DOB = isset($_POST['DOB']) ? $_POST['DOB'] : "";
$DOJ = isset($_POST['DOJ']) ? $_POST['DOJ'] : "";
$DOA = isset($_POST['DOA']) ? $_POST['DOA'] : "";
$companycode = isset($_POST['companycode']) ? $_POST['companycode'] : 0;
$sponsorcode = isset($_POST['sponsorcode']) ? $_POST['sponsorcode'] : 0;
$visatype = isset($_POST['visatype']) ? $_POST['visatype'] : "";
$status = isset($_POST['status']) ? $_POST['status'] : "";
$stmt = $db->prepare("INSERT INTO employee_info
(EmployeeCardNO,
EmployeeNO,
EmployeeName,
Nationality,
Profession,
DOB,
DOJ,
DOA,
CompanyCode,
SponsorCode,
VisaType,
Status)
VALUES
($cardNO,
$employeeNO,
$employeename,
$nationality,
$profession,
$DOB,
$DOJ,
$DOA,
$companycode,
$sponsorcode,
$visatype,
$status)"
);
$stmt->execute();
}
else{
//something went wrong
}
?>
您有很多isset()
条件。如果一个失败,整个代码都会失败。
以此为出发点。
感谢。