简体   繁体   English

在MySQL数据库中插入数据

[英]Inserting data in Mysql database

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 company_account是必须在其中插入数据的表名,它具有四行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();
    }
}
?>

You have a few issues there, the first is you are using mysql, either update it to mysqli or better still PDO. 您那里有一些问题,首先是您正在使用mysql,请将其更新为mysqli或更好的是PDO。

Second your form is submitting using POST and yet you try to collect the category using GET. 其次,您的表单是使用POST提交的,但是您尝试使用GET来收集类别。

You also need to supply the contents of your connect.inc.php WITHOUT THE IP AND PASSWORD so people can have a look at the config. 您还需要提供不带IP和密码的connect.inc.php内容,以便人们可以查看配置。

I would suggest before going any further you have read here and get a better understanding before you proceed. 我建议您在继续阅读之前,先阅读这里内容,然后再进行进一步的了解。

https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

Best of luck. 祝你好运。

Note: mysql : This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 注意: mysql :此扩展在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLiPDO_MySQL扩展。

You have provided with the form action and that will redirect after clicking the submit button. 您已经提供了form action ,在单击“提交”按钮后将进行重定向。

You have written the INSERT STATEMENT in the same page itself and that will not even work for you. 您已在同一页面本身中编写了INSERT STATEMENT ,这对您甚至不起作用。

First Remove up the action="" and then you redirect it based on the data saved. 首先删除action="" ,然后根据保存的数据将其重定向。

HTML: 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: 用于插入的PHP代码:

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. 您可以insert all in the single query本身,然后使用标题位置进行重定向。

Use header Location for redirection to particular page after the data has been saved. 保存数据后,使用header Location重定向到特定页面。

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

PHP PART: PHP部分:

<?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. 注意:您在插入语句中使用的$about ,但是没有值提供给插入语句。 Please check to that. 请检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM