我正在尝试根据HTML文件中的无线电选择创建两个不同的表。这是我到目前为止创建的HTML表单:
<form action="connect.php" method=POST>
First Name: <input type="text" name="firstname"><br><br>
Last Name: <input type="text" name="lastname"><br><br>
Email: <input type="text" name="email"><br><br>
Desired Password: <input type="password" name="password"><br><br>
Re-type Password: <input type="password" name="password"><br><br>
I am a: <input type="radio" name="role" value="student">Student
<input type="radio" name="role" value="alumni">Alumni<br><br>
<div class="stud">
Major: <input type="text" name="studmajor"><br><br>
Emphasis: <input type="text" name="studemphasis"><br><br>
Expected Graduation Year: <input type="text" name="studgradyear"><br><br>
Hobbies: <input type="text" name="studhobbies">
</div>
<div class="alum">
Gradutation Year: <input type="text" name="alumgradyear"><br><br>
Major: <input type="text" name="alummajor"><br><br>
Emphasis: <input type="text" name="alumemphasis"><br><br>
Company: <input type="text" name="alumcompany"><br><br>
Hobbies: <input type="text" name="alumhobbies">
</div>
<br>
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
$('input:radio[name=role]').change(function(){
var role = $(this).val();
if(role=='student'){
$('.stud').show();
$('.alum').hide();
}
else if(role=='alumni'){
$('.alum').show();
$('.stud').hide();
}
});
所以在这个HTML代码中,我正在尝试创建一个数据库。一个是校友,一个是学生。根据选中的单选框,显示不同的选项。以下是我编写(并借用)的php代码,用于INSERT INTO我创建的两个不同的表。我正在使用c9.io,因为这只是为了让我的想法让网站启动并运行。
<?php
$servername = getenv('IP');
$username = getenv('C9_USER');
$password = "";
$database = "c9";
$dbport = 3306;
$db = mysqli_connect($servername, $username, $password, $database, $dbport);
if($db === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$first_name = mysqli_real_escape_string($db, $_POST['firstname']);
$last_name = mysqli_real_escape_string($db, $_POST['lastname']);
$email_address = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$studmajor = mysqli_real_escape_string($db, $_POST['studmajor']);
$studemphasis = mysqli_real_escape_string($db, $_POST['studemphasis']);
$studgradyear = mysqli_real_escape_string($db, $_POST['studgradyear']);
$studhobbies = mysqli_real_escape_string($db, $_POST['studhobbies']);
$alumgradyear = mysqli_real_escape_string($db, $_POST['alumgradyear']);
$alummajor = mysqli_real_escape_string($db, $_POST['alummajor']);
$alumemphasis = mysqli_real_escape_string($db, $_POST['alumemphasis']);
$alumcompany = mysqli_real_escape_string($db, $_POST['alumcompany']);
$alumhobbies = mysqli_real_escape_string($db, $_POST['alumhobbies']);
$role = mysqli_real_escape_string($db, $_POST['role']);
// Post to Student
if($role == "student"){
$sql = "INSERT INTO Student (FirstName, LastName, Email, Password, Major, Emphasis, GradYear, Hobbies)
VALUES ('$first_name', '$last_name', '$email_address', '$password', '$studmajor', '$studemphasis', '$studgradyear', '$studhobbies');";
}
// Post to Alumni
else if($role == "alumni"){
$sql = "INSERT INTO Alumni (FirstName, LastName, Email, Password, GradYear, Major, Emphasis, Company, Hobbies)
VALUES ('$firstname', '$lastname', '$email', '$password', '$alumgradyear', '$alummajor', '$alumemphasis', '$alumcompany', '$alumhobbies');";
}
mysqli_close($db);
?>
非常感谢任何帮助。我对SQL和PHP很陌生,所以这有点令人困惑。
很抱歉没有特别说明。每当我填写表单并单击“提交”时,我都会被重定向并显示:
Cannot POST /username/project/connect.php
我的猜测是php文件中的if / elseif语句是导致问题的原因,但我无法准确地说出来。
答案 0 :(得分:1)
您应该为表单命名,从connect.php中的HTML中提取数据,然后使用php插入。
这是一个快速演示,向您展示它是如何完成的!
PHP MYSQL Database Manipulation
希望这有帮助!