gpt4 book ai didi

php - Mysql 插入不起作用并且没有给出错误

转载 作者:行者123 更新时间:2023-11-29 07:42:12 26 4
gpt4 key购买 nike

我不知道为什么下面的代码无法将数据插入mysql。

if (!$link = mysql_connect('server', 'user', 'password')) {
echo '700';
exit;
}

if (!mysql_select_db('vendors', $link)) {
echo '701';
exit;
}
$sql2 = "INSERT INTO transactions (TransID, payment_status, last_name, first_name, payer_email, address_name, address_state, address_zip, address_country, verify_sign, payment_gross, ipn_track_id, business, reciver_email) VALUES ('kris', 'kris', 'kris', 'kris', 'kris','kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris')";

$result2 = mysql_query($sql2, $link);

代码有什么问题吗?php 没有给出任何错误。

最佳答案

请尽量不要使用mysql_connect,而是使用mysqli_connectPDO_MySQL read this

也可以使用 die 查找代码中是否存在任何错误

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

否则(推荐方式)-

程序风格

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
echo "New Person created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

MySQLi面向对象风格

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
echo "New Person created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

关于php - Mysql 插入不起作用并且没有给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734963/

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