drnzpd579935 2016-10-12 08:44
浏览 142
已采纳

在Mysql数据库中插入数据

I have created a drop down list as well a input type for for category and name but in both the case as i click next it directs me to another page but nothing saves in my database. company_account is the table name in which data has to be inserted it has four rows id, category, cname, about

<?php include( "./inc/header.inc.php");
 require( "./inc/connect.inc.php"); ?>

<div>
<form action = "payment.php" method= "POST">
<select id="category" name="category" class="old_ui_selector">
    <option value="0" selected="1">Choose your category</option>
    <option value="">Accounting Firm</option>
    <option value="">Agriculture</option>
    <option value="">Automotive</option>
    <option value="">Aerospace/Defence</option>
    <option value="">Building Material</option>

</select>
</div>
<br>
<input type = "text" name="cname" placeholder= "Name"/>
<br><br>
By clicking Next you agree to the <a href="terms.php" style="color: #008781">Terms and Conditions.</a>
<br>
<input type = "submit" name="comp" value="Next"/>
</form>

<?php
if(isset($_POST['comp']))
{
    $category=$_GET['category'];
    $cname = $_POST['cname'];
    $ins=mysql_query("insert into company_account (category) values ('$category')");
    $insert = mysql_query("INSERT INTO company_account VALUES ('','$category','$cname','$about')");
    if($ins)
        if($insert)
    {
        echo "<br>".$category."inserted";
    }
    else
    {
        echo mysql_error();
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dongyuan9892 2016-10-12 08:49
    关注

    Note: mysql: This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    You have provided with the form action and that will redirect after clicking the submit button.

    You have written the INSERT STATEMENT in the same page itself and that will not even work for you.

    First Remove up the action="" and then you redirect it based on the data saved.

    HTML:

    <form action = "" method= "POST">
    </form>
    

    And you Option value is missing out values and after the form is submitted the values will not be entered into the DB. Hence you need to do the following.

    Replace:

    <option value="">Accounting Firm</option>
    

    With:

    <option value="Accounting Firm">Accounting Firm</option>
    

    PHP Code for Insert:

    You Insert Code will not work since you have not provided with the table values for insert operations.

    Syntax:

    INSERT INTO TABLENAME(`columnname1`,`columnname2`,.....) VALUES(VALUE1,VALUE1,....)
    

    Hence you are advised to use the correct table structure for the Operations and then insert the data.

    INSERT QUERY:

    $insert = mysql_query("INSERT INTO company_account(`category`,`name`,`about`) VALUES ('$category','$cname','$about')");
    

    You can insert all in the single query itself and then redirect using the header location.

    Use header Location for redirection to particular page after the data has been saved.

    header('location : http://www.google.co.in');
    

    PHP PART:

    <?php
    if(isset($_POST['comp']))
    {
        $category=$_POST['category'];
        $cname = $_POST['cname'];
        $insert = mysql_query("INSERT INTO company_account(`category`,`name`,`about`) VALUES ('$category','$cname','$about')");
        $rows = mysql_affected_rows();
        if($rows=0)
        {
            echo mysql_error();
        }
        else
        {
            ob_start(); // Refresh the Output Buffer
            header('location: http://www.google.co.in');
            exit;
        }
    }
    ?>
    

    Note: $about you are using in the Insert Statement but no values are provided over to the Insert Statement. Please check to that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?