PHP 用户输入未插入数据库

标签 php html mysql

我对这段代码感到很头疼,我知道我错过了一些简单的东西。我正在尝试将用户数据输入到名为 tbl_order 的表中

这是我的代码:

    <?php
    session_start();
    $connection = mysqli_connect('localhost', 'root', '', 'bookstore');

if (isset($_POST['submitted'])){

    $cTotal = $_SESSION['checkoutCartTotal'];
    $cName = $_POST['cardName'];
    $cNumber = $_POST['cardNum'];
    $cAdress = $_POST['cusAddress'];
    $cCity = $_POST['cusCity'];
    $cEmail = $_POST['cusEmail'];
    $cPhone = $_POST['cusPhone'];
    $sqlinsert = "INSERT INTO tbl_order (total_price, credit_card_number, fname, email, address, phone, city) VALUES ('$cTotal', $cNumber', '$cName', '$cEmail', '$cAdress', '$cPhone', '$cCity')";
    if (!mysqli_query($connection, $sqlinsert)) {
        die('Error inserting new record');
    } 


    $newrecord = "Thank you for making your purchase!";

}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Great Selling Book Store</title>
<link href="css/slider.css" rel="stylesheet" type="text/css" />

<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" />

<link rel="stylesheet" type="text/css" href="css/styles.css" />

<script language="javascript" type="text/javascript">

    function clearText(field)
    {
        if (field.defaultValue == field.value) field.value = '';
        else if (field.value == '') field.value = field.defaultValue;
    }

</script> 
</head>

<body id="subpage">

<div id="main_wrapper">
    <div id="main_header">
        <div id="site_title"><h1><a href="#" rel="nofollow">Great Selling Book Store</a></h1></div>

        <div id="header_right">
            <div id="main_search">
                <form action="products.php" method="get" name="search_form">
                  <input type="text" value="Search" name="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field" />
                  <input type="submit" name="Search" value="" alt="Search" id="searchbutton" title="Search" class="sub_btn"  />
                </form>
            </div>
         </div> <!-- END -->
    </div> <!-- END of header -->

    <div id="main_menu" class="ddsmoothmenu">
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="products.php">Books</a></li>
            <li><a href="shoppingcart.php">Cart</a></li>
            <li><a class="selected" href="checkout.php">Checkout</a></li>
            <li><a href="about.php">About</a></li>
        </ul>
        <br style="clear: left" />
    </div> <!-- end of menu -->

    <div class="cleaner h20"></div>
    <div id="main_top"></div>
    <div id="main">

        <div id="sidebar">
            <h3>Categories</h3>
            <ul class="sidebar_menu">
                <li><a href="index.php?cat=children">Children</a></li>              
                <li><a href="index.php?cat=Horror">Horror</a></li>
                <li><a href="index.php?cat=Thriller">Thriller</a></li>
        </ul>
        </div> <!-- END of sidebar -->

        <div id="content">
            <h2>Checkout</h2>
            <h5><strong>BILLING DETAILS<span style="color: #a11; font-size: 13px; margin-left: 50px;"><span></strong></h5>

            <form method ="post" action="checkout.php">
            <input type="hidden" name="submitted" value= "true" />
            <fieldset>
                <legend>Customer Checkout</legend>
                <label>Enter your name as it is on the credit card: <input type="text" name="cardName"></label>
                <label>Card Number: <input type="text" name="cardNum"></label>
                <label>Adress: <input type="text" name="cusAddress"></label>
                <label>City: <input type="text" name="cusCity"></label>
                <label>Email: <input type="text" name="cusEmail"></label>
                <label>Please, specify your reachable phone number. YOU MAY BE GIVEN A CALL TO VERIFY AND COMPLETE THE ORDER: <input type="text" name="cusPhone"></label>
            </fieldset>
            <input type="submit" value="Checkout!">
            </form>
            <div class="cleaner h50"></div>
            <h3>Shopping Cart</h3>
            <h4>TOTAL: <strong>R <?php echo @$_SESSION['checkoutCartTotal']; ?></strong></h4>
            <table style="border:1px solid #CCCCCC;" width="100%">
                <tr>
                    <td height="80px"> <img src="images/paypal.gif" alt="paypal" /></td>
                    <td width="400px;" style="padding: 0px 20px;">Recommended if you have a PayPal account. Fastest delivery time.
                    </td>



                </tr>
            </table>
               <?php
               echo @$newrecord;
               ?>
        </div> <!-- end of content -->

        <div class="cleaner"></div>
    </div> <!-- END of main -->

    <div id="main_footer">   
        <div class="cleaner h40"></div>
        <center>
            Copyright © 2048 DigitalNinja
        </center>
    </div> <!-- END of footer -->   

</div>


<!--<script type='text/javascript' src='js/logging.js'></script> -->
</body>
</html>

我得到的错误是我指定的错误插入数据库,我不知道我在哪里犯了错误

这是我的 tbl_order 数据库:

   CREATE TABLE `tbl_order` (
  `order_id` int(100) NOT NULL,
  `cus_id` int(100) NOT NULL,
  `prod_no` int(11) NOT NULL,
  `total_price` text NOT NULL,
  `credit_card_number` text NOT NULL,
  `fname` text NOT NULL,
  `email` text NOT NULL,
  `address` text NOT NULL,
  `phone` text NOT NULL,
  `city` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tbl_order` (`order_id`, `cus_id`, `prod_no`, `total_price`, `credit_card_number`, `fname`, `email`, `address`, `phone`, `city`) VALUES
(1, 1, 1, '350', '2147483647', '', '', '', '0', '');


ALTER TABLE `tbl_order`
  ADD PRIMARY KEY (`order_id`),
  ADD KEY `cus_id` (`cus_id`),
  ADD KEY `prod_no` (`prod_no`);

ALTER TABLE `tbl_order`
  MODIFY `order_id` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

ALTER TABLE `tbl_order`
  ADD CONSTRAINT `tbl_order_ibfk_1` FOREIGN KEY (`cus_id`) REFERENCES `customer` (`cus_id`),
  ADD CONSTRAINT `tbl_order_ibfk_2` FOREIGN KEY (`prod_no`) REFERENCES `tblproduct` (`prod_no`);

有人可以帮助我吗,我将不胜感激!

最佳答案

您在查询中错过了 $cNumber 之前的单引号

"INSERT INTO tbl_order (total_price, credit_card_number, fname, email, address, phone, city) 
    VALUES ('$cTotal', '$cNumber', '$cName', '$cEmail', '$cAdress', '$cPhone', '$cCity')"

关于PHP 用户输入未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009132/

相关文章:

PHP SimpleTest - 处理异常

html - 在 div 中间对齐 <ul>

html - CSS。中心内联 block 。不要将内容居中

html - 动态 Div 背景图像调整大小( Bootstrap )

php - 具有登录 session 的多个用户

php - Laravel SQL 查询返回重复项

php - 使用 PHP 编写包含 100,000 多个条目的 .tgz 文件,但避免写入单个文件

php - 找到迄今为止最近的周日?

php - 这个查找字符串所有排列的递归代码是如何工作的?

php - 将本地数据库更改为在线页面空白时