使用下拉列表更新数据库中的值

时间:2018-02-16 13:22:22

标签: php html mysql database phpmyadmin

我已经建立了IT支持票务系统,并且我在下拉列表中进行选择后尝试自动更改status的值。这是它的样子:

IT Support Ticketing System

我担心的是,每当我做出选择时,都没有任何反应。

这是我的代码:



<table class="table">
					<tr>
						<th>Employee Name</th>
						<th>Time</th>
						<th>Priority</th>
						<th>Assignee</th>
						<th>Subject</th>
						<th>Problem</th>
						<th>Status</th>
					</tr>
				<?php
					include ('database.php');
					$result = $database->prepare ("SELECT * FROM tickets order by ticketno DESC");
					$result ->execute();
					for ($count=0; $row_message = $result ->fetch(); $count++){
				?>
					<tr>
						<td><?php echo $row_message['full_name']; ?></td>
						<td><?php echo $row_message['time']; ?></td>
						<td><?php echo $row_message['priority']; ?></td>
						<?php if ($row_message['assignee']) : ?>
						<td><?php echo $row_message['assignee']; ?></td>
						<?php else : ?>
						<td>
							<form method="post" action="update1.php">
								<input type="hidden" name="ticketno" value="<?php echo $row_message['ticketno']; ?>" />
								<input type="submit" name="accept" value="Accept"></input>
							</form>
						</td>
						<?php endif ; ?>
						<td><?php echo $row_message['subject']; ?></td>
						<td><?php echo $row_message['problem']; ?></td>
						<td>
							<label for=""></label> <select style="font-family: Questrial;" name="status" required>
								<option disabled selected hidden>Select Status</option>
								<option name="status" value="In Progress">In Progress</option>
								<option name="status" value="Closed: Cancelled">Closed: Cancelled</option>
								<option name="status" value="Closed: Solved">Closed: Solved</option>
							</select>
						</td>
					</tr>
					<?php	}	?>
				</table>
&#13;
&#13;
&#13;

这是脚本:

&#13;
&#13;
<script>
		$(document).ready(function()
		{
			$('option[name="status"]').click(function()
			{
					var status = $(this).val();
					$.ajax(
					{
					url:"update2.php", method:"POST",
					data:{status:status}, success: function(data)
					{
						$('#result').html(data);
					}
				});
			});
		});
	</script>
&#13;
&#13;
&#13;

这里是update2.php

&#13;
&#13;
<?php
//Insert Data
	$hostname = "localhost";
	$username = "root";
	$password = "";
	$databasename = "companydb";
	
	try
	{
		$conn = new PDO("mysql:host=$hostname;dbname=$databasename",$username, $password);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
	if(isset($_POST["status"]))
	{
		$query = "INSERT INTO tickets(status) VALUES (:status)";
		$statement = $conn->prepare($query);
		$statement->execute(
	array('status' => $_POST["status"])
	);
	
	$count = $statement->rowCount();
	if($count > 0)
	{
		echo "Data Inserted Successfully..!";
	}
		else
	{
		echo "Data Insertion Failed";
	}
	}
}

	catch(PDOException $error)
	{
		echo $error->getMessage();
	}
?>
&#13;
&#13;
&#13;

这是我的表架构:

tickets table

0 个答案:

没有答案