gpt4 book ai didi

php - 使用 PHP 将变量插入数据库表

转载 作者:行者123 更新时间:2023-11-29 07:38:43 25 4
gpt4 key购买 nike

我有两个 PHP 文件,Database_conn 和 findOutMore

database_conn 中的 PHP:

<?php

$webserver = 'localhost';
$password = 'xxxx';
$username = 'xxxx';
$database = 'xxxx';



$conn = mysqli_connect( $webserver, $username, $password, $database);

if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
?>

以及 findOutMore 中的 PHP

<?php
include 'database_conn.php';

$forename = $_REQUEST['forename'];
$surname = $_REQUEST['surname'];
$email = $_REQUEST['email'];
$landline = $_REQUEST['landLineTelNo'];
$mobile = $_REQUEST['MobileTelNo'];
$address = $_REQUEST['postalAddress'];
$method = $_REQUEST ['sendMethod'];

echo"<h3> These are your details </h3>";
echo"Name: ";
echo $forename;
echo " ";
echo $surname;
echo "<br>";
echo "Email: ";
echo $email;
echo "<br>";
echo "Landline: ";
echo $landline;
echo"<br>";
echo "Mobile: ";
echo $mobile;
echo "<br>";
echo "Address: ";
echo $address;
echo "<br>";
echo"Contact Method: ";
echo $method;


$query="INSERT INTO Database_Table
(forename, surname, postalAddress, landLineTelNo, MobileTelNo, email, sendMethod)
values (NULL, '$forename','$surname','$address','$landline','$mobile','$email','$method')";


if (mysqli_query($conn, $query)) {
echo "New record created successfully";
}

mysqli_close($conn);




?>

我实际上没有收到任何错误,并且回显正在正常工作(返回用户输入的值。)但数据实际上从未输入到数据库中。

我认为这可能与值开头的 NULL 有关,但我不太确定如何更改它,因为它应该是主键,即条目的 ID。

最佳答案

您需要实际运行查询;)

http://php.net/manual/en/mysqli.query.php

mysqli_query($conn, $query);

不过我想指出,您的脚本非常不安全。您应该考虑使用准备好的语句,并可能切换到 PDO:

http://php.net/manual/en/mysqli.prepare.php

http://php.net/manual/en/book.pdo.php

关于php - 使用 PHP 将变量插入数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29794331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com