将数据添加到 mySql [英] Adding Data to mySql

查看:45
本文介绍了将数据添加到 mySql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单并试图将数据插入到 mysql 数据库中.但它总是跳入错误.

I have a form and trying to insert data to the mysql database. but it always jump into the error.

相同的数据库连接工作正常,可以查看数据库中已有的数据.

Same database connection working fine to view data already in the database.

存储在单独文件中的页面的数据库连接:

Database Connection for the page stored in a separate file :

   <?php 
$host ="localhost";
$user = "CENSORED";
$password = "CENSORED";



$link = mysql_connect($host,$user,$password) or die("An error occurred while connecting...");

//Database Selection
$dbname="CENSORED";
mysql_select_db($dbname);

?>

HTML 表单

<form action="add_admin.php" method="post">
    <table>
        <tr>
        <td>Email Address :</td>
        <td><input id="admin_email" name="admin_email" type="text" size="20"</></td>
        </tr>
        <tr>
        <td>Name :</td>
        <td><input id="admin_name" name="admin_name" type="text" size="20"</></td>
        </tr>
        <tr>
        <td>Mobile :</td>
        <td><input id="admin_mobile" name="admin_mobile" type="text" size="12"</></td>
        </tr>
        <tr>
        <td>Address :</td>
        <td><textarea id="admin_address" name="admin_address" rows="4" cols="50"/> </textarea></td>
        </tr>
        <td>Password :</td>
        <td><input id="admin_pw" name="admin_pw" type="text" size="20"</></td>
        </tr>
        <td><input type="reset" value="Reset"></td>
        <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
    </form>

PHP 代码

    <?php
$admin_email=$_POST['admin_email'];
$admin_name=$_POST['admin_name'];
$admin_mobile=$_POST['admin_mobile'];
$admin_address=$_POST['admin_address'];
$admin_password=$_POST['admin_password'];

$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";

if( mysql_query($link,$sql))
    {
        echo "Records Added";
    }
    else
    {
        echo "ERROR";
        mysql_error($link);
    }

mysql_close($link);

?>

提前致谢.

推荐答案

您必须包含您的 Database connection 文件,该文件已作为单独的文件保存在您的 php 文件中.

you have to include your Database connection file which you have kept as separate file in your php file.

<?php
    include("dbconnection filename.php"):// this line.
    $admin_email=$_POST['admin_email'];
    $admin_name=$_POST['admin_name'];
    $admin_mobile=$_POST['admin_mobile'];
    $admin_address=$_POST['admin_address'];
    $admin_password=$_POST['admin_password'];

    $sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";

    if( mysql_query($link,$sql))
        {
            echo "Records Added";
        }
        else
        {
            echo "ERROR";
            mysql_error($link);
        }

    mysql_close($link);

?>

这篇关于将数据添加到 mySql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆