Skip to content
Advertisement

Adding values to MySQL [closed]

I am trying to test writing to MySQL database before putting all of the content of a registration form in a database but this something is wrong. Could someone review my PHP code to see where I am going wrong.

<html>
    <body>
        <form action="testingPHP.php" method="post">
            Name: <input type="text" name="Name"><br>
            Age: <input type="text" name="Age"><br>
            Gender: <select name="Gender">
                <option value=" "> </option>
                <option value="Boy">Boy</option>
                <option value="Girl">Girl</option>    
            </select><br>
            <input type ="submit">
        </form>
    </body>
</html>
    

PHP:

$Name = $_.POST['Name'];
$Age = $_.POST['Age'];
$Gender = $_.POST['Gender'];

$servername = "localhost";
$username = "testing";
$password = "testing1";
$db = "tesingdb";

$conn new mysql($sername, $username, $password, $db);

$sql = "insert into testingtable (name,age,gender)
values('$Name','$Age','$Gender')"

$conn->close();

Advertisement

Answer

Here are the changes.

  • Removed testingPHP.php from <form action="testingPHP.php" ...>
  • $_.POST to $_POST
  • $conn = new mysqli(....) instead of $conn new mysql(....)
  • Added $conn->query($sql);
  • Spelling mistake $servername

Rest of the code

<form action="" method="post">
    Name: <input type="text" name="Name"><br>
    Age: <input type="text" name="Age"><br>
    Gender: <select name="Gender">
      <option value=" "> </option>
      <option value="Boy">Boy</option>
      <option value="Girl">Girl</option>    
    </select><br>
    <input type ="submit">
</form>





<?php

$Name = $_POST['Name'];
$Age = $_POST['Age'];
$Gender = $_POST['Gender'];

$servername = "localhost";
$username = "testing";
$password = "testing1";
$db = "tesingdb";

$conn  = new mysqli($servername, $username, $password, $db);

$sql = "insert into testingtable (name,age,gender) values('$Name','$Age','$Gender')";
$conn->query($sql);

$conn->close();

?>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement