Php 我无法将数据添加到mysql数据库中的表中

Php 我无法将数据添加到mysql数据库中的表中,php,mysql,forms,Php,Mysql,Forms,请参见下面的php代码: 我使用构建了html表单和html表单中的下拉菜单。在我看来,您使用这个draft5.php页面来显示表单并在数据库中插入一行。在这种情况下,问题可能来自$\u POST数据周围缺少的isset()。(首次加载页面时,未设置POST值)。 您可以使用以下代码: 你也可以考虑在你的表和COLMNS的名称周围使用回扣。 如果要防止同一学生在同一课程中注册两次,则需要在表中添加主键。但这还不够,事实上,如果在主键上执行insert请求时违反了contraint,MySql将返

请参见下面的php代码:


我使用构建了html表单和html表单中的下拉菜单。在我看来,您使用这个draft5.php页面来显示表单并在数据库中插入一行。在这种情况下,问题可能来自$\u POST数据周围缺少的isset()。(首次加载页面时,未设置POST值)。 您可以使用以下代码:

你也可以考虑在你的表和COLMNS的名称周围使用回扣。


如果要防止同一学生在同一课程中注册两次,则需要在表中添加主键。但这还不够,事实上,如果在主键上执行insert请求时违反了contraint,MySql将返回一个错误。您可以做的是检查在插入请求之前是否存在密钥,如果是,则通知用户,如果否,则执行插入请求

究竟是什么问题?是否有相关的错误消息?您是否尝试添加一些
echo
s来跟踪代码运行的距离?我建议将
echo
ing
mysql\u error()
与您的
fail
一起使用,这样您就可以明确知道出了什么问题。当然。。。在将用户生成的内容放入查询之前,您应该使用
mysql\u real\u escape\u string()
或强制转换为
整数(用于数字信息)。它是说“失败”还是更快出错?此外,由于您将提交回与表单相同的页面,因此您应该有一些逻辑,只有在页面发布到的情况下才尝试进行插入。如果表单操作在同一页面上处理,您可以将表单操作保留为空“”。名称为'backticks',确定,因此这允许我在mysql表中输入学生编号名称和课程!好的,这样我就可以在mysql表中输入学生编号名称和课程!现在我想知道我如何确保相同的学生ID和不被输入相同的课程……好的,这是我的代码修复多一点。如果我不想输入两次相同的学生ID和课程,我认为我必须向数据库中的表添加主键?
//connect3.php--login information to my mysql database//

<?php
 define ('HOST', 'localhost');
 define ('USER', 'root');
 define ('PASS', '******');
?>


// html form and connection code//
    <?php
     include 'connect3.php';
     $link = mysql_connect (HOST, USER, PASS) or die(mysql_error());
     mysql_select_db ('milleruniversity', $link);

// Added mysql_real_escape() to protect against SQL-Injection
$code        = mysql_real_escape( $_POST['code'] ); 
$uid         = mysql_real_escape( $_POST['uid'] );
$studentname = mysql_real_escape( $_POST['studentname'] );

// Insert a row of information into the table "enrolment"

$query = "INSERT INTO enrolment (code, uid, studentname) VALUES('$code', '$uid', '$studentname')";
if(mysql_query($query)){
echo "inserted";}
else{
echo "fail";}
echo <<<_END
<table border='1'cellpadding="10">
<tr>
<td>
<h4>Miller University Registration Form</h4>
<p>Please Register as a new Student or current student for the following courses below.</p>
</td>
</tr>
<form action="draft5.php" method="post"><pre>
<tr>
<td>
  Student Name <input type="text" name="studentname" maxlength="30"/>
</td>
</tr>
<tr>
<td>
   Student ID <input type="text" name="uid" maxlength="11"/>
</tr>
</td>
<tr>
<td>
Select a course <select name="code" size="1">
<option value="DC-00040">Digital Communications</option>
<option value="VC-00030">Visual Culture</option>
<option value="WP-00080">World Politics</option>
</select>
</tr>
</td>
<tr>
<td>
        <input type="submit" value="Submit to Register" />
</tr>
</td>
</pre></form>
</table>
_END;

mysql_close($link);

?>
if( (isset($_POST['code']) AND (isset($_POST['uid']) AND (isset($_POST['studentname']){
//process the data base treatment and display an acknoledgment of the insert
// Check if these new code, uid and studentname respect the primary key constraint


}

else{
// Display your form
}