gpt4 book ai didi

php - 无法将数据插入mysql

转载 作者:行者123 更新时间:2023-11-29 21:25:06 24 4
gpt4 key购买 nike

我已经尝试过这段代码,我希望用户填写表格来提高票证,并且单击提交时数据必须插入到数据库中。我已经为其编写了查询,但无法将表单数据放入其中。

Form.php

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Raise Ticket</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){

$("form#form-submission").submit(function(){ /* WHEN THE FORM IS SUBMITTED */

var formData = {
'ename' : $('input[name=ename]').val(),
'date' : $('input[name=date]').val(),
'ext' : $('input[name=ext]').val(),
'issue' : $('select[name=issue]').val(),
'query' : $('input[name=query]').val()

};

$.ajax({ /* CALL AJAX */
url: "insertticket.php", /* THE FILE WHERE THE DATA WILL GO */
type: "POST", /* TYPE OF METHOD TO BE USED TO PROCESS THE DATA */
data: formData, /* THE DATA TO BE PROCESSED */
contentType: false, /* TYPE OF DATA TO BE PASSED; FOR FILE PURPOSES THAT IS WHY IT IS SET TO FALSE */
processData: false, /* PREVENT AUTOMATIC PROCESSING */
success: function(result){ /* IF DATA IS PROCESSED SUCCESSFULLY */
alert('Ticket Raised Successfully ' ); /* ALERT THE RETURNED DATA FROM inserticket.php */
}
});

return false;

});

});
</script>
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading"><h1 align="center">Helpdesk</h1></div>
<hr>
<ul class="nav nav-pills nav-justified">
<li><a href="HomePage.php">Home</a></li>
<li><a href="NetworkIssue.php">Network Issue</a></li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Software Issue <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="SoftwareIssue.php">Software</a></li>
<li><a href="#">ERP</a></li>
<li><a href="#">CRM</a></li>
</ul></li>
<li><a href="HardwareIssue.php">Hardware Issue</a></li>
<li><a href="Ticket.php">Raise Ticket</a></li>
<li><a href="Logout.php">Logout</a></li>
</ul>
<hr>
<div class="panel-body" align="center">
<form class="form-horizontal" role="form" enctype="multipart/form-data" method="post" id="form-submission">
<div class="form-group">
<label class="control-label col-sm-2" for="text">Name:</label>
<div class="col-sm-10">
<input type="text" disabled class="form-control" id="text" name="ename" value="<?php echo ucwords($_SESSION['usr_name']); ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date">Date:</label>
<div class="col-sm-10">
<input type="text" class="form-control" disabled id="date" name="date" value="<?php date_default_timezone_set("Asia/Calcutta"); echo date('Y-m-d H:i:s')?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="ext">Extension No.:</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="ext" name="ext" placeholder="Enter Extension No.">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="issue">Issue Type:</label>
<div class="col-sm-10">
<select required="true" class="form-control" id="validate-issue" name="issue">
<option>Select an option</option>
<option>Hardware Issue</option>
<option>Network Issue</option>
<option>Software Issue</option>
<option>ERP</option>
<option>CRM</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="query">Query:</label>
<div class="col-sm-10">
<textarea required="true" rows="5" class="form-control" id="validate-query" name="query" placeholder=" Enter Query here"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
</div>
</form>
</div>
<hr>
<div class="panel-footer" align="center">&copy Copyright <?php echo date("Y");?></div>
</div>
</div>
</body>
</html>

insertticket.php

<?php
session_start();
$host = "localhost"; // Host name
$username = "root"; // Mysql username
$password = ""; // Mysql password
$db_name = "help"; // Database name

// Connect to server and select database.
$conn = mysqli_connect($host,$username,$password) or die("cannot connect");

mysqli_select_db($conn,$db_name);

$sname = $_SESSION['usr_name'];

$sqli = "select * from user where name = '$sname'";
$result = mysqli_query($conn,$sqli);
while($row = mysqli_fetch_array($result))
{
$emp_code = $row['emp_code'];
$dept = $row['department'];
$location = $row['location'];
$emailid = $row['emailid'];
$contact_no = $row['contact_no'];
}

$emp_name = $_SESSION['usr_name'];
$date = mysqli_real_escape_string($conn, $_POST['date']);
$issue_type = mysqli_real_escape_string($conn, $_POST['issue']);
$query = mysqli_real_escape_string($conn, $_POST['query']);
$ext_no = mysqli_real_escape_string($conn, $_POST['ext']);
//$attachment = mysqli_real_escape_string($conn, $_FLIES['upload']['name']);

$sql = "INSERT INTO ticket (emp_code,emp_name,dept,location,emailid,contact_no,date,issue_type,query,ext_no)
VALUES('$emp_code','$emp_name','$dept','$location','$emailid','$contact_no','$date','$issue_type','$query','$ext_no')";
if (mysqli_query($conn,$sql))
{
echo "Record added";
}
else
{
die('Error: ' . mysqli_error());
}

?>

最佳答案

首先您需要使用 mysqli 或 pdo。 mysql 已弃用。不再更新。

在 net 选项卡下检查 firebug。您的数据是否正在提交。如果没有,那么您可以专注于 form.php

尝试将以下代码放入测试文件中并在浏览器中运行该文件。

$host="localhost"; // Host name 
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="help"; // Database name
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");

mysqli_select_db($conn,$db_name);

$emp_code ='1';
$emp_name = 'test data';
$dept = 'test data';
$location ='test data';
$emailid = 'test data';
$contact_no = 'test data';
$date = '01/10/2016';
$isuse_type ='test data';
$query = 'test data';
$ext_no ='test data';

$sql="INSERT INTO ticket (emp_code,emp_name,dept,location,emailid,contact_no,date,issue_type,query,ext_no)
VALUES('$emp_code','$emp_name','$dept','$location','$emailid','$contact_no','$date','$issue_type','$query','$ext_no')";
if (mysqli_query($conn,$sql))
{
echo "Record added";

}
else
{
die('Error: ' . mysqli_error());
}

让我们知道它是否在sql中添加数据。这将帮助我们检查 insertticket.php,然后我们就可以专注于 ajax。

关于php - 无法将数据插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35550223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com