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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab