1

I want to make a connection between a hypothetical form and database (not localhost).

I do not get an error. However, data are not inserted in the database. I try to use the following codes, but not sure where is the issue. Please consider I am very new to PHP and it is very like that I have missed somethings.

<!DOCTYPE html>
<html>
<body>
<form method="post" action="new.php">
A : <input type="text" name="X1" placeholder="Enter A" /><br />
B : <input type="text" name="X2" placeholder="Enter B" /><br />
C : <input type="text" name="Y1" placeholder="Enter C" /><br />
D : <input type="text" name="Y2" placeholder="Enter D" /><br />
<br></br>
<input type="submit" value="Submit" />
</form>
</body>
</html>

<?php
    $dbhost = "db1109865_hf";
    $X1 = "dbX1";
    $X2 = "dbX2";
 $Y1 = "dbY1";
    $Y2 = "dbY2";
    $dbname = "abc";

$conn = new mysqli($dbhost, $X1, $X2,$Y1,$Y2, $dbname) or die('Could not connect');
$db = mysql_select_db($dbname);
?>
user1946911
  • 97
  • 1
  • 6
  • mysql_connect is outdated and deprecated so try to use modern techniqies like pdo https://phpdelusions.net/pdo#prepared – nbk Apr 07 '20 at 18:23
  • what is the equivalent to mysql_connect – user1946911 Apr 07 '20 at 18:28
  • mysqli_connect https://www.php.net/manual/de/function.mysqli-connect.php or pdo connect https://www.php.net/manual/de/pdo.connections.php but essentially you have to rewrite it and check also https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Apr 07 '20 at 18:31
  • add you new text and enable error messages. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display to see if your code produces error. – nbk Apr 07 '20 at 18:43
  • you complete code is not good. First you php code runs evertime, so you check if the page was submitted. you shoukld strat at the begining with a tutoial or two. btw. 500 means that it can't find your webpage, as i don't know what cpde and you have done – nbk Apr 07 '20 at 18:53
  • I have updated the question. – user1946911 Apr 07 '20 at 20:16

1 Answers1

0

This is not perfect, but it shows you how it is done. mysli connct has only 4 parametrs. As seen in the example.

Of course it will only open, when all paramerts databasename, hostname muserna, and password are correct.

You must especially during explorig and developing enable error reporting. So that you can check and eliminate errors that occir.

<!DOCTYPE html>
<html>
<body>
<form method="post" action="new.php">
A : <input type="text" name="X1" placeholder="Enter username" /><br />
B : <input type="text" name="X2" placeholder="Enter password" /><br />
C : <input type="text" name="Y1" placeholder="Enter C" /><br />
D : <input type="text" name="Y2" placeholder="Enter D" /><br />
<br></br>
<input type="submit" name="SubmitButton" value="Submit" />
</form>
</body>
</html>

<?php
if(isset($_POST['SubmitButton'])){


    $dbhost = "db1109865_hf";
    $dbname = "abc";
    $username = $_POST['X1']; 
    $password = $_POST['X1'];

 $Y1 = = $_POST['Y1'];
    $Y2 = = $_POST['Y2'];
    $link= new mysqli($dbhostT, $username, $password, $dbname;
    if (!$link) {
        echo "error connecting." . PHP_EOL;
        echo "Debug-errrorummer: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debug-error message: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    echo "connected";
}
?>
nbk
  • 20,484
  • 4
  • 19
  • 35