-2
<!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并创建了我的数据库,但它仍然是链接。

4

1 回答 1

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 注入。

于 2013-09-23T11:27:49.643 回答