将表单信息发送到数据库时,它只会发送一堆0

时间:2019-02-10 22:50:50

标签: php html mysql

当我试图创建MySQL订单时,我遇到了一个问题。它可以毫无问题地连接数据库,但与其发送表格中提供的信息,不如发送一堆0。我不确定是什么问题。有任何想法吗?在下面,您可以找到数据库条目:

database entry

<?php

define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}

$db_selected = mysqli_select_db($connection, DB_NAME);

if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}

echo 'Connected successfully';

$name = $_POST ['name'];
$email = $_POST ['email'];
$discord = $_POST ['discord'];
$website = $_POST ['website'];
$color = $_POST ['color'];
$budget = $_POST ['budget'];

$sql = "INSERT INTO orderform (name, email, discord, website, color, budget) VALUES ('$name', '$email', '$discord', '$website', '$color', '$budget')";

if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
?><!DOCTYPE html>
<html>
<head>
</head>
<body>

<form method="post" action="form.php">

<div class="question">
<label>Name</label>
<input type="text" name="name" required>
</div>

<div class="question">
<label>E-mail Address</label>
<input type="email" name="email" required>
</div>

<div class="question">
<label>Discord</label>
<input type="text" name="discord" required>
</div>

<div class="question">
<label>Website Address (If applicable)</label>
<input type="url" name="website" required>
</div>

<div class="question">
<label>Budget</label>
<input type="text" name="budget" required>
</div>

<div class="question">
<label>Color Scheme</label>
<input type="text" name="color" required>
</div>

<div class="submit">
<input type="submit" name="submit" value="Send">
</div>

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

是否所有PHP都包裹在if语句中,以检查是否确实按下了“提交”按钮,或者它只是在页面加载时运行,而没有人有机会填写并提交表格?

    if(isset($_POST['submit'])) {
      // all your php in here...
    }