<!DOCTYPE html>
<html>
<body>
<form action="hw4.php" method="POST">
Name:
<input type="text" name="name">
<br>
ID:
<input type="text" name="ID">
<br>
Password:
<input type="password" name="pass">
<br>
Gender:
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<br>
Major:
<input type="checkbox" name="major" value="Computer Science">Computer Science
<input type="checkbox" name="major" value="Electronic Engineering">Electronic Engineering
<input type="checkbox" name="major" value="Global Edison">Global Edison
<input type="checkbox" name="major" value=ICT/>ICT
<input type="checkbox" name="major" value="Economics">Economics
<input type="checkbox" name="major" value="Management">Management
<br>
Year:
<select name = "year">
<option value="Freshman">Freshman</option>
<option value="Sophomore">Sophomore</option>
<option value="Junior">Junior</option>
<option value="Senior">Senior</option>
</select>
<br>
<br>
Message
<br>
<textarea name="message" rows="10" cols="50">
</textarea>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
<?php
$db_host = "localhost" ;
$db_user = "root" ;
$db_password = "ha9056";
$db_name = "login" ;
$conn = mysqli_connect($db_host, $db_user, $db_password, $db_name) ;
if(mysqli_connect_errno($conn))
echo "Database connection failed : ".mysqli_connect_error() ;
else
echo "Database connected<br><br>" ;
$name=$_POST['name'];
$id=$_POST['id'];
$pass=$_POST['pass'];
$gender=$_POST['gender'];
$major=$_POST['major'];
$year=$_POST['year'];
$message=$_POST['message'];
$qry = "SELECT * FROM login WHERE NAME = '$name'" ;
$result = mysqli_query($conn, $qry) ;
$row = mysqli_fetch_array($result) ;
$qry = "INSERT INTO login VALUES('$name','$id','$pass','$gender','$major','$year','$message')";
mysqli_query($conn, $qry);
?>
大家好。我这里有一个注册表,想添加到 mysql 数据库中。我写下了一个 php 代码但不能正常工作,有人对我的代码有推荐吗??我将不胜感激。 性别必须在注册表中显示为男性或女性,在 mysql 中应显示为 M 或 F。并且年份必须显示为大一,大二等,并且应该在mysql中保存为1,2,3,4之类的数字 太感谢了。 下面是目前 mysql 的图像。 enter image description here
最佳答案
只需更改输入的值
<input type="radio" name="gender" value="M">Male
<input type="radio" name="gender" value="F">Female
<select name = "year">
<option value="1">Freshman</option>
<option value="2">Sophomore</option>
<option value="3">Junior</option>
<option value="4">Senior</option>
</select>
关于php - HTML注册表到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46678834/