<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel = "stylesheet" href = "css/mystyle.css" type = "text/css/">
<title> APPOINTMENT FORMS </title>
</head>
<body>
<form method="post" action="<?php $_PHP_SELF ?>">
<p> <?php if(isset($_REQUEST['message'])) echo $_REQUEST['message'] ?> </p>
<table width = '50%' border = '0' cell spacing = '0' cell padding = '2'>
<tr>
<td align = 'right' style='padding:10px'>EM_ID: </td>
<td> <input type = 'text' name = 'em_id' placeholder = 'Enter your ID here' size = '50'></td>
</tr>
<tr>
<td align = 'right' style='padding:10px'> Appointment Type: </td>
<td><input type = 'text' name = 'appointment_type' placeholder = 'Enter either part time/full time here' size = '50'></td>
</tr>
<tr>
<td align = 'right' style='padding:10px'> Appointment Category: </td>
<td><input type = 'text' name = 'appointment_category' placeholder = 'Enter either Academic/Non-Academic here' size = '50'></td>
</tr>
<tr>
<td align = 'right' style='padding:10px'> Date of Appointment: </td>
<td><input type = 'text' name = 'date_of_appointment' placeholder = 'Enter yy/mm/dd here' size = '50'></td>
</tr>
<tr>
<td align = 'right' style='padding:10px'> Date of Confirmation: </td>
<td><input type = 'text' name = 'date_of_confirmation' placeholder = 'Enter date of confirmation of appointment here' size = '50'></td>
</tr>
<tr>
<td align = 'right' style = 'padding:20px'><input type = 'submit' value ='Submit'></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit'])) {
$connect = mysql_connect('localhost','root','oluwaseun') or die ("Could not connect to the database");
mysql_select_db('school_of_science', $connect) or die ("Could not find the database");
$em_id = $_POST['em_id'];
$appointment_type = $_POST['appointment_type'];
$appointment_category= $_POST['appointment_category'];
$date_of_appointment = $_POST['date_of_appointment'];
$date_of_confirmation = $_POST['date_of_confirmation'];
mysql_query("INSERT into appointment (em_id,appointment_type,appointment_category,date_of_appointment,date_of_confirmation)
VALUES('$em_id','$appointment_type','$appointment_category','$date_of_appointment','$date_of_confirmation')") or die(mysql_error());
$message = "Your data has been entered successfully";
header("location:practice.php? message=$message");
}
?>
</body>
</html>
我是php的新手,我试图在我的html表单和数据库之间创建一个链接,但它不起作用。请帮助我。谢谢 我在同一页面上有html标签和php代码,我已经正确安装了mysql并创建了我的数据库,但它仍然是链接。
答案 0 :(得分:0)
好吧,就像评论中提到的那些人一样;什么是不起作用? 我还建议您使用mySQLi而不是mySQL。
关于如何制作插入表格的表单的示例;
<form action='index.php?a=register' method='post'> <!-- You can use if(isset($_POST['submit'])) as well, but I prefer ?a(ction)= :) -->
Username: <input type='text' name='username /> Password: <input type='password' name='password' />
<input type='submit' value='Register' />
</form>
<?php
$db = mysqli_connect("server_name", "user", "password", "database") or die (mysqli_error());
if($_GET['a']=='register')
{
$username = $_REQUEST['username']; // Can also use $_POST
$password = md5($_REQUEST['password']); // Same here ^.
mysqli_query($db, "INSERT INTO users(username, password) VALUES('$username', '$password')");
}
?>
如果您仍想使用mysql,只需将“mysqli”更改为“mysql”,然后编辑连接以及查询。 我会用你的代码来帮助你,但是你没有真正指出什么是错的我会简单地给你这个例子。 :) 正如其他人也说的那样;你应该熟悉如何防止SQL注入。