Asanka
Asanka

Reputation: 493

Adding Data to 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

<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 Code

    <?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);

?>

Thanks in advance.

Upvotes: 1

Views: 249

Answers (6)

MrK
MrK

Reputation: 1078

As you asked me to help out in one of my previous answers i decided to do some fancy stuff with this code :)

Remember, the db rows need to be named the same as your form name="name" for this to work!

db_connect.php:
$dbhost = ""; // this will ususally be 'localhost', but can sometimes differ
$dbname = ""; // the name of the database that you are going to use for this project
$dbuser = ""; // the username that you created, or were given, to access your database
$dbpass = ""; // the password that you created, or were given, to access your database

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('An error occured while connecting to: '. $dbhost.' as: '.$dbuser);
mysql_select_db($dbname, $conn) or die('Sorry, an error occured when selecting the database: '.$dbname);

form.php:

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

add_admin.php:

include 'db_connect.php'; //include connection


//Why add all post thingys when you can do it dynamically ?
$i = count($_POST);
$e = 0;


//Do a foreach loop on all POSTS coming in to this file.. 
foreach($_POST as $Key => $Value){


  //Add commas behind everything :)
  if($e++ < $i - 1){

      //Escaping all the strings:
      $Rows .= mysql_real_escape_string($Key).", ";
      $Values .= "'".mysql_real_escape_string($Value)."', ";
  }


  //if its the last one, dont add a comma behind!
  else{

      //Still escaping all the strings:
      $Rows .= mysql_real_escape_string($Key);
      $Values .= "'".mysql_real_escape_string($Value)."'";
  } 

}//end foreach loop





//Insert etc etc...
$sql = mysql_query("INSERT INTO admin($Rows) VALUES($Values)");

//If successful:
if(mysql_query($conn, $sql)){
    echo "Records added.";
}


//Error ?
else{
    echo "Sorry, an error occured while inserting to: ".$Rows;
    echo "<br/>";
    mysql_error($conn);
}


//Close connection:
mysql_close($conn);

Upvotes: 0

user5466402
user5466402

Reputation:

In your PHP page you should include your connection file:

require_once('yourdbconnection.php');

And change $_POST['admin_password'] to $_POST['admin_pw'] according to your HTML.

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
require_once('yourdbconnection.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_pw'];

$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')";

mysqli_query($link, $sql) or die("Error: " . mysqli_error($link));
mysqli_close($link);
?>

This works for me. If it doesn't for you then:

  • Check if query columns match table columns
  • Check if you are using the right database and the right table
  • Check if you are checking result on the right database and the right table

Hope this helps!

EDIT NOTE: I highly suggest you to switch from mysql to mysqli since mysql is now deprecated.

Upvotes: 0

KuKeC
KuKeC

Reputation: 4610

You have problems with connecting to a database. I don't like your approach to of connecting to a database so i'll provide mine approach (which works so far).

Your database config should look like

class DataBaseClass{
public $_host = "localhost";
public $_user = "X32284679";
public $_database = "X32284679";
public $_pass = "X32284679";

function connectToDatabase(){
$conn = new mysqli($this->_host, $this->_user, $this->_pass, $this->_database);
$conn->set_charset("utf8");
return $conn;

if(! $conn) {
    echo "Problems with connecting to database!";
    exit;
    }
}
}

Later on in some other code you use this file like this

require('nameOfFile.php');

$db = new DataBaseClass();
$mysqli=$db->connectToDatabase();
$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($rs = $mysqli->query($sql)) {
    //inserted        
else {
    //not inserted
    $mysqli->close();
}

And so on, try this approach and see if it helps you.

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

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

?>

Upvotes: 1

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

use mysql_real_escape_string

$admin_email=mysql_real_escape_string($_POST['admin_email']);
$admin_name=mysql_real_escape_string($_POST['admin_name']);
$admin_mobile=mysql_real_escape_string($_POST['admin_mobile']);
$admin_address=mysql_real_escape_string($_POST['admin_address']);
$admin_password=mysql_real_escape_string($_POST['admin_password']);

Upvotes: 0

MuthaFury
MuthaFury

Reputation: 815

Change to this

$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."')";

Upvotes: 0

Related Questions