gpt4 book ai didi

php - 从变量 HTML 表单插入 sql 表

转载 作者:行者123 更新时间:2023-11-28 23:36:41 25 4
gpt4 key购买 nike

我正在尝试根据 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 代码,用于插入我创建的两个不同的表。我正在使用 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 语句是导致问题的原因,但我不能准确地说。

最佳答案

您应该命名您的表单,从 connect.php 中的 HTML 中提取数据,然后使用 php 插入。

这是一个快速演示,向您展示它是如何完成的!

PHP MYSQL Database Manipulation

希望对您有所帮助!

关于php - 从变量 HTML 表单插入 sql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35571188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com