1

I am trying to create two different tables based upon a radio selection in the HTML file. This is the HTML form I have created so far:

<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();
}
});

So in this HTML code, I am trying to create a database. One for Alumni and one for Students. Depending on the radio box checked, different options are shown. The following is my php code I wrote (and borrowed) to INSERT INTO the two different tables I have created. I am using c9.io as this is just a mock up to get my idea for the site up and running.

<?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);
?>

Any help would be greatly appreciated. I'm pretty new to SQL and PHP so this is a little confusing.

Sorry for not being very specific. Whenever I fill out the form and click submit, I'm redirected and it displays:

Cannot POST /username/project/connect.php

My guess is that the if/elseif statement in the php file is what's causing the issue but I can't accurately say for sure.

4
  • But what it exacly the problem? be more specific. Commented Feb 23, 2016 at 7:24
  • is thisa a demo or a question? Commented Feb 23, 2016 at 7:26
  • are you getting any error there?? Commented Feb 23, 2016 at 7:27
  • Beware of sql injection. stackoverflow.com/questions/16282103/… Commented Feb 23, 2016 at 7:28

1 Answer 1

1

You should name your form, pull data from HTML in connect.php and then use php to insert.

Here is a quick demo to show you how it is done!

PHP MYSQL Database Manipulation

Hope this Helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I updated my original post with what I have now based upon your link however it still doesn't work. I'm assuming it has to do with my if/elseif statement being wrong maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.