如何在php中建立表单和数据库之间的连接?

我想在假设的表单和数据库(不是本地主机)之间建立连接。


我没有收到错误。但是,数据没有插入到数据库中。我尝试使用以下代码,但不确定问题出在哪里。请考虑我是 PHP 的新手,很可能我错过了一些东西。


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

?>


繁星点点滴滴
浏览 102回答 1
1回答

30秒到达战场

这并不完美,但它向您展示了它是如何完成的。mysli connct 只有 4 个参数。如示例所示。当然,只有当所有参数数据库名称、主机名 muserna 和密码都正确时,它才会打开。特别是在探索和开发过程中,您必须启用错误报告。这样您就可以检查并消除发生的错误。<!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><?phpif(isset($_POST['SubmitButton'])){&nbsp; &nbsp; $dbhost = "db1109865_hf";&nbsp; &nbsp; $dbname = "abc";&nbsp; &nbsp; $username = $_POST['X1'];&nbsp;&nbsp; &nbsp; $password = $_POST['X1'];&nbsp; &nbsp; $Y1 = = $_POST['Y1'];&nbsp; &nbsp; $Y2 = = $_POST['Y2'];&nbsp; &nbsp; $link= new mysqli($dbhostT, $username, $password, $dbname;&nbsp; &nbsp; if (!$link) {&nbsp; &nbsp; &nbsp; &nbsp; echo "error connecting." . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; echo "Debug-errrorummer: " . mysqli_connect_errno() . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; echo "Debug-error message: " . mysqli_connect_error() . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp; echo "connected";}?>
打开App,查看更多内容
随时随地看视频慕课网APP