dougengqiu8031 2013-04-19 18:20 采纳率: 100%
浏览 106

使用PHP将表单数据传递到MySQL数据库

I am unable to pass the following form data:

<form method="post" action="form.php" id="contact" enctype="multipart/form-data">
        <fieldset>
        <legend>Contact Us</legend>
        <div id="conleft">
        <label>First Name:</label><input type="text" name="firstName" required />
        <label>Last Name:</label><input type="text" name="lastName" required />
        <label>House/Flat No:</label><input type="text" name="houseNum"  />
        <label>Address:</label><input type="text" name="address" />
        <label>Town/City:</label> <input type="text" name="city" />
        <label>Postcode:</label> <input type="text" name="postcode" />
        <label>Telephone:</label> <input type="tel" name="telephone" />
        <label>Email:</label> <input type="email" name="email" required />
        </div>
        <div id="conright">
        <label>Enquiry:</label><textarea name="description" rows="13" required ></textarea>
        <label>Date:</label><input type="month" name="date" /><br /><br />
        <input type="submit" name="submit" value="Send" />
        <input type="reset" name="Reset" value="Reset" />
        <input type="hidden" name="customerNo" />
        <input type="hidden" name="enquiryNo" />
        <input type="radio" name="type" value="customer" checked />
        </div>
        </fieldset>
    </form>

with the following PHP to MySQL database

<?php
$con=mysqli_connect("localhost", "root", "myuser","mypass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql1="INSERT INTO customers (NULL, firstName, lastName, houseNum, address, city, postcode, telephone, email, type)
VALUES ('$_POST[customerNo]','$_POST[firstName]','$_POST[lastName]','$_POST[houseNum]','$_POST[address]','$_POST[city]','$_POST[telephone]', '$_POST[postcode]''$_POST[email]','$_POST[type]')";

mysqli_query($con,$sql1);

$sql2="INSERT INTO enquiry (NULL, customerNo, description, date)
VALUES ('$_POST[enquiryNo]','$_POST[customerNo]','$_POST[description]','$_POST[lastName]','$_POST[date]')";

mysqli_query($con,$sql2);

echo "<script language=javascript>window.location = 'thanks.html';</script>";

mysqli_close($con);
?>

The web page acts as if the form data has been sent by showing the thank.html page when submitted, but no data is populated in the database. I've set AUTO INCREMENT, PRIMARY and FOREIGN keys, is it the way I am trying to the pass the values?

  • 写回答

4条回答 默认 最新

  • dongya9904 2013-04-19 18:45
    关注

    use '".$_POST['customerNo']."' instead of '$_POST[customerNo]' and something like that for all $_POST values in your INSERT command

    EDIT 1:

    It seems customerNO and enquiryNo are auto-increment columns. So

    $sql1="INSERT INTO customers (NULL, firstName, lastName, houseNum, address, city, postcode, telephone, email, type)
    VALUES ('$_POST[customerNo]','$_POST[firstName]','$_POST[lastName]','$_POST[houseNum]','$_POST[address]','$_POST[city]','$_POST[telephone]', '$_POST[postcode]''$_POST[email]','$_POST[type]')";
    

    should be

    $sql1="INSERT INTO customers (customerNo, firstName, lastName, houseNum, address, city, postcode, telephone, email, type)
    VALUES (NULL,'$_POST[firstName]','$_POST[lastName]','$_POST[houseNum]','$_POST[address]','$_POST[city]','$_POST[telephone]', '$_POST[postcode]''$_POST[email]','$_POST[type]')";
    
    mysqli_query($con,$sql1);
    $customerNO=mysql_insert_id();
    

    and then

    $sql2="INSERT INTO enquiry (NULL, customerNo, description, date)
    VALUES ('$_POST[enquiryNo]','$_POST[customerNo]','$_POST[description]','$_POST[lastName]','$_POST[date]')";
    

    should be

    $sql2="INSERT INTO enquiry (enquiryNO, customerNo, description, date)
    VALUES (NULL,'$customerNo','$_POST[description]','$_POST[lastName]','$_POST[date]')";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误