gpt4 book ai didi

php - 我们向用户提示的详细信息(通过 php 编码)未显示在 MySQL 数据库(本地主机)中

转载 作者:行者123 更新时间:2023-11-29 18:52:15 27 4
gpt4 key购买 nike

我最近编写了一个 php 代码,提示用户输入他们的姓名、电子邮件、网站、评论和性别(我正在使用 php 编码)。当用户已经输入数据时,数据应该显示在我的本地主机 MySQL 数据库( phpmyadmin )中。但这里的问题是,无论我将 INSERT INTO 函数放在哪里,用户键入的数据都不会插入到 phpmyadmin 中的表中。是的,我已经成功创建了该表(使用php 代码),但详细信息似乎没有插入到我创建的表中。

这是我的代码:

<?php
include "header.php";
include "lib/db.php";
include "module/functions.php";

error_reporting(0);
?>

<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST">

<?php
$name = "";
$email = "";
$gender = "";
$comment = "";
$website = "";
?>

<center>
<table border="13" width="400" height="400">
<tr>
<td colspan="5" align="center" bgcolor="blue">guest's information</td>
</tr>

<tr>
<td align="right">Name:</td><td> <input type="text" name="name" value="<?php echo $name;?>"></td>
</tr>

<tr>
<td align="right">E-mail: </td><td><input type="text" name="email" value="<?php echo $email;?>"></td>
</tr>

<tr>
<td align="right">Website: </td>
<td><input type="text" name="website" value="<?php echo $website;?>"></td>
</tr>

<tr>
<td align="right">Comment: </td>
<td><textarea name="comment" rows="10" cols="65"><?php echo $comment;?></textarea></td>
</tr>

<tr>
<td align="right">Gender: </td>
<td>
<input type="radio" name="gender"<?php if ($gender=="female"); ?> value="female">Female
<input type="radio" name="gender"<?php if ($gender=="male"); ?> value="male">Male
<td>
</tr>

<tr>
<td colspan="5" align="center"><input type="submit" name="submit" value="submit"></td>
</tr>

<tr>
<td colspan="5" align="center"><input type="reset" name="reset" value="reset"></td>
</tr>


<?php
include "config.inc.php";
?>

<?php
{
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comment = $_POST['comment'];
$gender = $_POST['gender'];

$query = "INSERT INTO myguests (name, email, website, comment, gender)
VALUES ('$name', '$email', '$website' , '$comment' , '$gender')";

}

if(isset($_POST['submit'])) {
if($name !="" && $email !="" && $website !="" && $comment !="" && $gender !="") {
$query = "INSERT INTO myguests (name, email, website, comment, gender)
VALUES ('$name', '$email', '$website' , '$comment' , '$gender')";

echo "You've entered the data.";


echo '<script language="javascript">';
echo 'alert("Yo brother, sudah la brother.")';
echo '</script>';

} else {

echo '<script language="javascript">';
echo 'alert("Yo brother, fuck off. Isi elok-elok la brother.")';
echo '</script>';
}
}


?>
</form>
</body>
</html>

这是我的配置代码(连接到本地主机数据库)

<?php
header("Content-Type: text/html; charset=utf-8");

//Set connection time limit/memory limit to infinity/unlimited
set_time_limit(0);
ini_set('default_charset', 'UTF-8');
ini_set('memory_limit', '-1');

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "first_test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50),
website VARCHAR(100),
comment VARCHAR(420),
gender VARCHAR(6)
)";

$name = "";
$email = "";
$website = "";
$comment = "";
$gender = "" ;

if (mysqli_query($conn, $sql)) {
echo "<br><br>Table MyGuests created successfully";
}

$query = "INSERT INTO myguests (name, email, website, comment, gender)
VALUES ('$name', '$email', '$website' , '$comment' , '$gender')";

if(mysqli_query($conn, $query)) {
echo "<br><br>Guest's data is inserted successfully into the database<br><br>";
} else {
echo "<br><br>Fail to insert guest's data into the database<br><br>";
}

mysqli_close($conn);
?>

谢谢您,祝您有愉快的一天。

最佳答案

你的问题实际上在这里:

 if(isset($_POST['submit'])) {
if($name !="" && $email !="" && $website !="" && $comment !="" && $gender !="") {
$query = "INSERT INTO myguests (name, email, website, comment, gender)
VALUES ('$name', '$email', '$website' , '$comment' , '$gender')";

echo "You've entered the data.";


echo '<script language="javascript">';
echo 'alert("Yo brother, sudah la brother.")';
echo '</script>';

}

您实际上正在构建字符串,但没有传递到数据库来查询它。

您实际上缺少的是这一行:

$result = mysql_query($query);

修改代码:

        $query = "INSERT INTO myguests (name, email, website, comment, gender)
VALUES ('$name', '$email', '$website' , '$comment' , '$gender')";

$result = mysql_query($query);

echo "You've entered the data.";

关于php - 我们向用户提示的详细信息(通过 php 编码)未显示在 MySQL 数据库(本地主机)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320261/

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