0

I have tried looking at similar questions, however, I am tripping over my own code trying to understand what is going wrong.

What I have setup:

XAMPP GymPHP (Write code) Database on localhost called gym Table in database called records with 9 columns

I have created a form and am now trying to establish a connection between the form and the database in order to move the data from the form into the database.

Code I have:

Form.php:

<html>

<head>

    <title>Gym Form</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


</head>

<body>

    <form action="Login.php" method="post">

    <span>Gym Membership Registration</span><br><br>

    <Span>Title: </Span><input type ="text" Value =" " name ="Title" /><br>

    <Span>First Name: </Span><input type ="text" Value =" " name ="Fname" /><br>

    <Span>Last Name: </Span><input type ="text" Value =" " name ="Lname" /><br><br>

    <Span>Gender: </Span><select name ="Gender">

        <option value ="Junior">Male</option>
        <option value ="Adult">Female</option>
        <option value ="Senior">Private</option>

    </select><br>

    <Span>DOB: </Span><input type ="date" name ="DOB" /><br><br>

    <Span>MembershipExpiry: </Span> <input type ="date" name ="MemX" /><br>

    <Span>MembershipType: </Span><select name = "MemType">

        <option value ="Junior">Junior</option>
        <option value ="Adult">Adult</option>
        <option value ="Senior">Senior</option>

    </select><br><br>

    <Span>Email Address: </Span><input type ="email" name ="Email" /><br><br>

    <input type="Submit" name="submit" value ="Submit Form">




    </form>

</body>

Connect.php:

    <?php

$conn = mysql_connect("localhost", "root", "");

mysql_select_db("gym");

if(!$conn)
    echo"Error Connecting to Database!";
else
    echo"Connected to Database!";

Login.php:

include "Connect.php";

$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];

$sql = mysql_query("INSERT INTO records (Title, Fname, Lname, Gender, DOB,                   MemX, MemType, Email) values ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')", $conn);

I complete the form and get a message to say the connection has been made, however, when I check the database..there are not records added? Please help , thanks.

JSint
  • 101
  • 1
  • 11
  • $sql = mysql_query("INSERT INTO records (Title, Fname, Lname, Gender, DOB,MemX, MemType, Email) values ('".$title."', '".$fname."', '".$lname."', '".$gender."', '".$dob."', '".$memx."', '".$memtype."', '".$email."')", $conn); – Manish Jesani May 14 '16 at 09:41
  • echo your query and try running in phpmyadmin – Pardeep Poria May 14 '16 at 09:42
  • Your code is extremely vulnerable for SQL injections. See [this](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) to make your code more secure. – Dacaspex May 14 '16 at 09:44
  • Hi guys, This is only for a demonstration and not real world use. I am still not getting any uploads. I inserted a record manually and copied the sql code to make sure I had it right..still nothing. I get: Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\Connect.php on line 9 Connected to Database! – JSint May 14 '16 at 09:50
  • The error clearly tells you that your *version* of PHP doesn't support deprecated `mysql_` functions. `mysql_*` functions are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [And this is why you shouldn't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul May 14 '16 at 09:53
  • Mysqli::query and PDO::query do not work. how can I implement them into my code? – JSint May 14 '16 at 10:07

1 Answers1

0

Following code will help will help you to find the Error and resolve the Deprecation Error.

<?php
$con=mysqli_connect("localhost","root","","gym");
// Check connection
if (mysqli_connect_errno()){
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Perform queries 
mysqli_query($con, "INSERT INTO records (Title, Fname, Lname, Gender, DOB,MemX, MemType, Email) values ('" . $title . "', '" . $fname . "', '" . $lname . "', '" . $gender . "', '" . $dob . "', '" . $memx . "', '" . $memtype . "', '" . $email . "');" or die(mysqli_error($con));

mysqli_close($con);
?>
DarkKnight
  • 651
  • 5
  • 15