2017-10-11 51 views
2
<!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.年份必须显示为新生,大二等,并应保存为一个数字,如1,2,3,4在mysql 非常感谢。 下面是现在的mysql的图像。 enter image description here到mysql的HTML注册表格

+0

您可以将您的性别输入的值更改为'M'和'F'代替,而不是'male'和'female'。与 – Swellar

+0

相同哦,它必须在注册表格中显示为男性和女性,并且应该在mysql中显示为F或M – David

+0

我的意思是输入的值属性,它仍然呈现为男性或女性,但你会得到的价值是'M'或'F',这几年一样 – Swellar

回答

-1

只要改变值的输入

<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> 
+0

@Downvoter请解释一下,以便我可以相应地调整 – Swellar