doukun5339 2014-04-20 04:51
浏览 57
已采纳

联系表格不上传到数据库(MySQL)[关闭]

I have this contact form that I am trying to get to input information from my website into the designated table in my database. Does anyone know what I need to get this code to run correctly? I can input information in the textfields on my website and hit submit, but for some reason, the first name, last name, email, etc does not show up in the database at all. When I click submit, my "thank you" message does show up. I will include what my contact form, php, and database look like, perhaps that will help.

Contact Form contactus.php

<!DOCTYPE html>

<html>
    <head>
        <meta charset = "utf-8">
        <title>Contact</title>
        <style type = "text/css">
            em  { font-weight: bold;
                  color: black; }
            p   { font-size: 12pt; 
                  font-family: times new roman, sans-serif; color: black; }
        </style>
    </head>
    <body>
    <?php include ('menu.html'); ?>
    <? include ("form_process.php"); ?>
    <br> Please fill out the form below with your contact information.<br> 
    <form action= "form_process.php" method= "post" name= "contact_form"><br>
    <p>First Name:
    <input name= "first_name" type= "text" id= "first_name"/>
    </p><br>
    <p>Last Name:
    <input name= "last_name" type= "text" id= "last_name"/>
    </p><br>
    <p>Email:
    <input name= "email" type= "text" id= "email"/>
    </p><br> 
    <p>Phone:
    <input name= "phone" type= "text" id= "phone"/>
    </p><br>
    <p>Result:
    <div>
    <select name= "result" id= "result">
    <option value="">Choose</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">D</option>
    </select>
    </div>
    </p><br>
    <p>Comments: 
    <textarea name= "comments" id= "comments" placeholder= "Comment here" cols="25", rows="3"></textarea>
    </p><br>
    <p>Thank you for visiting our site!</p><br> 
    <p>
    <input type= "submit" name= "submit" id= "submit" value= "Submit" />
    <input type= "reset" name= "Reset" id= "reset" value= "Reset" />
    </p>
    </form>

    <div class="footer"><?php include('footer.html');?></div>

    </body>
</html>

Database The name of my database is project The table that I wish for the information to be input is called contactform & it is currently empty except the variables I have created. Within the table of contactform I have the variables:

ID int(10) AUTO_INCREMENT
first_name varchar(60)
last_name varchar(60)
email varchar(60)
phone int(15)
result varchar(15)
comments varchar(255)

**UPDATE: When I change my php to this (I tried to follow @DaveCartwright suggestions):

<?php

$user = 'root';
$pass = '';
$db = 'project';

$db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect to database");
/*I do not know what you mean by this: mysqli_real_escape_string($db, $escapeString);*/

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$result = $_POST['result'];
$comments = $_POST['comments'];

$first_name = mysql_real_escape_string($first_name);
$last_name = mysql_real_escape_string($last_name);
$email = mysql_real_escape_string($email);
$phone = mysql_real_escape_string($phone);
$result = mysql_real_escape_string($result);
$comments = mysql_real_escape_string($comments);

$query= "INSERT INTO `project`.`contactform` (`ID`,`first_name`, `last_name`, 
`email`, `phone`, `result`, `comments`) 
VALUES (NULL, '$first_name', '$last_name', '$email', '$phone', '$result', '$comments');";

mysqli_query($db, $query) or die(mysqli_error($db));

echo "<h2>Thanks, your information has been submitted!</h2>";

?>

I get several notices of undefined variables for escapeString (line 8), undefined index for: first_name (line 10), last_name (line 11), email (line 12), phone (line 13), result (line 14), comments (line 15). However, good news: when I look in my database table I do have some input there. The ID fills in correctly, then for the variable phone the only thing listed is 0 & all other variables are empty for each entry. This is at least some progress - I have not actually seen entries show up before now. How can I further fix this so that all the input will show up?

  • 写回答

2条回答 默认 最新

  • doufubu2518 2014-04-20 05:04
    关注

    Ok, the main problem I see is that you are mixing mysql and mysqli commands.

    First, you should definitly use

    mysqli_real_escape_string($db, $escapeString).
    

    secondly, your query should also use mysqli like:

    $query= "INSERT INTO `project`.`contactform` (`ID`,`first_name`, `last_name`, 
    `email`, `phone`, `result`, `comments`) 
    VALUES (NULL, '$first_name', '$last_name', '$email', '$phone', '$result', '$comments');";
    
    mysqli_query($db, $query) or die(mysqli_error($db));
    

    I'm also not sure if enclosing your variables is necessary...

    Update:

    Ok, because it seems to be such an issue to understand that mysql_real_escape_string() and mysqli_real_escape_string() are different functions... please change:

    $first_name = mysql_real_escape_string($first_name);
    $last_name = mysql_real_escape_string($last_name);
    $email = mysql_real_escape_string($email);
    $phone = mysql_real_escape_string($phone);
    $result = mysql_real_escape_string($result);
    $comments = mysql_real_escape_string($comments);
    

    to

    $first_name = mysqli_real_escape_string($db, $first_name);
    $last_name = mysqli_real_escape_string($db, $last_name);
    $email = mysqli_real_escape_string($db, $email);
    $phone = mysqli_real_escape_string($db, $phone);
    $result = mysqli_real_escape_string($db, $result);
    $comments = mysqli_real_escape_string($db, $comments);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行