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

在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();
    }
}
?>

图片转代码服务由CSDN问答提供 功能建议

我创建了一个下拉列表以及类别和名称的输入类型,但在这种情况下我都是 单击下一步它将我引导到另一个页面,但我的数据库中没有任何保存。 company_account是必须插入数据的表名,它有四行id,category,cname,about

 &lt;?php include(“./inc/header  .inc.php“); 
 require(”./inc/connect.inc.php“);  ?&gt; 
 
&lt; div&gt; 
&lt; form action =“payment.php”method =“POST”&gt; 
&lt; select id =“category”name =“category”class =“old_ui_selector”&gt; \  n&lt; option value =“0”selected =“1”&gt;选择您的类别&lt; / option&gt; 
&lt; option value =“”&gt;会计师事务所&lt; / option&gt; 
&lt; option value =“”&gt  ;农业&lt; / option&gt; 
&lt; option value =“”&gt; Automotive&lt; / option&gt; 
&lt; option value =“”&gt; Aerospace / Defense&lt; / option&gt; 
&lt; option value =“”&gt;  ;建筑材料&lt; /选项&gt; 
 
&lt; / select&gt; 
&lt; / div&gt; 
&lt; br&gt; 
&lt; input type =“text”name =“cname”placeholder =“Name”/&gt; 
&lt;  ; br&gt;&lt; br&gt; 
单击“下一步”即表示您同意&lt; a href =“terms.php”style =“color:#008781”&gt;条款和条件。&lt; / a&gt; 
&lt; br&gt; \  n&lt; input type =“submit”name =“comp”value =“Next”/&gt; 
&lt; / form&gt; 
 
&lt;?php 
if(isset($ _ POST ['comp']))
  {
 $ category = $ _ GET ['category']; 
 $ cname = $ _POST ['cname']; 
 $ ins = mysql_query(“insert into company_account(  category)value('$ category')“); 
 $ insert = mysql_query(”INSERT INTO company_account VALUES('','$ category','$ cname','$ about')“); 
 if(n)  $ ins)
 if($ insert)
 {
 echo“&lt; br&gt;”。$ category。“inserted”; 
} 
 else 
 {
 echo mysql_error(); 
} \  n} 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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

相关推荐 更多相似问题