无法将PHP表单数据存储在数据库中

时间:2017-09-19 12:14:32

标签: php html mysql mysqli

我正在开发一个基于PHP,Bootstrap和MySQLi构建的示例电子商务网站

我遇到2个PHP页面的问题:home.php和cart-script.php

带有文本输入和按钮的表单的home.php的一些相关部分是: `

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">
            <?php TitleQuery(2); ?>
        </div>
    </div>
    <div class="panel-body">
        <center>
            <?php
                PhotoQuery(2);
            ?>
        </center>
            <p>
                <?php
                    BodyQuery(2);
                ?>
            </p>
    <form action="cart-script.php" method="POST">
        <p>
            <center>
                <input type="number" class="form-control" name="name2" placeholder="Select quantity to add:">
                </input>
            </center>
        </p>
        <input type="button" name="submit2" class="btn btn-primary btn-block" value="Add to cart">
        </input>
    </form>
</div>

`

我的cart-script.php文件代码是: `

session_start();
require_once("connection.php");
if(isset($_POST['submit2']))
{
    $n=int($_POST['name2']);
    $id=2;
    $query1= "SELECT ItemName,Price FROM items WHERE ItemID='2'";
    $result1=mysqli_query($con,$iquery1);
    while($row=mysqli_fetch_array($result))
    {
        $iname=$row["ItemName"];
        $p=$row["Price"];
    }
    $query = "INSERT INTO shopcart VALUES 
             (
                 '{$id}',
                 '{$iname}',
                 '{$p}',
                 '{$n}'
             )";
    $result=mysqli_query($con,$query);
}

&GT;`

每当我点击home.php中的“添加到购物车”按钮,然后在WampServer上的PHPMyAdmin上检查数据库时,数据库都没有受到影响。 我哪里错了?

2 个答案:

答案 0 :(得分:1)

您只需提交表格:

<input
  type="submit"
  name="submit2"
  class="btn btn-primary btn-block"
  value="Add to cart">

您的表单尚未提交

答案 1 :(得分:0)

var RecordsComponent = React.createClass({
    getInitialState: function() {
        return {
            records: [{name:'Kevin Petersen',designation:"Junior Software Engineer",salary:"$1000"}, {name:'Michel Johnson',designation:"Senior Software Engineer",salary:"$2500"},{name:'Shane Bond',designation:"HR Manager",salary:"$1250"}],
            newRecord: [{name:'new name', designation:'new designation', salary:'new salary'}],
            expandedRecords : []
        }
    },
    render : function() {
        return (
            <div className="container" style={{"width" : "50%", "alignment" : "center"}}>
                <table className="table table-striped">
                    <thead>
                    <tr>
                        <th colSpan={2}>Records</th>
                    </tr>
                    </thead>
                    <tbody>
                    {this.state.records.map((r) => (
                        <tr>
                            <td onClick={this.showDetails}>
                                {r.name}
                            </td>
                            {this.renderDesignation(r.name)}
                            {this.renderSalary(r.name)}
                            <td>
                                <button className="tableBtn" onClick={() => this.deleteRow(r)}>Delete</button>
                            </td>
                        </tr>
                    ))}
                    </tbody>
                </table>
                <input type="text" id={"newNameId"} placeholder={"name"} onChange={this.updateNewName}></input>
                <input type="text" id={"newDesId"} placeholder={"designation"} onChange={this.updateNewDesignation}></input>
                <input type="text" id={"newSalId"} placeholder={"salary"} onChange={this.updateNewSalary}></input>
                <button id="addBtn" onClick={this.addRow}>ADD</button>
            </div>
        );
    },
    updateNewName: function(component) {
        this.setState({...this.state,
            newRecord: {...this.state.newRecord,name:component.target.value},
        });
    },
    updateNewDesignation: function(component) {
        this.setState({...this.state,
            newRecord: {...this.state.newRecord,designation:component.target.value},
        });
    },
    updateNewSalary: function(component) {
        this.setState({...this.state,
            newRecord: {...this.state.newRecord,salary:component.target.value}
        });
    },
    addRow : function() {
        var records = this.state.records;
        var newRecord = this.state.newRecord;
        records.push(newRecord)
        this.setState({records: records})
    },
    deleteRow : function(record) {
        this.setState({
            records: this.state.records.filter(r => r !== record)
        });
    },
    showDetails : function(record) {
        let expandedRecords = this.state.expandedRecords;
        expandedRecords.push(record.target.innerHTML);
        this.setState({...this.state, expandedRecords: expandedRecords });
    },
    renderDesignation : function(name){
        if(this.state.expandedRecords.includes(name)) {
            var row = this.state.records.filter(r=> r.name === name)[0]
            return(<td>{"Designation: "+row.designation}</td>);
        }
        return;
    },
    renderSalary : function(name){
        if(this.state.expandedRecords.includes(name)) {
            var row = this.state.records.filter(r=> r.name === name)[0]
            return(<td>Salary: {row.salary}</td>);
        }
        return;
    }
});

添加然后你有一些未定义的像$ iquery1我已经更新了它可能适合你的代码。